This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

start and run method

 
Neelima Paramsetty
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calling the run method does not start a new thread. When you call start on a Thread object, the JVM starts a physical thread on the OS and then calls run. So if you call run directly, you are just using the calling thread, not creating a new thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic