• 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

start and run method

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sir,
i have the following code.for calling the run method,we have to use start() mehtod.though i am directly calling the run method through run.it is working nicely.then waht for start();
Anybody help me?
class Thread1 implements Runnable
{
Thread t;
String name;
Thread1(String tname)
{
name=tname;
t=new Thread(this,name);
t.run();
}
public void run()
{
for(int i=0;i<8;i++)
{
System.out.println(i);
}
}
}
class DemoJoin
{
public static void main(String args[])
{
new Thread1("one");
}

}
thanks
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, except that by calling run() directly, your thread is being executed on a single thread and is not getting its own thread.
Try something like this:

Then try the same code using t.start()
Then only difference between my code and yours is the second Thread1 instance and the sleep block. (and it prints the name so you can see which Thread1 instance is running)
Dave
[ July 07, 2003: Message edited by: David O'Meara ]
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic