• 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: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why only one thread runs in this code

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();
}
}
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 thread objects are created and initially their priorities are same. So, the first thread starts and enters into infinite while loop. This thread never blocks and hence runs for ever.
However, this output needs not to be same for all the cases, since thread implementation is platform dependent. For instance, some operating systems use round robin scheduling, that time, will see all the threads are running in a periodic interval for a fixed time.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,




Thread can only be started by calling the start() method.

In the above code , run() has be called on the instances of the thread.
In this context run() method is executed like any other instance method.
So, the above code does start any new thread.

But the anwser is 1 thread. As every time you run a program a new thread is created which invokes main() method.

Thanks
Shyam Ramineni
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you watch closely, we instantiated only the runnable
instances and then the called the run methods on these
runnable objects.

This piece of code never passed a runnable object to a Thread
constructor, to active a thread object.

The run, in this case, is treated as any normal
method. (Note : even if the runnable object is passed
to the thread constructor, the behavior would be, i.e.,invoking run
method directly would still be treated as any normal method
same as long as start method is not invoked). Hope this helps.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Actually..no Thread object instantiated...so no thread process Use Thread class constructor to instantiate a thread object

For example:

Thread t = new Thread(Runnable Object);

and to start process(to invoke run method) call start method:

t.start();//it will invokes run() method which is overrieden

After all these steps...We are able to run a thread...


Reagards

RK
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic