• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Threads confusing

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
When I try to run the code using t.start() the output is about both main thread and the child thread. But when I try to run the code with t.run() then the out put is just about the child thread.
Can any one tell the reason why is this behaviour.What is the difference between calling t.start() or calling t.run() directly.
class counter implements Runnable
{
private int currentvalue;
private Thread t;
counter(String threadname)
{
currentvalue=0;
t=new Thread(this,threadname);
System.out.println(t);
t.run();
}//this is for the constructor
public void run()
{
try
{
while(currentvalue<5)
{
System.out.println("The thread's name is"+t.getName()+":"+(currentvalue++));
t.sleep(500);
}
}catch(InterruptedException e)
{
System.out.println("Thread is interrupted");
}
System.out.println("The child thread exitted");
}
public int getValue()
{
return currentvalue;
}
}//this is for the counter class

public class client
{
public static void main(String [] args)
{
counter c=new counter("Child");
try
{
int val;
do
{
val=c.getValue();
System.out.println("The main thread's value is"+val);
Thread.sleep(1000);
}while(val<5);
}catch(InterruptedException e)
{
System.out.println("Main thread interrupted");
}
System.out.println("Main exitted");
}//this is for the main method
}//this is for the class client
THanks & regards
Ananth
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats simple.
t.start() - starts the Thread named t which will execute the run method it is attached to. Note that you can not tell which of the two running Threads will execute next since t inherits the priority of the Thread that created it.
t.run() - the current Thread (which is executing a run method somewhere) now executes the run method attached to Thread t. Thread t remains in a non-running state.
Bill
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever we call the t.start() fn,after this line ,the compiler goes back to the method where the constructor was called from
and therefore the main thread starts executing.
and after the main thread,the child thread starts executing(output varies depending on the o.s).
if u call directly the run method only the compiler will move onto that method and start reding it w/out going to the main thread .
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic