-
January 1st, 2010, 04:45 PM
#1
notifyAll
I wanted to write a program about notifyAll:
Code:
public class Main {
/** Creates a new instance of Main */
public static void main(String[] args) {
// TODO code application logic here
distanceshow d=new distanceshow();
Thread thread1 = new Thread(d);
Thread thread2 = new Thread(d);
Thread thread3 = new Thread(d);
thread1.start();
thread2.start();
thread3.start();
}
}
Code:
public class distanceshow implements Runnable {
/** Creates a new instance of distanceshow */
int i;
int d;
public static int distance=100;
public void run()
{
if(i<3){
i++;
System.out.println(" Runner number" +'\t' + i +'\t'+"is ready");
try{
wait ();
}
catch (InterruptedException e) { }
} else if(i==3){
notifyAll();
System.out.println(" START");
for ( i=100;i<=400;i+=100){
System.out.println("distance="+ i );
}
}
}
}
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!
-
January 2nd, 2010, 07:29 AM
#2
Re: notifyAll
When all else fails read the documentation.
Originally Posted by API Docs
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
By executing a synchronized instance method of that object.
By executing the body of a synchronized statement that synchronizes on the object.
For objects of type Class, by executing a synchronized static method of that class.
-
January 2nd, 2010, 09:00 AM
#3
Re: notifyAll
Code:
notifyAll();
System.out.println(" START");
for ( i=100;i<=400;i+=100){
System.out.println("distance="+ i );
}
This is the way that I want this program works:
After notifyAll(),I want to run this loop:
Code:
for ( i=100;i<=400;i+=100){
System.out.println("distance="+ i );
}
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|