• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

threads

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please tell me why the output of the following code is T1T1T3.



why not T1T2T3.

new Thread(new A(),"T2").run();
here we are creating a new thread "T2" and on this thread we are invoking run() method directly.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Invoking the run() method doesn't start a new thread. All you're doing is making a normal method invocation, i.e. you're executing the statements in run() within the same thread that called it. You need to use the Thread object's start() method to actually create a new thread. start() will then invoke run() for you in the new thread.

(This is an extremely common point of confusion, by the way. I can't even begin to estimate the number of times I've spotted junior programmers making this mistake at my workplace.)
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Kelvin says directly calling run() will not take you into a new thread's execution context (T2) so it's still T1. Look at the code and see where the direct call to run() is made, think about what the current thread is at that point, and note that THAT will be the current thread when the run() method is executed (just as if you called any other method like foo() or bar(). In fact, when you created the T2 Thread in went into the 'NEW' state because you did get an instance of Thread, but the only way it will go into the 'RUNNABLE' state, and eventually on to RUNNING, is if you call start(). Not saying anything different than Kelvin really but maybe it'll help seeing it explained two different ways.
[ December 07, 2007: Message edited by: nico dotti ]
 
ushma agarwal
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Kelvin and nico for explaining it so nicely
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic