• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

class Th extends Thread
{
public void run(){System.out.println("ABC");}
public static void main(String a[])
{
Th th=new Th();
Thread t=new Thread(th);
t.start();//1
t.run();//2
}
}
output is ABC
ABC
I understood that at line 1 the run() method of the Runnable object th is called. But why is the output of line 2 ABC...should that be calling the run() method(which will not print anything) of Thread class?

Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default run() method of the thread class calls the run() method of the runnable object. And BTW, if you wait until the thread finishes and cleans up after itself, calling the run() method may actually not print anything as the runnable object may get reset.

One more point, you don't know that the first ABC is caused by the start() method. It may actually be the second.

Henry
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry i didnt get you... could you explain once again ........

thanks
sri
[ October 12, 2005: Message edited by: srikanth reddy ]
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi srikant,
The run() method of the thread is being called in the method after start() method. However for the run() method to get executed standalone, the thread object must not reset or dead. If the thread object is dead, then run() method won't get executed and hence the output. You can try this by putting a join() before run() method in the code.
 
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call to a run() method directly works same as
calling an ordinary method of a class.

calling run() does not create a thread.

Thread is always created using start() method.

The start method creates the system resources necessary to run the thread, schedules the thread to run, and calls the thread's run method.
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic