• 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

calling start() twice on a thread.

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir ,now its not more Anonymous , you have given it a reference name namely "testThread"


 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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~!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic