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

Threads problem.

 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because, you're calling different start() methods, but, ultimately, same thread runs. That's internal implementation of start() method.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But dude, the method is non static. How is it possible to invoke it just like that?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you mean in this case there will be exception.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it's

It was a typing mistake.
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I appreciate it. Completely understand it now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic