aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes calling start() twice on a thread. 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 "calling start() twice on a thread." Watch "calling start() twice on a thread." New topic
Author

calling start() twice on a thread.

Suresh Babu Venkat
Ranch Hand

Joined: Sep 15, 2009
Posts: 30
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..


SCJP 6.0 (88%) | SCWCD 5.0 (89%)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
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.


Ping & DNS - my free Android networking tools app
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
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 !!!

SCJP 6.0 96%

(Connecting the Dots ....)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Sep 12, 2009
Posts: 316
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Sep 12, 2009
Posts: 316
Sir ,now its not more Anonymous , you have given it a reference name namely "testThread"


Pradeep Kr
Greenhorn

Joined: Feb 17, 2010
Posts: 22
Abimaran Kugathasan wrote:

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

Joined: Nov 04, 2009
Posts: 2066

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~!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling start() twice on a thread.