I'm trying to make sure that all my threads of Class A have finished before notify is called. At the moment if one thread has finished his work he calls notify (the threads of Class B) while other A threads are still running. If that happens, B Threads start working which is changing the condition for the remaining A Threads.
I've tryed to use synchronisation blocks but I guess Im using them wrong.
The idea is that A fills the Array until its full and notifys B so it can empty the array again.
public class A extends Thread {
public static Object lockA = new Object();
private ArrayList list;
public A(ArrayList list) {
this.list = list;
}
public void run(){
while(true){
synchronized (A.lockA){
if(list.size() < 10){
list.add("A");
System.out.println(currentThread().getName() + " " + list);
}else{
synchronized (B.lockB){
B.lockB.notifyAll();
}
return;
}
}
}
}
}
public class B extends Thread {
public static Object lockB = new Object();
private ArrayList list;
public B(ArrayList list) {
this.list = list;
}
public void run(){
synchronized (B.lockB){
try {
B.lockB.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
while (list.size() > 0){
list.remove("A");
System.out.println(currentThread().getName() + " " + list);
}
return;
}
}
}
public class Main {
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
A a = new A(list);
A aa = new A(list);
A aaa = new A(list);
B b = new B(list);
B bb = new B(list);
B bbb = new B(list);
B bbbb = new B(list);
a.start();
aa.start();
aaa.start();
b.start();
bb.start();
bbb.start();
bbbb.start();
}
}
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)