• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

strange behaviour with Threads

 
Sagar kanchi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the following code

class Th extends Thread{
public void run(){System.out.println("ABC");}
public static void main(String[] args) throws Exception
{Th th = new Th();Thread t = new Thread(th)\\1;
t.start();t.join();t.run();}}

Output is : ABC

Now remove line number 1 i.e. rewrite the code in main as follows
Th t = new Th();t.start();t.join();t.run();
Output is : ABC ABC

Whats happening there?
Please can anyone help me.......
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: The distinction is in calling run on a Thread constructed using a Runnable instance (in this case, a Thread that's already died) vs. calling run on a Thread created with a no-args constructor.
 
Thomas Drew
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
t.start() start your thread running which executes the commands in the run() method. You then call the run method again with t.run(). Thus the run method is called twice. once by the thread and once with t.run().
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thomas i agreed to ur point..but in the previous case why is it printing only once....


is it like that ..incase first case since we are calling on thread object ..
for t.start(); thread's start method is called which inturn calls the run method but whom ?? mentioned in argument right...
for second case t.run()...it directly invokes the thread's run method which has empty body ...so its not printing anything...right

please clarify

thanks
sri
 
Ashwin Kumar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Th extends Thread{
public void run(){System.out.println("ABC");}
public static void main(String[] args) throws Exception
{Th th = new Th();Thread t = new Thread(th)\\1;
t.start();t.join();t.run();}}


In this case...
'th' is the reference variable to the object belonging to the class 'Th'. Hence you can say "th.run()" to invoke run() just like anyother method (Note: Only th.start() will create a live thread). But you cannot say "t.run()" because, 't' is the reference to the Thread created and NOT TO THE object of the class 'Th'. So you cannot invoke a method in 'Th' using 't'.

Now remove line number 1 i.e. rewrite the code in main as follows
Th t = new Th();t.start();t.join();t.run();


In this case...
't' is the reference to the object of class 'Th'. Hence you can very well call the run() method as 't.run()'. Note that this will just execute the SOP and not create a thread.
Hope it is clear.
 
sampath garapati
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Th extends Thread{
public void run(){System.out.println("ABC");}
public static void main(String[] args) throws Exception
{Th th = new Th();Thread t = new Thread(th)\\1;
t.start();t.join();t.run();}}
---------------------------------------------------------------------------


In this case...
'th' is the reference variable to the object belonging to the class 'Th'. Hence you can say "th.run()" to invoke run() just like anyother method (Note: Only th.start() will create a live thread). But you cannot say "t.run()" because, 't' is the reference to the Thread created and NOT TO THE object of the class 'Th'. So you cannot invoke a method in 'Th' using 't'.


Then why if we execute the following code:

class Th extends Thread{
public void run(){
System.out.println("ABC");
}
public static void main(String[] args) throws Exception{
Th th = new Th();
Thread t = new Thread(th);//1;
t.start();
t.run();
}
}

It is displaying ABC ABC

Please explain

Thanks
Sampath
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic