C
crash.test.dummy
i have two different threads that needs to be synchronized.
what needs to be accomplished is:
Thread1 cannot run while Thread2 is running.
Thread2 cannot run while Thread1 is running.
sample code:
public class ThreadTest {
public static long interval;
public static boolean thread = false;
public static void main(String[] args) {
System.out.println("starting");
Thread1 th1 = new Thread1();
Thread2 th2 = new Thread2();
for (int i = 0; i < 10 ; i++) {
th1.start();
th2.start();
System.out.println("i = " + i);
}
System.out.println("done.");
}
static class Thread1 extends Thread {
public void run() {
method1();
}
private synchronized void method1() {
while (thread) {
try {
System.out.println("Thread1 waiting...");
wait();
} catch (InterruptedException e) {}
}
System.out.println("Thread1 running...");
try {
// sleep 5 seconds
sleep(5000);
} catch (InterruptedException e) {}
thread = true;
System.out.println("Thread1 done.");
notifyAll();
try {
// sleep 2 seconds
sleep(2000);
} catch (InterruptedException e) {}
}
}
static class Thread2 extends Thread {
public void run() {
method2();
}
private synchronized void method2() {
while (!thread) {
try {
System.out.println("Thread2 waiting...");
wait();
} catch (InterruptedException e) {}
}
System.out.println("Thread2 running...");
try {
// sleep 5 seconds
sleep(5000);
} catch (InterruptedException e) {}
thread = false;
System.out.println("Thread2 done.");
notifyAll();
try {
// sleep 2 seconds
sleep(2000);
} catch (InterruptedException e) {}
}
}
}
Here is the output:
starting
Thread1 running...
i = 0
Thread2 waiting...
Thread1 done.
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at samples.thread.ThreadTest.main(ThreadTest.java:14)
after calling notifyAll() in Thread1.method1(), why didn't Thread2
start?
Line13: for (int i = 0; i < 10 ; i++) {
Line14: th1.start();
Line15: th2.start();
thanks.
what needs to be accomplished is:
Thread1 cannot run while Thread2 is running.
Thread2 cannot run while Thread1 is running.
sample code:
public class ThreadTest {
public static long interval;
public static boolean thread = false;
public static void main(String[] args) {
System.out.println("starting");
Thread1 th1 = new Thread1();
Thread2 th2 = new Thread2();
for (int i = 0; i < 10 ; i++) {
th1.start();
th2.start();
System.out.println("i = " + i);
}
System.out.println("done.");
}
static class Thread1 extends Thread {
public void run() {
method1();
}
private synchronized void method1() {
while (thread) {
try {
System.out.println("Thread1 waiting...");
wait();
} catch (InterruptedException e) {}
}
System.out.println("Thread1 running...");
try {
// sleep 5 seconds
sleep(5000);
} catch (InterruptedException e) {}
thread = true;
System.out.println("Thread1 done.");
notifyAll();
try {
// sleep 2 seconds
sleep(2000);
} catch (InterruptedException e) {}
}
}
static class Thread2 extends Thread {
public void run() {
method2();
}
private synchronized void method2() {
while (!thread) {
try {
System.out.println("Thread2 waiting...");
wait();
} catch (InterruptedException e) {}
}
System.out.println("Thread2 running...");
try {
// sleep 5 seconds
sleep(5000);
} catch (InterruptedException e) {}
thread = false;
System.out.println("Thread2 done.");
notifyAll();
try {
// sleep 2 seconds
sleep(2000);
} catch (InterruptedException e) {}
}
}
}
Here is the output:
starting
Thread1 running...
i = 0
Thread2 waiting...
Thread1 done.
java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at samples.thread.ThreadTest.main(ThreadTest.java:14)
after calling notifyAll() in Thread1.method1(), why didn't Thread2
start?
Line13: for (int i = 0; i < 10 ; i++) {
Line14: th1.start();
Line15: th2.start();
thanks.