• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

thread

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this question from http://www.jchq.net/mockexams/exam1.htm

The answer is 3, and it explains: If you wished to get the output mixed you would need to override the run method but call the start method.

Does this question mean if the class did not override public void run(), it is not actually running new Threads?

Thanks
 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reubin,

Background:
If you notice the start method declaration in the Thread.java file,

public synchronized native void start();

it is native method. So in this native method, JVM would create a thread (appropriately depending on the target OS, like windows or Unix) and then it invokes the run method. Run method implementation in the Thread class, does nothing but executing a run of runnable implementation (if you had provided one). Else, just return.

In the example, you are overriding the start method. So when you call start, instead of calling the native start method (which actually creates the thread), you are calling your own implementation of start, which just executes some statements (sequencially).

Answer to your question:
Whether it is creating thread or not, is not depending on the run method but start method. If you have given any impmentation, thread is actually created but it has nothing to execute. So it dies very soon it got created.

Hope this helps.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since start() is overridden, this code onlylooks multithreaded. The output is the same as if Holt extended Object (or Vector, or JFrame, or whatever) rather than Thread.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think in this start() is overridding and no new thread is created..
becuase its not calling the start() method of Thread class which internally calls the run() method of Thread class or subclassimplementing Thread class.

so no new thread is create except the main() one...
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic