This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes notifyAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "notifyAll" Watch "notifyAll" New topic
Author

notifyAll

abalfazl hossein
Ranch Hand

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

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: 18509
    
  40

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: 635


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: 18509
    
  40

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: 635

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: 18509
    
  40

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: 18509
    
  40

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: 635


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: 18509
    
  40

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: 635


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: 18509
    
  40

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: 635
give me an example of what you think

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: notifyAll
 
Similar Threads
Getting tasks to run in a separate thread
Thread java.lang.IllegalMonitorStateException: Iam new to threads
Re:Help on stuck thread
JBoss deployment issues
Thread Madness - please help!