L
lonelyplanet999
Hi,
I write below program with one thread t1 calling another thread's
run() method from within. However, I found that out of 4 run of this
program. In one time it returned t2 was the thread calling run() from
within t1's run() while for others, it told t1 was the thread which do
the calling (see the '^' explicitly marked to illustrate). I couldn't
figure out why this happened. Is it because I haven't marked the
methods as synchronized ?
Program output on the first call
================================
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t2 is running***
t1 is running*** ^
Program output on the second call
=================================
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
t2 is running*** ^
Program output on the third call
================================
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
t2 is running*** ^
Program output on the third call
================================
Program starts
t2 is running***
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
=======================
public class Waiting5 {
public static void main (String [] args) {
System.out.println("Program starts");
MyThread2 t2 = new MyThread2();
MyThread1 t1 = new MyThread1(t2);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
MyThread2 t2;
MyThread1 (MyThread2 t2) {
this.t2 = t2;
}
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+" is running...");
System.out.print(name+" calling run() of MyThread2 class output:
");
t2.run();
}
}
class MyThread2 extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName()+" is
running***");
}
}
After I marked the run() methods as synchronized, I repeated calling
the program 5 times. In 3 of the 5 times, it output below:
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t2 is running***
t1 is running***
In 2 of the 5 times, it output below:
Program starts
t2 is running***
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
It seems the program behaviour doesn't change after coding change.
I write below program with one thread t1 calling another thread's
run() method from within. However, I found that out of 4 run of this
program. In one time it returned t2 was the thread calling run() from
within t1's run() while for others, it told t1 was the thread which do
the calling (see the '^' explicitly marked to illustrate). I couldn't
figure out why this happened. Is it because I haven't marked the
methods as synchronized ?
Program output on the first call
================================
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t2 is running***
t1 is running*** ^
Program output on the second call
=================================
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
t2 is running*** ^
Program output on the third call
================================
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
t2 is running*** ^
Program output on the third call
================================
Program starts
t2 is running***
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
=======================
public class Waiting5 {
public static void main (String [] args) {
System.out.println("Program starts");
MyThread2 t2 = new MyThread2();
MyThread1 t1 = new MyThread1(t2);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
}
class MyThread1 extends Thread {
MyThread2 t2;
MyThread1 (MyThread2 t2) {
this.t2 = t2;
}
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+" is running...");
System.out.print(name+" calling run() of MyThread2 class output:
");
t2.run();
}
}
class MyThread2 extends Thread {
public void run() {
System.out.println(Thread.currentThread().getName()+" is
running***");
}
}
After I marked the run() methods as synchronized, I repeated calling
the program 5 times. In 3 of the 5 times, it output below:
Program starts
t1 is running...
t1 calling run() of MyThread2 class output: t2 is running***
t1 is running***
In 2 of the 5 times, it output below:
Program starts
t2 is running***
t1 is running...
t1 calling run() of MyThread2 class output: t1 is running***
It seems the program behaviour doesn't change after coding change.