File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "threads" Watch "threads" New topic


abhishek chauhan

Joined: Jul 21, 2005
Posts: 8
why only one thread runs in this code

public class NiceThreads implements Runnable
public void run()

public static void main(String args[])
NiceThreads nt1 = new NiceThreads();
NiceThreads nt2 = new NiceThreads();
NiceThreads nt3 = new NiceThreads();;;;
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
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.

shyam ramineni
Ranch Hand

Joined: Jul 08, 2005
Posts: 43

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.

Shyam Ramineni

<a href="" target="_blank" rel="nofollow"></a>
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
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.
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61 Thread object 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...


I agree. Here's the link:
subject: threads
It's not a secret anymore!