wait() and notify() related issue - java

I simply want to use thread to print out from 1 to 10. But my code will stop at number 1. input() will provide variable from 1 to 10, while output() will print out them. input() will be executed first and then output(). After that for() will make sure they will start another iteration.
class InputOutput{
private static int i=0;
private static boolean ToF=false;
synchronized void output(){
try{
while(!ToF){
notify();
wait();
}
}
catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("Output: "+i);
ToF=false;
notify();
}
synchronized void input(){
try{
while(ToF){
notify();
wait();
}
}
catch(InterruptedException e){
e.printStackTrace();
}
i++;
ToF=true;
notify();
}
class input implements Runnable{
private int i=1;
InputOutput io=new InputOutput();
public void run(){
for(i=1;i<=10;i++)
io.input();
}
}
class output implements Runnable{
private int i=1;
InputOutput io=new InputOutput();
public void run(){
for(i=1;i<=10;i++)
io.output();
}
}
public class Homework07Part3 {
public static void main(String[] args) {
Thread t1=new Thread(new input());
t1.start();
Thread t2=new Thread(new output());
t2.start();
}
}

while loop you put wait on a single object for which two thread communication
while(ToF){
//dont put notify here.
notify();
wait();
}
Make it instance variable
private static boolean ToF=false;
public class Homework07Part3 {
public static void main(String[] args) {
InputOutput io = new InputOutput();
Thread t1 = new Thread(new input(io));
t1.start();
Thread t2 = new Thread(new output(io));
t2.start();
}
private static class input implements Runnable {
private int i = 1;
private InputOutput io;
public input(InputOutput io) {
this.io = io;
}
public void run() {
for (i = 1; i <= 10; i++)
io.input();
}
}
private static class output implements Runnable {
private int i = 1;
private InputOutput io;
public output(InputOutput io) {
this.io = io;
}
public void run() {
for (i = 1; i <= 10; i++)
io.output();
}
}
}
class InputOutput {
private int i = 0;
private boolean ToF = false;
synchronized void output() {
try {
while (!ToF) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Output: " + i);
ToF = false;
notify();
}
synchronized void input() {
try {
while (ToF) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
ToF = true;
notify();
}
}

I simply want to use thread to print out from 1 to 10. But my code will stop at number 1.
[[ The other answer seems to have fixed your problem but it doesn't explain what is happening and why the fix works. ]]
You problem is that both threads are calling synchronize and notify() and wait() on different objects. When threads communicate using these signals they both need to be sharing the same object instance. You are creating 2 InputOutput objects so both of your threads are stuck in wait() since the notify() calls are lost.
class Input implements Runnable{
...
// this is local to the Input class
InputOutput io=new InputOutput();
...
class Output implements Runnable{
...
// this is a different instance
InputOutput io=new InputOutput();
You should do something like the following:
final InputOutput io = new InputOutput();
Thread t1=new Thread(new Input(io));
t1.start();
Thread t2=new Thread(new Output());
t2.start();
...
private static class Input {
private final InputOutput io;
public Input(InputOutput io) { this.io = io; }
...
private static class Output {
private final InputOutput io;
public Output(InputOutput io) { this.io = io; }
...
So then both of your Input and Output classes are using the same instance of the InputOutput class. When they call synchronized on the methods, they are locking on the same instance and when they call wait() and notify() the signals are seen by the other thread.

Related

Issue with multi threading , as producer consumer are executed only once despite wait and notify usage

I have written the following program for inter thread communication ,, which is simply supposed to produce and consume one by one and program should keep running and printing until stopped externally .
package multithreading;
public class WaitNotifyExample
{
private final int asd;
public WaitNotifyExample(int asd)
{
this.asd = asd;
}
public static void main(String[] args)
{
CounterWaitNotifyExample counter = new CounterWaitNotifyExample(0);
Thread t1 = new Thread(new ConsumerWaitNotifyExample(counter));
Thread t2 = new Thread(new ProducerWaitNotifyExample(counter));
t2.start();
t1.start();
}
}
class ConsumerWaitNotifyExample implements Runnable
{
CounterWaitNotifyExample counter;
public ConsumerWaitNotifyExample(CounterWaitNotifyExample counter)
{
this.counter = counter;
}
#Override
public void run()
{
while (true)
{
counter.consume();
}
}
}
class ProducerWaitNotifyExample implements Runnable
{
CounterWaitNotifyExample counter;
public ProducerWaitNotifyExample(CounterWaitNotifyExample counter)
{
this.counter = counter;
}
#Override
public void run()
{
counter.produce();
}
}
class CounterWaitNotifyExample
{
private int counter;
private boolean produced =false;
public CounterWaitNotifyExample(int counter)
{
this.setCounter(counter);
}
public synchronized void consume()
{
if(!produced)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("consumed "+--counter);
produced = false;
notifyAll();
}
public synchronized void produce()
{
if(produced)
{
try
{
wait();
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println("produced "+(++counter));
produced = true;
notifyAll();
}
public int getCounter()
{
return counter;
}
public void setCounter(int counter)
{
this.counter = counter;
}
}
but i only get the following output , as application is still running but nothing is printing , meaning , producer and consumer are not executed any further.
produced 1
consumed 0
what Am I doing wrong here conceptually ?
Your producer doesn't have any loop. Only your consumer has.
Also, read the javadoc of wait(). It must always be called inside a loop checking for a condition.

Inter thread Communication-Java

I am new to java thread. I am unable to give the lock back to the thread from the main thread in the following code. I am getting the undesired output because i am unable to unlock the thread. I want thread to increment the value using thread (goes to wait state after that) and after printing the value, release the lock to print the next incremented value.
class Foo implements Runnable
{
public volatile int value=0,i=0;
Thread t=new Thread();
public void method(Thread t)
{
this.t = t;
}
#Override
public synchronized void run()
{
while(i<3)
{
value++;//receive and process ACK
i++;
try
{
System.out.println("im thread here");
wait();
System.out.println("passed wait");
}
catch(InterruptedException ex){
}
System.out.println("im notified");
}//while
//}//sync
}//run method
public int getValue()
{
try
{
Thread.sleep(1000);
}
catch (Exception e) {
System.out.println(e);
}
return value;
}
}//class foo
public class ThreadTest
{
public static int value1,times=0;
public static void main(String[] args)
{
Foo foo=new Foo();
Thread t=new Thread(foo);
foo.method(t);
t.start();
while(times<3)
{
synchronized(t)
{
value1=foo.getValue();
times++;
System.out.println(value1);
System.out.println(t.getState());
try
{
t.notify();
System.out.println("Notify is reached");
}
catch(IllegalMonitorStateException ex)
{
System.out.println("Thread is blocked");
}
}//sync
}//while
}//main
}//mclasss
Are you trying to do something like this? If you really must use wait/notify & want to use Runnable.
I added a wait block, otherwise the main thread may finish before the background thread increments the value.
class Foo implements Runnable {
public volatile int value = 0, i = 0;
private Thread backgroundThread;
public void setThread(Thread thread) {
this.backgroundThread = thread;
}
#Override
public void run() {
synchronized (backgroundThread) {
while (i < 2) {
value++;
i++;
backgroundThread.notify();
try {
System.out.println("background thread wait start");
backgroundThread.wait();
System.out.println("background thread notified");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
public int getValue() {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}
public class ThreadTest {
public static int value1, times = 0;
public static void main(String[] args) {
Foo foo = new Foo();
final Thread thread = new Thread(foo);
foo.setThread(thread);
thread.start();
while (times < 3) {
synchronized (thread) {
value1 = foo.getValue();
times++;
System.out.println(value1);
System.out.println(thread.getState());
thread.notify();
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Or you can extend thread:
class BackgroundThread extends Thread {
public volatile int value = 0, i = 0;
#Override
public synchronized void run() {
while (i < 2) {
value++;
i++;
notify();
try {
System.out.println("background thread wait start");
wait();
System.out.println("background thread notified");
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
public int getValue() {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}
public class ThreadTest {
public static int value1, times = 0;
public static void main(String[] args) {
BackgroundThread backgroundThread = new BackgroundThread();
backgroundThread.start();
while (times < 3) {
synchronized (backgroundThread) {
value1 = backgroundThread.getValue();
times++;
System.out.println(value1);
System.out.println(backgroundThread.getState());
backgroundThread.notify();
try {
backgroundThread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
It is very unclear what you really want to do but we will assume here that you want to run a thread in the background which should run only when its spawner (let's say the main thread) allows it to.
The JDK has several tools for this already, no need to rely on the low level wait() and notify{,All}() methods.
One example of such a primitive is a CountDownLatch. It is a one-use entity which allows you to specify the times a given set of threads should countDown() it before any threads .await()ing for them can trigger.
In combination with the multithread handling classes which appeared as far back as Java 1.5, this means you could do something like this:
// Implementation of a Runnable waiting for the counter to trigger
public final class MyWaitingClass
implements Runnable
{
private final CountDownLatch latch;
public MyWaitingClass(final CountDownLatch latch)
{
this.latch = latch;
}
#Override
public void run()
{
try {
latch.await();
// do whatever is necessary
} catch (InterruptedException e) {
// Argh; interrupted before the latch was released
Thread.currentThread().interrupt();
}
}
}
// In the main class:
final ExecutorService executor = Executors.newSingleThreadPool();
final CountDownLatch latch = new CountDownLatch(1);
final Runnable runnable = new MyWaitingClass(latch);
executor.submit(runnable);
// do whatever is needed; then:
latch.countDown();

The output of the counter to this multi-threaded code

I came across a Java problem about multi-threaded programming (please see the code below). Based on this question and answer on StackOverflow, I think I understand why there could be a deadlock. But what I don't understand was if the program works correctly (i.e. there is no deadlock), what would be the value of foo printed? I thought it would be 20 (thread1 counting up to 10 and thread2 counting up to 10 more). Could someone help me explain how this might (preferably in a simple way because I'm still new to thread programming)? Thank you.
public class ThreadTest{
private static class ThreadOne extends Thread{
private ThreadTwo threadTwo;
public int foo = 0;
public void setThreadTwo(ThreadTwo th){
threadTwo = th;
}
public void run(){
try{
for(int i=0;i<10;i++) foo += i;
synchronized(this){this.notify();};
synchronized(threadTwo){threadTwo.wait();};
System.out.print("Foo: " + threadTwo.foo);
}catch(InterruptedException e){ e.printStackTrace();}
}
}
private static class ThreadTwo extends Thread{
private final ThreadOne threadOne;
public int foo = 0;
public ThreadTwo(ThreadOne th){
threadOne = th;
}
public void Run(){
try{
synchronized(threadOne){threadOne.wait();}
foo = threadOne.foo;
for(int i=0;i<10;i++) foo += i;
synchronized(this){this.notify();};
}
catch(InterruptedException e){e.printStackTrace();}
}
}
public static void main(){
ThreadOne th1 = new ThreadOne();
ThreadTwo th2 = new ThreadTwo(th1);
th1.setThreadTwo(th2);
th1.start(); th2.start();
th1.join(); th2.join();
}
}
According to your code and without deadlocks foo value will be 90 (if i didn't miscalculate). Because instead of foo += 1 you did foo += i.
EDIT: Okay, step by step.
foo = 0
th1 and th2 starts. th2 waits for notify. th1 increments foo up to 45
th1 notifies and starts to wait th2. th2 is notified and starts to increment foo from 45 to 90
th2 notifies th1. th1 is notified, and it prints th2.foo, which is 90
EDIT 2: Correct way to count from 0 to 90 from 2 threads without concurrent modification is something like this
public class ThreadTest {
private static int counter = 0;
private static class Thread1 extends Thread {
final Object lock;
public Thread1(Object lock) {
this.lock = lock;
}
#Override
public void run() {
synchronized (lock) {
for (int i = 0; i < 10; i++)
counter += i;
}
}
}
private static class Thread2 extends Thread {
final Object lock;
public Thread2(Object lock) {
this.lock = lock;
}
#Override
public void run() {
synchronized (lock) {
for (int i = 0; i < 10; i++)
counter += i;
}
}
}
public static void main(String[] args) {
final Object lock = new Object();
final Thread th1 = new Thread1(lock);
final Thread th2 = new Thread2(lock);
th1.start();
th2.start();
try {
th1.join();
th2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Counter: " + counter);
}
}
But if you are forced to use wait and notify, than it's a bit more complicated. Use object of this class as common lock instead of Object
class Locker {
private boolean isLocked = false;
public synchronized void lock() throws InterruptedException {
while (isLocked) wait();
isLocked = true;
}
public synchronized void unlock() {
isLocked = false;
notify();
}
}
And in run method us it like this:
#Override
public void run() {
try {
locker.lock();
for (int i = 0; i < 10; i++)
counter += i;
locker.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Printing "Hello" and "world" multiple times using two threads in java

Assume that one thread prints "Hello" and another prints "World". I have done it successfully for one time, as follows:
package threading;
public class InterThread {
public static void main(String[] args) {
MyThread mt=new MyThread();
mt.start();
synchronized(mt){
System.out.println("Hello");
try {
mt.wait();
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyThread extends Thread{
public void run(){
synchronized(this){
System.out.println("World!");
notify();
}
}
}
How do I do it for multiple time printing, say for 5 times? I tried putting for loop around the synchronized block, but of no use.
Here being two interdependent threads, we need two synchronizing objects. they could be one of many things. one integer, another object; one Boolean another object; both object; both semaphores and so on. the synchronization technique could be either Monitor or Semaphore any way you like, but they have to be two.
I have modified your code to use semaphore instead of Monitor. The Semaphore works more transparently. You can see the acquire and release happening. Monitors are even higher constructs. Hence Synchronized works under the hood.
If you are comfortable with the following code, then you can convert it to use Monitors instead.
import java.util.concurrent.Semaphore;
public class MainClass {
static Semaphore hello = new Semaphore(1);
static Semaphore world = new Semaphore(0);
public static void main(String[] args) throws InterruptedException {
MyThread mt=new MyThread();
mt.hello = hello;
mt.world = world;
mt.start();
for (int i=0; i<5; i++) {
hello.acquire(); //wait for it
System.out.println("Hello");
world.release(); //go say world
}
}
}
class MyThread extends Thread{
Semaphore hello, world;
public void run(){
try {
for(int i = 0; i<5; i++) {
world.acquire(); // wait-for it
System.out.println(" World!");
hello.release(); // go say hello
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadSeq {
Object hello = new Object();
Object world = new Object();
public static void main(String[] args) throws InterruptedException {
for(int i=0; i<6;i++){
Runnable helloTask = new Runnable(){
#Override
public void run(){
new ThreadSeq().printHello();
}
};
Runnable worldTask = new Runnable(){
#Override
public void run(){
new ThreadSeq().printWorld();
}
};
Thread t1 = new Thread(helloTask);
Thread t2 = new Thread(worldTask);
t1.start();
t1.join();
t2.start();
t2.join();
}
}
public void printHello(){
synchronized (hello) {
System.out.println("Hello");
}
}
public void printWorld(){
synchronized (world) {
System.out.println("World");
}
}
}
The goal here is to synchronize threads so that when one is done it notify the other. If I have to make it, it would be 2 threads executing the same code with different data. Each thread has its own data ("Hello" and true to T1, "World" and false to t2), and share a variable turn plus a separate lock object.
while(/* I need to play*/){
synchronized(lock){
if(turn == myturn){
System.out.println(mymessage);
turn = !turn; //switch turns
lock.signal();
}
else{
lock.wait();
}
}
}
Before you start trying to get it to work five times you need to make sure it works once!
Your code is not guaranteed to always print Hello World! - the main thread could be interrupted before taking the lock of mt (note that locking on thread objects is generally not a good idea).
MyThread mt=new MyThread();
mt.start();
\\ interrupted here
synchronized(mt){
...
One approach, that will generalise to doing this many times, is to use an atomic boolean
import java.util.concurrent.atomic.AtomicBoolean;
public class InterThread {
public static void main(String[] args) {
int sayThisManyTimes = 5;
AtomicBoolean saidHello = new AtomicBoolean(false);
MyThread mt=new MyThread(sayThisManyTimes,saidHello);
mt.start();
for(int i=0;i<sayThisManyTimes;i++){
while(saidHello.get()){} // spin doing nothing!
System.out.println("Hello ");
saidHello.set(true);
}
}
}
class MyThread extends Thread{
private final int sayThisManyTimes;
private final AtomicBoolean saidHello;
public MyThread(int say, AtomicBoolean said){
super("MyThread");
sayThisManyTimes = say;
saidHello = said;
}
public void run(){
for(int i=0;i<sayThisManyTimes;i++){
while(!saidHello.get()){} // spin doing nothing!
System.out.println("World!");
saidHello.set(false);
}
}
}
This is in C:
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t hello_lock, world_lock;
void printhello()
{
while(1) {
pthread_mutex_lock(&hello_lock);
printf("Hello ");
pthread_mutex_unlock(&world_lock);
}
}
void printworld()
{
while(1) {
pthread_mutex_lock(&world_lock);
printf("World ");
pthread_mutex_unlock(&hello_lock);
}
}
int main()
{
pthread_t helloThread, worldThread;
pthread_create(&helloThread,NULL,(void *)printhello,NULL);
pthread_create(&helloThread,NULL,(void *)printhello,NULL);
pthread_join(helloThread);
pthread_join(worldThread);
return 0;
}
There are two thread and both has its own data ("Hello" and true to ht, "World" and false to wt), and share a variable objturn.
public class HelloWorldBy2Thread {
public static void main(String[] args) {
PrintHelloWorld hw = new PrintHelloWorld();
HelloThread ht = new HelloThread(hw);
WorldThread wt = new WorldThread(hw);
ht.start();
wt.start();
}
}
public class HelloThread extends Thread {
private PrintHelloWorld phw;
private String hello;
public HelloThread(PrintHelloWorld hw) {
phw = hw;
hello = "Hello";
}
#Override
public void run(){
for(int i=0;i<10;i++)
phw.print(hello,true);
}
}
public class WorldThread extends Thread {
private PrintHelloWorld phw;
private String world;
public WorldThread(PrintHelloWorld hw) {
phw = hw;
world = "World";
}
#Override
public void run(){
for(int i=0;i<10;i++)
phw.print(world,false);
}
}
public class PrintHelloWorld {
private boolean objturn=true;
public synchronized void print(String str, boolean thturn){
while(objturn != thturn){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(str+" ");
objturn = ! thturn;
notify();
}
}
In simple way we can do this using wait() and notify() without creating any extra object.
public class MainHelloWorldThread {
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
Thread t1 = new Thread(() -> {
try {
helloWorld.printHello();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
Thread t2 = new Thread(() -> {
try {
helloWorld.printWorld();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// printHello() will be called first
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}
class HelloWorld {
public void printHello() throws InterruptedException {
synchronized (this) {
// Infinite loop
while (true) {
// Sleep for 500ms
Thread.sleep(500);
System.out.print("Hello ");
wait();
// This thread will wait to call notify() from printWorld()
notify();
// This notify() will release lock on printWorld() thread
}
}
}
public void printWorld() throws InterruptedException {
synchronized (this) {
// Infinite loop
while (true) {
// Sleep for 100ms
Thread.sleep(100);
System.out.println("World");
notify();
// This notify() will release lock on printHello() thread
wait();
// This thread will wait to call notify() from printHello()
}
}
}
}

multithread state-dependent issue

I was given following code snippet:
public class ThreadTest{
private static class Thread01 extends Thread{
private Thread02 _th2;
public int foo = 0;
public void setThrd02(Thread02 thrd2){
_th2 = thrd2;
}
public void run(){
try{
for(int i=0;i<10;i++) foo += i;
synchronized(this){this.notify();};
synchronized(_th2){_th2.wait();};
System.out.print(" Foo: " + _th2.foo);
}catch(InterruptedException ie){ ie.printStackTrace();}
}
}
private static class Thread02 extends Thread{
private final Thread01 _th1;
public int foo = 0;
public Thread02(Thread01 th1){
_th1 = th1;
}
public void Run(){
try{
synchronized(_th1){_th1.wait();}
foo = _th1.foo;
for(int i=0;i<10;i++) foo += i;
synchronized(this){this.notify();};
}
catch(InterruptedException ie){ie.printStackTrace();}
}
}
public static void main(){
Thread01 th1 = new Thread01();
Thread02 th2 = new Thread02(th1);
th1.setThrd02(th2);
th1.start(); th2.start();
th1.join(); th2.join();
}
}
I think the assumption and corresponding purpose of the code is like
th2 run first, it is changed to waiting status by calling _th1.wait();
Then, th1 calculates foo and wake up th2, th1 goes into waiting status;
Th2 reads foo from thread1 and updated to 110, then wakes up th1 and th2 exit.
Then th1 exit.
The threads could be very risk because it is very possible that thread one runs first and thread 2 will wait forever.
I am not sure any other potential problems of the code.
One possible way that can fix the problem is, for example in the thread1
public class ThreadTest{
private static boolean updated = false;
private static boolean finished = false;
private static Thread01 extends Thread{
public void Run(){
// do calcuation
while(finished){
wait();
}
// output result
}
}
private static Thread02 extends Thread{
public void run(){
while(false){
wait();
}
foo = th1.foo;
// do calculation
// similar mechanism to notify thread 1
}
}
There is no guarantee of ordering in your threads. It's sufficient for Thread01 to go past
synchronized(this){this.notify();}; before Thread02 does synchronized(_th1){_th1.wait();} to have both threads waiting indefinitely.
Note: The fact that you are calling wait and notify on _th1 and _th2 is irrelevant. Threads here will be treated as any other object.
#Alex has already pointed out the problems with wait and notify not being called in the order the code expects them to be (+1). However, since this is an interview question there are several other things wrong with this code:
Horrible naming conventions and code formatting,
Public field accessors,
Synchronizing on a Thread object (bizarre),
Catching InterruptedException and then just exiting the Thread,
No exception handling,
(Personal preference) Not using the Java concurrency libraries.
I'm sure the question was posed to tie you in knots and figure out why the concurrency is broken but, IMHO, that code is so hideous i wouldn't even begin to debug it - i'd just throw it away.
Following can be a better fix
public class ThreadTest{
private static volatile boolean updated = false;
private static volatile boolean finished = false;
private static class Thread01 extends Thread{
private Thread02 _th2;
public int foo = 0;
public void setThread2(Thread02 th2){
_th2 = th2;
}
public void Run(){
for(int i=0;i<10;i++) foo += i;
System.out.print(" thread1 calcualtion " + foo + "\n");
try{
updated = true;
synchronized(this) {this.notify();};
synchronized(_th2){
while(!finished)
_th2.wait();
System.out.print("Foo: " + _th2.foo );
}
}
catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
private static class Thread02 extends Thread{
private final Thread01 _th1;
public int foo = 0;
public Thread02(Thread01 th1){
_th1 = th1;
}
public void run(){
try{
synchronized(_th1){
while(!updated)
_th1.wait();
foo = _th1.foo;
}
for(int i=0;i<10;i++) foo +=i;
finished = true;
synchronized(this){ this.notify();}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread01 th1 = new Thread01();
Thread02 th2 = new Thread02(th1);
th1.setThread2(th2);
try{
th1.start();
th2.start();
th1.join();
th2.join();
}catch(InterruptedException ie){
ie.printStackTrace();
}
}

Categories