ForkJoinPool result never arrived - java

Hi I am new to Java concurrency and I am trying to Double the List Content by fork join and dividing the task into multiple parts.
The task gets Completed but result never arrived.
package com.learning;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;
class DoubleNumbers extends RecursiveTask<List<Integer>> {
private final List<Integer> listToDo;
public DoubleNumbers(List<Integer> list) {
System.out.println("Cons Called"+list.get(0));
this.listToDo = list;
}
#Override
protected List<Integer> compute() {
List<DoubleNumbers> doubleNumbersList= new ArrayList<>();
System.out.println(Thread.currentThread().toString());
for (int i = 0; i < listToDo.size(); i++) {
listToDo.set(i, listToDo.get(i) * 2);
}
return listToDo;
}
}
public class FJPExample {
public static void main(String[] args) {
List<Integer> arrayList = new ArrayList<>();
for (int i = 0; i < 149; i++) {
arrayList.add(i, i);
}
ForkJoinPool forkJoinPool = new ForkJoinPool(4);
System.out.println(forkJoinPool.getParallelism());
DoubleNumbers doubleNumbers = new DoubleNumbers(arrayList.subList(0, 49));
DoubleNumbers doubleNumbers50ToNext = new DoubleNumbers(arrayList.subList(50, 99));
DoubleNumbers doubleNumbers100ToNext = new DoubleNumbers(arrayList.subList(100, 149));
forkJoinPool.submit(doubleNumbers);
forkJoinPool.execute(doubleNumbers50ToNext);
forkJoinPool.execute(doubleNumbers100ToNext);
do {
System.out.println("Parallel " + forkJoinPool.getParallelism());
System.out.println("isWorking" + forkJoinPool.getRunningThreadCount());
System.out.println("isQSubmission" + forkJoinPool.getQueuedSubmissionCount());
try {
TimeUnit.SECONDS.sleep(1000);
} catch (InterruptedException e) {
//
}
} while ((!doubleNumbers.isDone()) || (!doubleNumbers50ToNext.isDone()) || (!doubleNumbers100ToNext.isDone()));
forkJoinPool.shutdown(); // Line 56
arrayList.addAll(doubleNumbers.join());
arrayList.addAll(doubleNumbers50ToNext.join());
arrayList.addAll(doubleNumbers100ToNext.join());
System.out.println(arrayList.size());
arrayList.forEach(System.out::println);
}
}
If I debug my task then I am able to find the numbers gets doubled but the result never arrived at line no 56

Issue is with the code arrayList.addAll(doubleNumbers.join()) , line# 54, 55 and 56 because this may result into ConcurrentModificationException. So, what you can do is, replace these lines with below lines and it will work(it will work because you have used arrayList.subList at line #36 which is backed by same arraylist, read its javadoc for more info)
doubleNumbers.join();
doubleNumbers50ToNext.join();
doubleNumbers100ToNext.join();

Related

Split with new line and store in datastructure

I have text file data like :
2,2,1
data1,123,89,1
data2,124,90,2
data3,125,91,3
data4,126,92,4
data5,127,93,5
data6,128,94,6
data7,129,95,7
data8,130,96,8
data9,131,97,9
data10,132,98,10
The first line 2,2,1 indicate 2 lines from 1st set of lines and store it in nodeFile, 2 lines from 2nd set of lines store it in linkFile and 1 line from 3rd set of lines store it in moduleFile. However for example purpose I have shows small number of lines but its a larger file.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadFile {
static List<String> moduleFile = new ArrayList<>();
static List<String> linkFile = new ArrayList<>();
static List<String> nodeFile = new ArrayList<>();
static int a[];
public static void main(String[] args) {
File file11 = new File("/home/madhu/Desktop/node.txt");
Scanner scAll = null;
try {
scAll = new Scanner(file11);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] numberOfLines = (scAll.nextLine()).split(",");
int flag = 0;
int counter = 1;
while (scAll.hasNext()) {
if (flag == 0 && "\\n\\n".equals(scAll.nextLine()) && counter <= Integer.parseInt(numberOfLines[0].trim())) {
for (int i = 0; i < Integer.parseInt(numberOfLines[0].trim()); i++) {
System.out.println(scAll.nextLine());
nodeFile.add(scAll.nextLine());
counter++;
}
if (counter > Integer.parseInt(numberOfLines[0].trim())) {
flag = 1;
counter = 1;
}
} else if (flag == 1 && "\\n\\n".equals(scAll.nextLine())
&& counter <= Integer.parseInt(numberOfLines[1].trim())) {
for (int i = 0; i < Integer.parseInt(numberOfLines[1].trim()); i++) {
System.out.println(scAll.nextLine());
linkFile.add(scAll.nextLine());
counter++;
}
if (counter > Integer.parseInt(numberOfLines[1].trim())) {
flag = 2;
counter = 1;
}
} else if (flag == 2 && "\\n\\n".equals(scAll.nextLine())
&& counter <= Integer.parseInt(numberOfLines[2].trim())) {
for (int i = 0; i < Integer.parseInt(numberOfLines[2].trim()); i++) {
System.out.println(scAll.nextLine());
moduleFile.add(scAll.nextLine());
counter++;
}
} else {
continue;
}
}
scAll.close();
}
}
I have written the above code, but this code gets terminated during execution. How to get the desired result? Please help.
Hopefully I am not misunderstanding, but this is what I'd do.
I didn't check to see if this is fully working example, but it should work more or less.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
class ReadFile {
// basically just do what you did
static List<String> nodeFile;
static List<String> linkFile;
static List<String> moduleFile;
public static void main(String[] args) throws FileNotFoundException {
final File file = new File("/home/madhu/Desktop/node.txt");
final Scanner scanner = new Scanner(file);
// make it a little better for indexing
final List<Integer> selections = Arrays
.stream(scanner.nextLine().split(","))
.map(Integer::parseInt)
.collect(Collectors.toList());
// this is the meat of the code
// basically each split up block of lines is a block
final List<List<String>> blocks = new ArrayList<>();
while (scanner.hasNextLine()) {
List<String> lines = new ArrayList<>();
String line;
while (!(line = scanner.nextLine()).equals("\n")) {
lines.add(line);
}
if (!lines.isEmpty()) {
blocks.add(lines);
}
}
// allocate your files now
nodeFile = blocks.get(0).subList(0, selections.get(0));
linkFile = blocks.get(1).subList(0, selections.get(1));
moduleFile = blocks.get(2).subList(0, selections.get(2));
scanner.close();
}
}
try this code , i use BufferedReader because its more cleaner :
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadFile {
static List<String> moduleFile = new ArrayList<>();
static List<String> linkFile = new ArrayList<>();
static List<String> nodeFile = new ArrayList<>();
public static void main(String[] args) {
File file = new File("/home/madhu/Desktop/node.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String data[] = reader.readLine().split(",");
String s;
int nbline=0, i=0,block =0;
while ((s = reader.readLine())!=null && block < data.length) {
if(s.equals("")){
block++;
nbline = Integer.parseInt(data[block-1]);
i = 0;
}
for(;i<nbline;i++){
s = reader.readLine();
if(s == null) break;
else if(s.equals("")){
block++;
break;
}
switch(block){
case 1 :
nodeFile.add(s);
break;
case 2:
linkFile.add(s);
break;
default: moduleFile.add(s);
}
}
}
} catch (IOException | NumberFormatException ex) {
System.err.println(ex.getStackTrace());
}
finally{
closeReader(reader);
}
System.out.println("nodeFile : "+nodeFile);
System.out.println("linkFile : "+linkFile);
System.out.println("moduleFile : "+moduleFile);
}
public static void closeReader(BufferedReader reader) {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
output :
nodeFile : [data1,123,89,1, data2,124,90,2]
linkFile : [data5,127,93,5, data6,128,94,6]
moduleFile : [data8,130,96,8]

How to make multithreads code with 2 loops

I trying to make my code faster with multi-threads.
I have 2 bigs arrays (in my code above I put small arrays for the example).
This is my code that I tryied, but its stuck and not make anything:
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private ArrayList<String> blackList = new ArrayList<String>();
String[] cities = {
"London",
"Paris",
"Barcelona"
};
String[] cats = {
"Animals",
"Jobs",
"Env",
"B"
};
public static void main(String[] args) {
Main m = new Main();
m.run();
}
public void run() {
ExecutorService svc = Executors.newCachedThreadPool();
int chunks = Runtime.getRuntime().availableProcessors();
long iterationsCities = cities.length;
long iterationsCats = cats.length;
for (int i = 0; i < chunks; ++i) {
int startCities = (int) (iterationsCities / chunks * i);
int endCities = (int) (iterationsCities / chunks * (i + 1));
int startCats = (int) (iterationsCats / chunks * i);
int endCats = (int) (iterationsCats / chunks * (i + 1));
svc.execute(new Task(startCities, endCities, startCats, endCats));
}
}
public class Task implements Runnable {
int startCities;
int endCities;
int startCats;
int endCats;
public Task(int startCities, int endCities, int startCats, int endCats) {
this.startCities = startCities;
this.endCities = endCities;
this.startCats = startCats;
this.endCats = endCats;
}
public void launch() throws IOException, InterruptedException {
for(int i=startCities; i<endCities; i++)
{
for(int j=startCats; j<endCats; j++)
{
String link = "https://.../pro/search/" + cats[j] + "/" + cities[i];
//System.out.println(link);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(link))
.GET()
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
//System.out.println(link);
String html = response.body();
Pattern pattern = Pattern.compile("data-count=\"([A-Za-z0-9_]*)\"");
Matcher matcher = pattern.matcher(html);
boolean matchFound = matcher.find();
if(matchFound)
{
int dataCount = Integer.parseInt(matcher.group(1));
if(dataCount == 0)
{
String l = cats[j] + "/" + cities[i];
if(!blackList.contains(l)) {
blackList.add(l);
}
}
}
}
}
}
#Override
public void run() {
try {
launch();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
My code makes http request, gets value in the meta tag of the HTML and asks if the value equal to 0, if yes it saves the URL in the blacklist.

CharAt index error while reading through text file in Java

I'm getting some confusing errors when I iterate through this entire enable1 txt file (https://raw.githubusercontent.com/dolph/dictionary/master/enable1.txt) to check if it meets the "I before E except after C" English word 'rule'. I noticed the code succeeds when I remove the "-1" from charAt(indexEI - 1) that I starred below (****).
Any ideas why this might be? The errors just say "at java.lang.String.charAt (String.java: 658)", "Main.ibeforeE", and "at Main.main" in seemingly random spots in the "e" section of the iteration. Then it says var\cache\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED at the very end.
I'm quite new to Java so any other constructive criticism is appreciated as well. Thanks!
Main:
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
EnableWord test = new EnableWord();
test.EnableWords();
Scanner read = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
list = test.getList();
int x = 0;
int falseCounter = 0;
while (x < list.size()) {
System.out.print(list.get(x) + ": ");
String input = list.get(x);
if (input.equals("x")) {
break;
} else {
System.out.println(iBeforeE(input));
if(!iBeforeE(input)) {
falseCounter++;
}
x++;
}
}
System.out.println(falseCounter);
}
public static boolean iBeforeE(String input) {
if (!input.contains("ie") && !input.contains("ei")) {
return true;
}
if (input.contains("ie")) {
int indexIE = input.indexOf("ie");
Character searchIE = input.charAt(indexIE - 1);
if (!searchIE.toString().equals("c")) {
return true;
}
} else if (input.contains("ei")) {
int indexEI = input.indexOf("ei");
****Character searchEI = input.charAt(indexEI - 1);****
if (searchEI.toString().equals("c")) {
return true;
}
}
return false;
}
Class EnableWord:
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class EnableWord {
private ArrayList<String> list;
private Scanner s;
private File file;
public EnableWord() {
}
public void EnableWords() {
try {
this.s = new Scanner(this.file = new File("enable1.txt"));
} catch (FileNotFoundException ex) {
Logger.getLogger(EnableWord.class.getName()).log(Level.SEVERE, null, ex);
}
this.list = new ArrayList<>();
while (s.hasNext()) {
list.add(s.next());
}
s.close();
}
public void printWords() {
for (String word : list) {
System.out.println(word);
}
}
public ArrayList<String> getList() {
ArrayList<String> newList = new ArrayList<String>();
for (String word : list) {
newList.add(word);
}
return list;
}
}
There are words in your list that start with ei (ex: eicosanoid)
When your program gets to these words, it finds the index of 'ei' to be 0. So the value of "indexEI - 1" is negative 1, an invalid index.
you could fix it by checking if indexEI is > 0 before trying to check the previous character is a 'c':
if (indexEI == 0 || input.charAt(indexEI - 1).toString().equals("c")) {
return true;
}
As soon as a word starts with 'ie' or 'ei' then input.charAt(indexIE - 1) will generate an error since indexIE then is 0.
I am not sure what you want to do in your code but some kind of check is needed
if (indexIE == 0) {
} else {
//current code
}

How to assign values to an ArrayList in a class?

I have been having trouble for the longest time on figuring out why you seemingly cannot assign values to an ArrayList without getting an error.
The first block of code is the main method which gets line by line from a textfile and splits the line using a delimiter. The first string is stored in one variable as a long, then other 9 strings are stored in an ArrayList. It does this for every line in the file.
I have debugged this code many times and it shows that the array is getting the right values.
The issues is when the code reaches the part where it calls insert, which I have commented in the code.
It first goes to create the node, but when it reaches the part when it is going to add the values from the first ArrayList to the newly created ArrayList, everything breaks. The for loop stops functioning as it should and it continues to increment even though the limit was reached.
I have decided to omit the BinaryTree Class which I also use for this project because it works as it should.
So how would I go about properly assigning the values from the ArrayList that I pass to the Node ArrayList?
package assignment7;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.Long.parseLong;
import java.util.Scanner;
import java.util.ArrayList;
public class Assignment7 {
public static void main(String[] args) throws IOException {
boolean headerLine = true;
String firstLine = "";
String catchLine;
String token;
long s_cid;
ArrayList<String> Arr = new ArrayList<String>();
Scanner delimS;
int count = 0;
BinaryTree snomedTree = new BinaryTree();
try(BufferedReader br = new BufferedReader(new FileReader("Data.txt"))) {
while ((catchLine = br.readLine()) != null) {
for(int i = 0; i < 9; i++){
Arr.add("");
}
if(headerLine){
firstLine = catchLine;
headerLine = false;
}
else{
delimS = new Scanner(catchLine);
delimS.useDelimiter("\\|");
s_cid = parseLong(delimS.next());
while(delimS.hasNext()){
token = delimS.next();
Arr.set(count, token);
count++;
}
//Runs fine up to here then this insert function is called
snomedTree.insert(new Node(s_cid, Arr));
}
Arr.clear();
}
}
try (PrintWriter writer = new PrintWriter("NewData.txt")) {
writer.printf(firstLine);
snomedTree.inorder(snomedTree.root, writer);
}
}
}
Here is the Node class:
package assignment7;
import java.util.ArrayList;
class Node {
public long cid;
public ArrayList<String> Satellite;
public Node l;
public Node r;
public Node(long cid, ArrayList<String> Sat) {
this.Satellite = new ArrayList<String>();
this.cid = cid;
//This for loop continues to run even after i = 9
for(int i = 0; 0 < 9; i++){
Satellite.add(Sat.get(i));
}
}
}
And this is the Binary Tree class:
package assignment7;
import java.util.ArrayList;
import java.io.PrintWriter;
public class BinaryTree {
public Node root;
public BinaryTree() {
this.root = null;
}
public void insert(Node x) {
if (root == null) {
root = x;
}
else {
Node current = root;
while (true) {
if (x.cid > current.cid) {
if (current.r == null) {
current.r = x;
break;
}
current = current.r;
}
else {
if (current.l == null) {
current.l = x;
break;
}
current = current.l;
}
}
}
}
public void inorder(Node x, PrintWriter out) {
if (x != null) {
inorder(x.l, out);
out.printf(String.valueOf(x.cid) + "|");
for(int i = 0; i < 9; i++){
if(i != 8){
out.printf(x.Satellite.get(i) + "|");
}
else{
out.printf(x.Satellite.get(i) + "\n");
}
}
inorder(x.r, out);
}
}
}
public Node(long cid, ArrayList<String> Sat) {
this.Satellite = new ArrayList<String>();
this.cid = cid;
for(int i = 0; i < Sat.size()-1; i++){
Satellite.add(Sat.get(i));
}
}
here 0<9 is wrong,
what error are u getting ??
Thanks to Miller Cy Chan's comment it works perfect now.
I changed the Node class to:
package assignment7;
import java.util.ArrayList;
import java.util.List;
class Node {
public long cid;
public ArrayList<String> Satellite;
public Node l;
public Node r;
public Node(long cid, ArrayList<String> Sat) {
this.Satellite = new ArrayList<String>(Sat.subList(0, Math.min(9, Sat.size())));
this.cid = cid;
}
}
I also made sure to change the "count" variable in the main back to 0 after each iteration.

The method importTeams() is undefined for the type SchedulerOutput

I'm creating a scheduler in Java. I had everything inside in one class, but now I want to split it up into separate classes. It's quite small program, so there's likely little benefit, but I want to get the concepts of it correct. Code is below.
I'm getting an error in the second class on the importTeams() method. I thought that as long as I imported the package this method was in, it would be ok. Obviously not. Any tips?
package fifa.scheduler.fileimport;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import fifa.scheduler.output.*;
public class FileRead2 {
private String rrTeam;
public List<String> importTeams() {
ArrayList<String> teamList = new ArrayList<String>();
BufferedReader br = null;
int linecount = 0;
String teamcounter;
try {
br = new BufferedReader(new FileReader("path"));
while (br.readLine() != null){
linecount ++;
}
br.close();
br = new BufferedReader(new FileReader("path"));
setRrTeam(br.readLine());
while ((teamcounter = br.readLine()) != null) {
teamList.add(teamcounter);
}
if (linecount % 2 != 0) {
teamList.add("byeteam");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return teamList;
}
public static void main(String args[]){
FileRead2 fr = new FileRead2();
fr.PrintTeams();
}
private void setRrTeam(String rrTeam) {
this.rrTeam = rrTeam;
}
private String getRrTeam() {
return rrTeam;
}
}
package fifa.scheduler.output;
import java.util.Collections;
import java.util.List;
import fifa.scheduler.fileimport.FileRead2;
public class SchedulerOutput {
public void PrintTeams(){
List<String> teamList = importTeams();
int tl = teamList.size();
int bh = 0;
int uh = (tl - 2);
for (int i = 0; i <=(teamList.size()-1); i++) {
System.out.println("Week " + (i+1) + " fixtures");
System.out.println(getRrTeam() + " vs " + teamList.get(tl -1));
while ((bh <= (tl / 2)) && (uh >= ((tl / 2)))) {
System.out.println(teamList.get(bh) + " vs " + teamList.get(uh));
bh++;
uh--;
}
Collections.rotate(teamList, 1);
tl = teamList.size();
bh = 0;
uh = (tl - 2);
}
}
}
When you import the class using import statement, only the class's interface is imported. In order to call member methods, you need an instance of the class.
In your case, you should create an object of type FileRead2 to call the importTeams() method on it.
// Since FileRead2 has some member variable, you should also think about
// initializing it appropriately if it is needed by importTeams method.
FileRead2 fileRead2Obj = new FileRead2();
fileRead2Obj.importTeams();
Similarly, to call static methods, you need to qualify the method name with the class name (though Java 5+ allows static import of methods as well).
If importTeams() was static method, then you should call it as follows, after importing the class:
FileRead2.importTeams();

Categories

Resources