Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another thread ques

 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
a1.start();
a2.start();
// a2.start(); //1
try{
Thread.sleep(1000);
}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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that you can only start a thread once. Once it has terminated, you can't start it again.
I hope this helps,
Corey
 
Gautam Sewani
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
     
    Swati Gupta
    Ranch Hand
    Posts: 106
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Corey,Gautam and Ajith.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic