1
votes

Lets say we have two Threads A, B and one Mutex (Shared resource) M.

I start the A thread (a.start()), and it will call a synchronized method in M and it causes A to wait(). How can the thread B (b) follow A after A enters wait() ?

thanks

EDIT:

Is there a method "similar" to join() in which the thread b will join the thread started a when it is in WAITING state? (As I found , join() will happen when the thread finishes, but i don't want that to happen

1
So thread A locks the mutex and calls a synchronized method? There's two mutual exclusion mechanisms or just one? Object.wait releases all synchronization resources on the object.blackcompe
+1 - A calls a "public synchronized" method in Mutex. From inside this method there's just "wait()" (not threadObj.wait() since the it will wait the current thread,which is A). You're free to put any code as long as there's wait()coder9
What does getState() have to do with it?user207421
@EJP I tried a getState() check below "new A().start()" but it doesn't execute that line.coder9
So it has nothing to do with the problem, it's just what you get to when the problem is solved. I suggest you amend your title to something that describes the actual topic. At present it isn't helping you get answers.user207421

1 Answers

1
votes

A object in thread "t" can call wait if in a synchronized block, and the jvm will manage the execution of synchronous blocks that are running... --- It's actually quite simple, objects that are waiting will grab the lock of that threads execution when an opposing object yields, and start working.... So, in short, your object B will start running if it:

1) is in the same thread as A

2) is "wait()"ing when A stops running

3) is executing in a synchronous block