aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes strange behaviour with Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "strange behaviour with Threads" Watch "strange behaviour with Threads" New topic
Author

strange behaviour with Threads

Sagar kanchi
Ranch Hand

Joined: Oct 07, 2005
Posts: 50
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.......


Where there are more wonders there is less knowledge
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
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

Joined: Jul 28, 2005
Posts: 252
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


Thanks & Regards<br /> <br />-Srikanth
Ashwin Kumar
Greenhorn

Joined: Oct 13, 2005
Posts: 27
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.


SCJP 1.4<br />SCBCD 5.0<br />SCWCD 1.4 (Preparing)
sampath garapati
Ranch Hand

Joined: Sep 26, 2005
Posts: 39
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: strange behaviour with Threads