File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Simple wait notify Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple wait notify" Watch "Simple wait notify" New topic
Author

Simple wait notify

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


Main thread must wait, Until Amir completes for loop.

But this program stops!

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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
Sheriff

Joined: Sep 28, 2004
Posts: 18752
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 06, 2007
Posts: 635
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 06, 2007
Posts: 635


Lock and o are same object in this code.May show me how they are not please?Thanks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Sep 06, 2007
Posts: 635


Output:

10 Run!
BUILD SUCCESSFUL

Please me give me any suggestion to improve this program
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18752
    
  40

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

Joined: Sep 06, 2007
Posts: 635


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

Joined: Oct 13, 2005
Posts: 38436
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple wait notify