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

thread

 
smily s sharma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
have a look this is an interesting q.actualy, the answer just struck my mind, while posting this q on the forum.
hope you would like it.

o/p T1T1T3

----- indenting repaired, Bu.
[ January 08, 2008: Message edited by: Burkhard Hassel ]
 
Ian Edwards
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It took a minute to unravel what was going on there.

Calling the run() method doesn't start the thread which is why calling the run() method on A prints out the thread name of thread T1 on the first 2 occasions.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

as Ian said: The thread with the // doubt in the line is actually never started.
It only invokes the run method without starting as a seperate thread. And since there is no seperate thread started with "T2" as name, you'll never read this.


Something else, "smily sharma" !

Please have a look into your private messages by clicking the "My Private Messages" link near the top of this page.

Yours,
Bu.
 
Arjun Cheng
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just runed the program. I got T1T2T3 as I thought.
 
Christian Hans
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why the line wouldn't start a new thread, as was previously mentioned. Calling a Java thread's start method initializes a thread and calls its run() method. If the line were to read , the output would be T1T1T3. As was written in the original post, the code should print T1T2T3.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic