• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait/notify, A program that works forever  RSS feed

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


output:
run:
10 The thread that is running is Amir


But thread hossein does not show anything and this program does not stop, Why?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:

output:
run:
10 The thread that is running is Amir


But thread hossein does not show anything and this program does not stop, Why?



There is no queuing with notifications -- notify() will wake one waiting thread. And if a the time of the notify, there are no threads waiting, then the notification is tossed.

It is not a good idea to do "blind" notifications. You should have some sort of state variable somewhere. Otherwise, you get into this situation where Hossein calls wait() even though Amir has already called notify.

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


This program work well.
These is question. At this line, run method,runs:



How can I be sure that notify(); would not be crossed before call this line:

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:

This program work well.


"works well" doesn't mean that it will always work. Run this example on an JVM/OS pair that places high priority on starting a thread, such as old versions of MacOS, and this will break. Just because something works, doesn't mean it works everywhere, and it also doesn't mean that it will continue to work in the future.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sir, But you didn't answer my question.
 
Henry Wong
author
Sheriff
Posts: 23283
125
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:
These is question. At this line, run method,runs:



How can I be sure that notify(); would not be crossed before call this line:



As already mentioned, use some sort of state variable, which you can check prior to the wait() call.

After all, why do you think the wait() and notify() methods require that you own the lock? It is not to annoy the programmer, forcing him/her to wrap the code in sync blocks -- like you did. Your wait and notify calls are protected by synchronization blocks -- now use those blocks to protect the variables that track the state.

abalfazl hossein wrote:Thanks Sir, But you didn't answer my question.


Yea... I kinda can only type so fast...

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now use those blocks to protect the variables that track the state.


I've read tutorials

http://www.avajava.com/tutorials/lessons/how-do-i-use-the-wait-and-notify-methods.html

But they didn't use state variable. May you show me how to use by an example?

Thanks
 
Henry Wong
author
Sheriff
Posts: 23283
125
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:
now use those blocks to protect the variables that track the state.


I've read tutorials

http://www.avajava.com/tutorials/lessons/how-do-i-use-the-wait-and-notify-methods.html

But they didn't use state variable. May you show me how to use by an example?


This is *not* a wait and notify specific thing -- by state variable, I mean an actual variable that the two threads use to maintain state.

Do you know how to create a variable? And properly synchronize / share that variable between threads? You need to understand that first.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
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:
I've read tutorials

http://www.avajava.com/tutorials/lessons/how-do-i-use-the-wait-and-notify-methods.html

But they didn't use state variable. May you show me how to use by an example?


In reading that link that you provided, I have to say that it isn't very good. It is teaching you the API only without teaching you how to properly use the API.

There are certain practices that you should always follow with condition variables (aka the wait/notify mechanism)... First, you should never do a "blind" wait -- always confirm that you need to wait before you actually call wait(). Second, you should never assume that the state is correct upon return from wait -- always confirm that you are able to continue upon return.


Anyway, I guess I can quickly modify your example, so that it will work regardless of which thread gets to wait/notify point first...


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate a bit on what I think Henry is getting at...

When you have multiple threads, you can't guarantee when one thread or another will get CPU cycles. Once you call start(), that new thread may execute to completion before your current thread or any other thread gets any CPU time. Or it may get no CPU cycles at all until all the other threads are done. Or it may get some bits and pieces of CPU, taking turns with th other threads. Or all the threads may get cycles simultaneously, if we have a multi-CPU or multi-core machine.

Therefore, we can't guarantee that one thread will call wait() before another calls notifyAll().

So in addition to just wait/notify, we use some other mechanism--perhaps a simple state variable--to tell the threads where we are in our overall processing. Such as, in a rough, simple case, "Did the other thread call wait() yet?"
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are certain practices that you should always follow with condition variables (aka the wait/notify mechanism)... First, you should never do a "blind" wait -- always confirm that you need to wait before you actually call wait(). Second, you should never assume that the state is correct upon return from wait -- always confirm that you are able to continue upon return.


Two sentences make me think, always confirm that you are able to continue upon return, always confirm that you are able to continue upon return.

Sir, May you give me simple example about these sentences? I appreciate Sir.
 
Henry Wong
author
Sheriff
Posts: 23283
125
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:
Two sentences make me think, always confirm that you are able to continue upon return, always confirm that you are able to continue upon return.

Sir, May you give me simple example about these sentences? I appreciate Sir.



I already did -- in my last example. Wouldn't it be kinda silly for me to not follow the "practices" that I just preached in the very same post? ...

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


I add a flag, Because I want be sure that for loop finished its job then hossein thread finishes its job.I used a static member in order this variable could accessed in other class easily,Just by the name of class. Other than that I had to use getter/setter.I wanted Player2 class knows what happened in Player1.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I also change the code of another example:

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestion for my code?
 
Henry Wong
author
Sheriff
Posts: 23283
125
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:

I also change the code of another example:




There is nothing in this example that implements what was discussed in this topic. This example have the same issue that the original example has.

Interestingly, this example has another issue. It is using the thread object as the notification object, so it will get an extraneous notification (due to the fact that the core library also use the same object). So, arguably, this example is worst than the original. On the other hand, this is a case of two wrongs does make a right -- the extraneous notification may actually help make the first issue go away....

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
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:

I add a flag, Because I want be sure that for loop finished its job then hossein thread finishes its job.I used a static member in order this variable could accessed in other class easily,Just by the name of class. Other than that I had to use getter/setter.I wanted Player2 class knows what happened in Player1.



The flag that you added is both redundant and broken.

It is redundant because the notification is always sent after the loop. This means that there is no case where the stateNotify flag is set, but the other flag is not. If you wanted to be sure that the loop finished, the stateNotify flag already does that.

And of course, your new flag is broken, as it is not thread safe. The only reason it works is because the flag is redundant -- kinda protected by the statenotify flag.

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

I also change the code of another example:




There is nothing in this example that implements what was discussed in this topic. This example have the same issue that the original example has.

Interestingly, this example has another issue. It is using the thread object as the notification object, so it will get an extraneous notification (due to the fact that the core library also use the same object). So, arguably, this example is worst than the original. On the other hand, this is a case of two wrongs does make a right -- the extraneous notification may actually help make the first issue go away....

Henry


Then Please guide me what is necessary to change this code?
 
Henry Wong
author
Sheriff
Posts: 23283
125
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:Then Please guide me what is necessary to change this code?


Considering that I already did that -- for your previous example. And you have a working example (before and after) to learn from. Perhaps it is better to review, and try to understand how it works, than to ask for yet another example of the exact same thing?

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!