aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads problem." Watch "Threads problem." New topic
Author

Threads problem.

Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102


Shouldn't it throw an IllegalThreadStateException? Turns out the output is 2main2 and main22. Could someone please explain this to me?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Because, you're calling different start() methods, but, ultimately, same thread runs. That's internal implementation of start() method.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
But dude, the method is non static. How is it possible to invoke it just like that?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Within the Constructor of the Test class, you've invoked the inherited start() method of the Test class, and, in 13th line, you've invoked the Thread class start() method! So, as those are different, you can invoke. But, ultimately the Test thread object, which you've created in the 12th line, will be the thread which will invoke the run() method!
Saumya Srivastava
Ranch Hand

Joined: Mar 19, 2007
Posts: 45
I think when main thread invoked the constructor when the object of the class was created. Because during that time the new Thread is just created. It is still not alive. It will be alive only after calling start() method. And I have read in one of the Java documents that invoking start() method from the constructor is not a good idea. Doing so could expose partially constructed objects to the new thread. So, may be because of this reason when main thread invoked constructor a new thread might have invoked start() from constructor and executed run() function. I am sorry I don't know much in detail that how it happens.
But you can check this by using System.out.println(Thread.currentThread().getName()) in the constructor and in the run() method.
You will get a new Thread which have executed at the time when you call start() from constructor. And 2nd time is invoked by the thread that we created in the code.

Hope that helps.

Thanks,
Saumya


OCPJP 6.0
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Thank you.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Abimaran Kugathasan wrote:
Within the Constructor of the Test class, you've invoked the inherited start() method of the Test class, and, in 13th line, you've invoked the Thread class start() method! So, as those are different, you can invoke. But, ultimately the Test thread object, which you've created in the 12th line, will be the thread which will invoke the run() method!


Correct reason.
IllegalThreadStateException is thrown, if line 12 is replaced by:


Because in this case, the start() method of the Test object would be run twice (once in the constructor, and second time in main method)

OCPJP6
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
I don't get it what does

do?

2 is just an int value how will Thread class accept it as an argument? It can only accept threads and runnables right?
I'm guessing you meant

Test t=new Test(2);

Right?
Vasily Stepanov
Greenhorn

Joined: Oct 31, 2008
Posts: 5
Did you mean in this case there will be exception.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Rohit Ramachandran wrote:
I'm guessing you meant

Test t=new Test(2);

Right?

Yes, he means that. Is it OK to you? Cleared?
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Yeah, it's

It was a typing mistake.
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Thanks guys. I appreciate it. Completely understand it now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads problem.
 
Similar Threads
join()??
Threads - static variable and static/non static method execution
calling start() twice on a thread.
Strange Error [java.lang.ArrayIndexOutOfBoundsException]