• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Threads

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the output?
public class NiceThreads implements Runnable
{
public void run()
{
while(true)
{
}
}

public static void main(String args[])
{
NiceThreads nt1 = new NiceThreads();
NiceThreads nt2 = new NiceThreads();
NiceThreads nt3 = new NiceThreads();
nt1.run();
nt2.run();
nt3.run();
}
}
The code compiles but runs only 1 non ending, non demon thread.
It is because no new threads are created. call to first run method runs in the current thread. since it doesnt complete, the next two calls are never executed.
Am I right?
Thanks
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the following -
public class NiceThreads extends Thread
{
public void run()
{
while(true)
{
}
}
public static void main(String args[])
{
NiceThreads nt1 = new NiceThreads();
NiceThreads nt2 = new NiceThreads();
NiceThreads nt3 = new NiceThreads();
nt1.start();
nt2.start();
nt3.start();
}
}
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya
If you call a threads run method directly (instead of calling start) the run method runs in the current thread not a new thread. so you're calling the run method on the first thread and it is looping endlessly, and will never return to call the next run method.
This code shows that that is whats happening:

This will print the numbers 0 to 9999 in order three times in a row. Then change it like Dan showed in his post to call start instead of run, you should see some evidence of the threads being preempted by the others. It may print 0 to 8000 then start at 0 again for the next thread, then later you'll see it start at 8001 to finish the first one. If you dont see it you can always increase the loop counter.
hope that helps
Dave
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Running the run() does not cause a thread to be spawned. Only running the start() method will do that.
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic