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 Another thread ques 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 "Another thread ques" Watch "Another thread ques" New topic

Another thread ques

Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
Can someone pl. explain about this ques.
When I uncomment the line at 1 I get a illegalThreadStateException which is because I am starting a thread already running but when I uncomment the line at 2 (commenting the line at 1 ) I don't get any exception and program runs successfully but does not print the number from 0-9 between "main1" and "main2". Here I am starting a thread which has earlier completed its work but it is not started why?
Is there is some other explanation for this.
Thanks alot.
public class ThreadTest implements Runnable {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println("The value of i " +i);
public static void main(String [] args) {
ThreadTest tt = new ThreadTest();
Thread a1 = new Thread(tt);
Thread a2 = new Thread(tt);
Thread a3 = new Thread(tt);
// a2.start(); //1
}catch (InterruptedException e){System.out.println(e);}
System.out.println("In main 1");
// a2.start();//2
System.out.println("In main 2");
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I believe that you can only start a thread once. Once it has terminated, you can't start it again.
I hope this helps,

SCJP Tipline, etc.
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
Threads are like human beings,once a Thread is dead,it cant be revived or restarted.In the try block,when you call Thread.sleep(1000),the main thread sleeps for a second and the other threads complete.At line 2,the other thread is dead,so when you call the start method,it does start again.Please note a thing,calling start method on a dead thread does not cause an exception.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Adding to what has been said already,
  • Although you cannot call start on the same thread object, you can use it as a target to create another thread. In your example, you can say new Thread(a2).start(); This will indeed create another thread.
  • Calling start() is slightly different than calling run(). start() tells the JVM to create a lightweight process using the particular object as the thread object. The lightweight process will then, run as a JVM thread. When the thread eventually gets to run, it will execute the code in the run() method. Whereas, if you were to call run() directly, it will execute like a synchronous plain vanilla method call.

  • Hope that helps!

    Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
    Swati Gupta
    Ranch Hand

    Joined: May 28, 2002
    Posts: 106
    Thanks Corey,Gautam and Ajith.
    I agree. Here's the link:
    subject: Another thread ques
    It's not a secret anymore!