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

calling start() twice on a thread.

 
Suresh Babu Venkat
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember reading that if we call start twice on a thread we will get IllegalThreadStateException.

but if i execute the code below which i found on http://www.javachamp.com/public/showQuestionDetail.xhtml?itemIndex=17&examId=178809



its doesn't throw any exception and gives output as main22 .


can somebody help me understanding this..
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember reading that if we call start twice on a thread we will get IllegalThreadStateException.

That's correct, but that's not what's happening here. To understand why, put "System.out.println(Thread.currentThread())" into the run method.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are actually two threads in your program 1. Test, that is your own thread. 2. From Thread class.
In the Test class's Constructor, you start() that thread. and in your main method, your pass this(you Test object) as target to the Thread class thread, and start it.
If you don't extends Thread class and implements Runnable as bellow, you'll get Compilation error, because Test class don't have start() method.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are starting two completely different threads although run method called is of same object.

Thread1 :- referenced by t
Thread2 :- anonymous Object

In constructor you are starting Thread2

In main you are starting Thread 1

Thanks !!!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Kapoor wrote:
Thread2 :- anonymous Object


That's not anonymous Thread, that is the Thread the programmer has created(The Test Thread)! The start() method of that thread is inherited from Parent Thread class, which the programmer extends. Check it with the invocation of run() method of this thread.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is not Anonymous then how would you control it.....I mean you dont have any reference to it.....except with the JVM whic calls run on it .
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Kapoor wrote:If it is not Anonymous then how would you control it.....I mean you dont have any reference to it.....except with the JVM whic calls run on it .


Try this code...
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir ,now its not more Anonymous , you have given it a reference name namely "testThread"


 
Pradeep Kr
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abimaran Kugathasan wrote:

Just to make it clear, I think above Abimaran means RuntimeException not compilation error.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep- Kumar wrote:
Abimaran Kugathasan wrote:

Just to make it clear, I think above Abimaran means RuntimeException not compilation error.


Yea, Exactly! Compiler doesn't know the logic! Thanks mate. It's Great~!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic