• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

can i invoke run method directly

 
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?
 
Rancher
Posts: 43016
76
  • 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).
 
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 ...
 
author
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic