synchronizing 2 threads

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.
 
M

Mohan Kakulavarapu

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
 
C

crash.test.dummy

thanks oliver and mohan.


Mohan said:
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

crash.test.dummy said:
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.
 
B

blmblm

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 {

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.
 
T

Timo Stamm

crash.test.dummy said:
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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top