• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source:http://www.danchisholm.net/oct1/topic/section7/threads1.html
Can anyone explain me the flow of this program,i am not able to figure out???

class A implements Runnable {
public void run() {System.out.print(Thread.currentThread().getName());}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}}
class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}}

Answer: T1T1T3
 
chander shivdasani
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
Source:http://www.danchisholm.net/oct1/topic/section7/threads1.html
Can anyone explain me the flow of this program,i am not able to figure out???

class A implements Runnable {
public void run() {System.out.print(Thread.currentThread().getName());}
}
class B implements Runnable {
public void run() {
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}}
class C {
public static void main (String[] args) {
new Thread(new B(),"T1").start();
}}

Answer: T1T1T3



The main program calls a new thread call T1 which calls the run method.

new A().run();
new Thread(new A(),"T2").run();

The above two calls doesnot create a new thread, they simply call run()(over here run is treated like any other function, so a new thread will not be created)

So the line

System.out.print(Thread.currentThread().getName() prints the name of the current thread which is T1

A new thread is created only when you call start().


The following line

new Thread(new A(),"T3").start();

Creates a new thread and sets its name as "T3"

Hence the output is T1T1T3
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In line 12, a new Thread object T1 is created and started(thread) with an object of class B as target.
And the run() method(line 5) in B will be invoked by the thread T1.
And in line 6, a new object of class A is created and run()(line 2) method is invoked. Remember no new thread is created. This call is similar to any other method call. And print stmt will print T1 as the current executing thread is T1.
Control comes back to line 7. Here a new Thread object will get created with an object of A as target. The Thread object is given T2 as its name. But no new thread is created. Again line 2 will be executed: resulting in T1.
Control comes back to line 8. Here a new Thread object T3 will get created with an object of A as target and started(a new thread is created).
Then run() method(line 2) is executed. As the current executing thread is T3, print stmt will print T3.

Thats why the answer is: T1T1T3

Hope this is clear!
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Shrilatha and Chander.I am getting confused with the Thread questions, as I am new to this topic.
Also i was not aware that while passing to the thread constructor we can actually name the Thread...
new Thread(new B(),"T1").start();
Usually, its done this way:
Thread t1=new Thread();
t1.setName("T1");


So, I got confused.
[ October 14, 2008: Message edited by: Abhi vijay ]
 
Mohammad Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remember the followings when you try to resolve thread question related to run() and start()

If you have a class that implements Runnable
1. As a contract, you need to implement method run()
2. You still can have method start() in your class, if you want
3. If you have a thread like

you can execute t.start() which would execute MyRunnable#run a new thread.
4. You can execute t.run(), which will do the same of 3, except it won't have a new thread, instead it would execute in main thread.
5. Using reference of t, you can't invoke MyRunnable#start(), If you want to invoke it, you would need a reference of MyRunnable, such as

would execute MyRunnable#start() in main thread, NOT in new thread.

Thanks,
Mohammad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic