in Education by
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)

1 Answer

0 votes
by
Unless lockB protects some state that a thread might want to wait for changes to or might need to notify another thread about changes to, using it in association with wait and notify doesn't make any sense. synchronized (B.lockB){ try { B.lockB.wait(); You must never, ever call wait until you have checked to be sure that the thing you are waiting for has not already happened. The reason you have this code inside a synchronized block is precisely so that you can perform that check and wait if, and only if, you need to wait. synchronized (B.lockB){ B.lockB.notifyAll(); } This is pretty baffling. Why are you calling lockB.notifyAll() if you haven't changed anything protected by that lock that you need to notify other threads about? It looks like you don't really understand how the wait/notify semantics actually work. The point is that you only need to wait if something hasn't happened yet and you can never be sure whether or not it's already happened (because schedulers are unpredictable) without holding some lock. But you need to release that lock before you wait, creating a race condition if some other thread does the thing you're waiting for after you release the lock but before you start waiting. The wait function is an atomic function to release a lock and wait for something to happen. The atomicity avoids this race condition. However, you don't have the lock that's released protecting the thing you're waiting for. This destroys the logic of the atomic unlock and wait function and results in code that can always deadlock.

Related questions

0 votes
    We have a service method GetDataParallel( ) which maybe called by many clients currently, and we use the ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked May 27, 2022 in Education by JackTerrance
0 votes
    In my Java application I want to have a pool of threads (I use ExecutorService for this) that connect ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Which of this method can be used to make the main thread to be executed last among all the threads? ... portion Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    I have a @Stateless-Bean which performs some Database operations in a single method public void doOperation() ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I have a @Stateless-Bean which performs some Database operations in a single method public void doOperation() ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    Can we make changes on the repo and how we can make sure that the same has been merged into the working copy?...
asked Feb 18, 2021 in Technology by JackTerrance
0 votes
    Before a block of data in main memory can be output to the database, all log records pertaining to ... Buffer Management topic in chapter Recovery System of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    very quick newbish question here... In the jquery-mobile docs, it says that Note that the refresh() ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Materialised views make sure that (a) View definition is kept stable (b) View definition is kept up-to ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    State true or false. When you have finished your work in the IMG or ABAP Workbench, or have reached a certain stage, you can release the request. A. True B. False...
asked Feb 20, 2023 in Technology by JackTerrance
0 votes
    As you sense your device has been infected with spyware, you should run a scan with your existing security ... for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
...