Inconsistent execution output

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

Andrew Thompson

ak said:
...
I suggest you read first one of many books about Threads and synchronisation
in java.
<most requoted post snipped>

Please quote only what is necessary to give
a context ak. It was not necessary to repost
the OP's 4Kb of code for the single line reply.
 
L

lonelyplanet999

ak said:
I suggest you read first one of many books about Threads and synchronisation
in java.
Read the books:

"Java 2 - Sun Certified Programmer & Developer for Java 2 (310-035) by
Kathy Sierra, Bert Bates"

"Java 2 certification all-in-one exam guide by Wiliam Stanek"

No discussion about my issue :)

Actually, I think my question is not typical that java programming
books will usually cover.
 
P

Peter J. Grey

Both threads, directly or indirectly, are calling MyThread2.run(). Where
that output will appear will depend on which thread is starting ahead of
the other, and on context switching. How is this 'inconsistent execution
output'? What sort of consistency do you expect in this situation?

Peter
 
T

Thyme

Read the books:

"Java 2 - Sun Certified Programmer & Developer for Java 2 (310-035) by
Kathy Sierra, Bert Bates"

"Java 2 certification all-in-one exam guide by Wiliam Stanek"

No discussion about my issue :)

Actually, I think my question is not typical that java programming
books will usually cover.

I've read the first of these books to help prep for a Java exam, but
it's not designed to teach anyone to use threads.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top