This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can i invoke run method directly  RSS feed

 
pandu ranga
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have piece of code

Thread myThread= new Thread();
myThread.run();


why should be invoke start method before invoking run ? Any reason for this?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Calling run does not actually start a new thread - it executes the code within the current thread context (and thus blocks it until run finishes).

Only calling start will actually start a new thread, and execute its runs method in a new concurrent context (and thus ensure that the current thread can continue running right away).
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[pandu ranga:]why should be invoke start method before invoking run

Start actualy calls the run method, which may or may not do some good on a single processor machine. On a multiple processor machine using the start() form allows multiple processors to work concurrently. Java has an api for this work: JDK 5.0 Concurrency-related APIs & Developer Guides -- from Sun ...
 
Henry Wong
author
Sheriff
Posts: 22840
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start actualy calls the run method, which may or may not do some good on a single processor machine.


The start() starts a thread which in turn, calls the run() method -- it doesn't directly call the run() method.

In most cases, it does "do some good" on a single processor machine. Threads are timesliced even with a single processor, so while it doesn't actually run parallel on a single processor, the asynchronous nature of threads has tons of value.

Henry
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed, I was trying to simplifiy for poster.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!