• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Thread Ques

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JQplus
Assume that the class compiles fine.
What will happen if a Worker object is created and it's start method is called?

The answer is "nothing will be printed."
But I think we are creating here a new worker object and will be calling its start() method that will call the
run() method and it will print Working .. once?
I don't understand that here we are creating here another thread inside the start method?

[ June 11, 2002: Message edited by: swati gupta ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
swati
The start method in the Thread class does all of the behind scenes work to create a new thread and then calls its run method to get it started. When you oeverride the start method you lose all of that functionality and it will execute as a normal method and do nothing except what you tell it to do. In his case it just creates a new Thread and then returns. the run method is never called.
If you did call the run method in your overloaded start metod it would just execute inline (ie. not as a separate thread).
Hope that helps
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave. This is the first example I came across where we are overriding the start method.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave,
I dont think the above code overrides the start method.The worker class implements the Runnable interface and the runnable interface doesn't even have a start method.So I dont think it is the case of overriding.
The problem coz of which the thread does not start is because the start method on the Thread is not called.For eg,a one line addition would make the worker class execute the run method.

When the start method of this worker class is called,the run method will definitely be executed.
Please correct me if I am wrong
 
Swati Gupta
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Gautam. Here Worker is implementing Runnable so if I create a new Thread and pass a object of Worker class as argument and call a start method on this Thread then it works as shown below

[ June 12, 2002: Message edited by: swati gupta ]
 
You have to be odd to be #1 - Seuss. An odd little ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic