Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple wait notify

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Main thread must wait, Until Amir completes for loop.

But this program stops!

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are some wait/notify construction problem in your code.

1. synchronized block is missing in your run method of thred. remember notify call should inside a synchronized block.

2. you need to synchronize and wait/notify on the same object.

thus I modified your Player class
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:thus I modified your Player class


Even with the change, there is a race condition. It is possible for the run() method to grab the lock first. If that happens, the run() method will send all the notifications before the main thread can grab the lock, and hence, call the wait() method. Which means that the main thread will wait forever.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even with the change, there is a race condition. It is possible for the run() method to grab the lock first. If that happens, the run() method will send all the notifications before the main thread can grab the lock, and hence, call the wait() method. Which means that the main thread will wait forever.


How to fix it?

you need to synchronize and wait/notify on the same object.


Lock and o are same object.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
How to fix it?

you can make some delay in run method before the synchronized block. but there is another alternative for your task, this way you can eliminate wait/notify

Alternative:

1. start the thread in your main method.
2.and then call the join method on started thread from main thread.


 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much indeed!

May you answer my question about the second pint you told:

you need to synchronize and wait/notify on the same object.

Lock and o are same object.What do you mean by same object?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:Lock and o are same object.What do you mean by same object?

i meant what you said here/above quote(Lock and o are same object; your example violate this)
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Lock and o are same object in this code.May show me how they are not please?Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:Lock and o are same object in this code.May show me how they are not please?Thanks

ok, cool. your code is ok in terms of wait/notify call on same object. but what about synchronized block ? i mean you did not call your notify with in a synchronized block also it is important where your calling your notify!
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output:

10 Run!
BUILD SUCCESSFUL

Please me give me any suggestion to improve this program
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:
Even with the change, there is a race condition. It is possible for the run() method to grab the lock first. If that happens, the run() method will send all the notifications before the main thread can grab the lock, and hence, call the wait() method. Which means that the main thread will wait forever.


How to fix it?


An excerpt from an old post...

Henry Wong wrote:
There are two rules that you should always adhere to when you wait()...

1. Never ever do a blind wait(). Always check the state to confirm that the thread should wait (with the synch lock, of course). If a notication had been sent eariler, then the state should be set too. And a wait isn't necessary.

2. Never assume that a return from a wait means that the state is correct. It is technical possible for another thread, using rule #1, to take the condition without waiting. The state can go to what you want, and back to what you don't want, by the time the thread wakes up, and hence, needs to go back to a wait state.


In other words, use a flag of some sort, put the wait() in a while loop, and protect the flag with the synchronization.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ok. I wrote another program.
This program woks fine.I don't see it needs putting wait in a loop.what is your idea?

Is it possible to write this program so only one player class be in this program? and creates tw0 object of this Player class and same result?



 
Campbell Ritchie
Sheriff
Pie
Posts: 49781
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for putting the wait() invocation in a loop is that there might be a precondition or dependency which must be fulfilled or available before the thread is woken. You use that dependency as the loop termination condition.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic