java - Why does notifyAll() not resume the other threads in this simple case? -



java - Why does notifyAll() not resume the other threads in this simple case? -

i'm new java concurrency. have simple object 3 methods, each corresponding code run 3 different threads. why notifyall() statement in case not release wait in other 2 threads?

public class main { static class obj { synchronized void t1 () { system.out.println("t1 ran"); seek { thread.sleep(1000); } grab (interruptedexception e) { e.printstacktrace(); } notifyall(); } synchronized void t2 () { seek { wait(); } grab (interruptedexception e) { e.printstacktrace(); } system.out.println("t2 ran"); } synchronized void t3() { seek { wait(); } grab (interruptedexception e) { e.printstacktrace(); } system.out.println("t3 ran"); } } public static void main(string[] args) { final obj o = new obj(); new thread(new runnable() { @override public void run() { o.t1(); } }).start(); new thread(new runnable() { @override public void run() { o.t2(); } }).start(); new thread(new runnable() { @override public void run() { o.t3(); } }).start(); }}

i expected: t1 ran ~~pause 1 second~~ t2 ran t3 ran

i got: t1 ran

thread.sleep not release or relax locks in way wait does, synchronization still in total effect , other threads won't allowed come in methods during sleep.

if replace the

thread.sleep(1000);

with

wait(1000);

the other threads allowed capture same lock, come in methods, start waiting, , sample work expected.

java multithreading concurrency

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -