wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Order of execution for wait and notify/notifyall Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Order of execution for wait and notify/notifyall" Watch "Order of execution for wait and notify/notifyall" New topic

Order of execution for wait and notify/notifyall

Micheal John
Ranch Hand

Joined: Nov 01, 2006
Posts: 344
From web page

In the above pgm (for all my pgm inmy JVM), first everything in the main thread is executing and then only new thread is executed and I am also aware of Thread Scheduler is not guaranteed..

What my question is in the main thread itself notifyall/notify will be executed. without wait is executed in the new thread, which causes the pgm not to terminate..right? In the above pgm, notifyall() will be executed first, then wait() is executing, but nothing to notify() the wait(), since notifyall() is executed already..and as i expected this causes pgm not to be terminated, but it prints some number..how it is possible?can anyone explain this?

Micheal John
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
Ranch Hand

Joined: Nov 10, 2006
Posts: 127
i'm afraid of a compilation error.

inside the for loop, can you declare A t1 = new A(this)? that means t1 gets repeatly declared following the loop progress.

when move the declaration out of the for loop, i except the call flow as following,

main thread: new B().m1();
main thread: t1.start();
t1 threads 0-9: this.wait();
main thread: System.out.println("1");
main thread: this.notifyAll();
t1 threads 0-9: System.out.print(Thread.currentThread().getName());

Hard work rewards
I agree. Here's the link: http://aspose.com/file-tools
subject: Order of execution for wait and notify/notifyall
It's not a secret anymore!