• 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

Threads problem.

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you mean in this case there will be exception.
 
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

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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I appreciate it. Completely understand it now.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic