| Author |
notifyAll
|
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
I wanted to write a program about notifyAll:
The goal of this program is :
we have three runners , when they are ready for run ,
We must print :
Runner number 1 is ready
Runner number 2 is ready
Runner number 3 is ready
START
after that, I want this program shows something like this:
Runner number 1 distance is 100
Runner number 3 distance is 200
Runner number 2 distance is 300
Runner number 4 distance is 400
Runner number 4 wins!
But, I have problmes :
1-When I compile this , These errors shows:
compile:
run:
Runner number 2 is ready
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
Runner number 1 is ready
at java.lang.Object.wait(Native Method)
Runner number 3 is ready
at java.lang.Object.wait(Object.java:485)
at notifyalltest.distanceshow.run(distanceshow.java:29)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at notifyalltest.distanceshow.run(distanceshow.java:29)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at notifyalltest.distanceshow.run(distanceshow.java:29)
at java.lang.Thread.run(Thread.java:619)
BUILD SUCCESSFUL (total time: 0 seconds)
why?
2-
in order to print this part, Which way do you suggest?
Runner number 1 distance is 100
Runner number 3 distance is 200
Runner number 2 distance is 300
Runner number 4 distance is 400
Runner number 4 wins!
|
 |
Anurag Blore
Ranch Hand
Joined: Jan 15, 2003
Posts: 74
|
|
Please add
synchronized
on the run method to resolve the exception.
I guess in Thread their is no guaranty in what order threads will execute a piece of code.
Thanks,
Anurag
Happy New Year 2010.
|
Thanks,
Anurag
SCJP 1.2 & 1.5, PMP
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
I changed the program:
This the result:
run:
Runner number 1 is ready
Runner number 2 is ready
Runner number 3 is ready
START
runner num 2distance=100
runner num 2distance=200
runner num 2distance=300
runner num 2distance=400
BUILD SUCCESSFUL (total time: 0 seconds)
My question is :
When I run this program ,Always "runner num 2" and "runner num 3" show.
But I don't see runner num 1 at all
Why?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Anurag Blore wrote:Please add
synchronized
on the run method to resolve the exception.
This answer solves the symptom. It doesn't explain why.
The reason "synchronize" is needed is related to how mutexes and condition variables interact. It may be a good idea to start with the Sun tutorial on threading first.
http://java.sun.com/docs/books/tutorial/essential/concurrency/
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
abalfazl hossein wrote:
When I run this program ,Always "runner num 2" and "runner num 3" show.
But I don't see runner num 1 at all
Why?
Having a race condition means that the output order can vary -- can be different from run to run. It does not mean that the output order will actually be different from run to run. It does not mean that the output is random. etc.
Henry
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
This is the way that I want this program works:
After notifyAll(),I want to run this loop:
In other words , I want print out that every hundred meters, Which runners is in front.
The idea behind this is:
Because we are dealing with threads , we can'nt predict which thread run this loop,In other words , after print a line like this:
Runner number 1 distance is 100
Runner number 3distance is 100
Runner number 2 distance is 100
I want for example another thread run this loop and print this:
Runner number 3 distance is 200
Runner number 2 distance is 200
Runner number 1 distance is 200
until the 400,The last thread that run this is winner:
for example :
Runner number 2 wins!
I do'nnt know how can I do this in JAVA,Please guide me how to write code for that?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
Your program doesn't do what you described... Basically...
The first thread that runs will just call wait(). When it does get a notification, it will wake up, and do nothing else. DONE.
The other two threads will just notify the waiting threads to wake up. And then prints the output in a loop. Also, since it is holding the lock, no other thread will run while it is doing it.
So... First thread does no output. Second thread does all of its output. Then the last thread does all of its output.
Henry
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
The first thread that runs will just call wait(). When it does get a notification, it will wake up, and do nothing else. DONE.
With all due respect Sir, If you run this program for 100 times , 1 times you see this:
run:
Runner number 1 is ready
Runner number 2 is ready
Runner number 3 is ready
START
runner num 1distance=100
runner num 1distance=200
runner num 1distance=300
runner num 1distance=400
Of course , most of the result is runner num 2distance=200
why?
The code:
About my question in last post to print :
Runner number 1 distance is 100
Runner number 3distance is 100
Runner number 2 distance is 100
Or :
Runner number 3 distance is 200
Runner number 2 distance is 200
Runner number 1 distance is 200
until the 400,The last thread that run this is winner:
for example :
Runner number 2 wins!
May you guide me how can I code for that?Could you write code that can do that?
Thanks in advance
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
abalfazl hossein wrote:
With all due respect Sir, If you run this program for 100 times , 1 times you see this:
When I say "first thread", I don't mean thread number one of your program. I mean the first thread that gets to do the wait. See my previous response about race conditions.
Henry
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
abalfazl hossein wrote:
May you guide me how can I code for that?Could you write code that can do that?
If you start with the tutorial, and take a shot at it. We can guide you from there.
At this point, you need to get your application to wait AND print. And not wait and exit. You also need to get your program to wait with each iteration of the loop, and not just print everything. There are also issues about threads not keeping track of states (but we can worry about that later).
Henry
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
ok,
At this time the third thread has started
START is printed
Now notifyAll()
Other two threads chage the situation from wait.
because this method is synchronized , only one thread at same time can access to that.(run() )
if I write this code
The program lock and stops
runner num 2distance=100
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
abalfazl hossein wrote:
Now notifyAll()
Other two threads chage the situation from wait.
And after they do wake up, they will just finish the run() method and return -- they don't do anything else.
abalfazl hossein wrote:
The program lock and stops
runner num 2distance=100
Well... think about it. The other two threads are now gone. So, there is now nothing to wake this thread up. You need to get all three threads to coordinate wait() *AND* notify() each other too.
Henry
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
same result again
run:
Runner number 1 is ready
Runner number 2 is ready
Runner number 3 is ready
START
runner num 2distance=100
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
abalfazl hossein wrote:
same result again
Uhhhhh..... it may be a good idea to start with the tutorial mentioned in my first reply.
The suggestion "get all three threads to coordinate wait() *AND* notify() each other" doesn't mean "call notifyAll() after catching an interrupted exception".
Henry
|
 |
abalfazl hossein
Ranch Hand
Joined: Sep 06, 2007
Posts: 602
|
|
give me an example of what you think
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16687
|
|
abalfazl hossein wrote:give me an example of what you think
Maybe it would be a good idea to get the basic concepts down first. As mentioned, the Sun tutorial is a good place to start.
Henry
|
 |
 |
|
|
subject: notifyAll
|
|
|