• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of execution for wait and notify/notifyall

 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic