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.
Oliver Wong - 13 Jul 2006 20:39 GMT
[...]
> public static void main(String[] args) {
>
[quoted text clipped - 7 lines]
> System.out.println("i = " + i);
> }
[...]
> java.lang.IllegalThreadStateException
Read the JavaDocs:
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#start()
- Oliver
Mohan Kakulavarapu - 13 Jul 2006 22:05 GMT
You are instantiating the thread objects only once and invoking the
start method on them
repeatedly in a loop. Once a thread is started same thread cannot be
started again
move the instantiation into the loop
for(int i=0; i<10; i++) {
th1 = new Thread();
th2 = new Thread();
th1.start();
th2.start();
}
Mohan Kakulavarapu
Freelance Java Developer
> i have two different threads that needs to be synchronized.
> what needs to be accomplished is:
[quoted text clipped - 100 lines]
>
> thanks.
crash.test.dummy - 13 Jul 2006 22:20 GMT
thanks oliver and mohan.
> You are instantiating the thread objects only once and invoking the
> start method on them
[quoted text clipped - 117 lines]
> >
> > thanks.
blmblm@myrealbox.com - 13 Jul 2006 22:43 GMT
>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:
This seems unnecesssarily complicated if what you want is to make
sure that at any given time only one thread is executing either
method1 or method2. The easier alternative that occurs to me (and
there may be others) is to create a single lock object and have all
the threads use it for synchronization; this guarantees that only
one thread at a time (either a Thread1 or a Thread2) can execute
the code. Here's my modified version of your code illustrating
what I have in mind (and adding an "id" variable for each thread
so that you can tell the instances of Thread1 apart -- probably not
needed in the real code but might be helpful here):
// (most) changed code flagged with "added" or "modified":
public class ThreadTest {
public static long interval;
public static boolean thread = false;
public static void main(String[] args) {
System.out.println("starting");
// added
Object lockObj = new Object();
// modified
for (int i = 0; i < 10 ; i++) {
Thread1 th1 = new Thread1(lockObj, i);
Thread2 th2 = new Thread2(lockObj, i);
th1.start();
th2.start();
System.out.println("i = " + i);
}
System.out.println("done.");
}
static class Thread1 extends Thread {
// added
private Object lockObj;
private int id;
public Thread1(Object lockObj, int id) {
this.lockObj = lockObj;
this.id = id;
}
public void run() {
method1();
}
private void method1() {
// modified
System.out.println("Thread1 (" + id + ") waiting...");
synchronized(lockObj) {
System.out.println("Thread1 (" + id + ") running...");
try {
// sleep 5 seconds
sleep(5000);
} catch (InterruptedException e) {}
System.out.println("Thread1 (" + id + ") done.");
}
try {
// sleep 2 seconds
sleep(2000);
} catch (InterruptedException e) {}
}
}
static class Thread2 extends Thread {
// added
private Object lockObj;
private int id;
public Thread2(Object lockObj, int id) {
this.lockObj = lockObj;
this.id = id;
}
public void run() {
method2();
}
private synchronized void method2() {
// modified
System.out.println("Thread2 (" + id + ") waiting...");
synchronized (lockObj) {
System.out.println("Thread2 (" + id + ") running...");
try {
// sleep 5 seconds
sleep(5000);
} catch (InterruptedException e) {}
System.out.println("Thread2 (" + id + ") done.");
}
try {
// sleep 2 seconds
sleep(2000);
} catch (InterruptedException e) {}
}
}
}
>public class ThreadTest {
>
[quoted text clipped - 94 lines]
>
>thanks.

Signature
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Timo Stamm - 14 Jul 2006 01:41 GMT
crash.test.dummy schrieb:
> what needs to be accomplished is:
> Thread1 cannot run while Thread2 is running.
> Thread2 cannot run while Thread1 is running.
Why use Threads, then?
for (;;) {
task1();
task2();
}