• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Wait()

 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The following code produces the output as below when the line BBBB is commented and the line AAAA is uncommented:






[output]

52 Thread-0
A great game is here:::::Thread-0
[/output]


But when the line AAAA is commenetd and the BBBB line is uncommented,the output changes to:

[output]
52 Thread-0
A great game is here:::::Thread-0
A great game is here:::::main
[/output]

Please let me know that how does wait() work when the wait() is invoked from inside synchronized method and when it is executed from outside the synchronized method.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a bunch of problems with your code. The first thing is that you eat the general exception and don't do anything with if:

This hides an exception you get when you try to call the b.wait() method. When you report the exception:

The output when AAAA is uncommented is:

The IllegalMonitorStateException occurs because you are trying to wait() on an object whose lock you don't have (you have to synchronize on b before calling b.wait()).

Please read: The API for the wait() method and the Concurrency tutorial for more info on how to properly use synchronization techniques.

You may also want to read this Thread on the CodeRanch as it will play a part in the execution of your code as soon as you work out the exceptions you have.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!