I have Sum class, Creator class, Item class and Main. The Creator creates random Items and adds them to an ArrayList which is in the Item class. The Sum class reads items and sums the weight of all. In the Main class I start in multiple threads Creator and Sum. Both classes implement Runnable and override the run method. After 200 created are items into is printed in the console.
How to synchronize this methods? When I start threads, the method from Sum ends first and returns weight 0 and after that Creator creating 40 000 random Items. I will create items and at the same time Sum all weights of them and in the end Return how many items were created and weight of all of them.
Sum class method:
@Override
public synchronized void run() {
for(Towar x: Towar.list){
try {
Thread.currentThread().wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
counter++;
sum+=x.getWaga();
if(counter%100==0){
System.out.println("Sum of "+counter+" items");
}
}
System.out.println("Total weight of Items: "+sum);
}
Creator class method:
@Override
public void run() {
reader=new Scanner(text);
while(reader.hasNextLine()){
counter++;
String[] x=reader.nextLine().split("_");
synchronized (Towar.getList()){
Towar.add(new Towar(x[0], Integer.parseInt(x[1])));
Towar.list.notify();
if(counter%200==0){
System.out.println("Created "+counter+" items");
}
}
}
System.out.println("Created in total: "+counter+" items");
}