This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

threads  RSS feed

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Once a thread has been started, it can never be started again."

"If you have a reference to a Thread, and you call start(), it's started. If you call
start() a second time, it will cause an exception"

that was in katherine sierra and bert bates book.. I had a small doubt on that concerning a question asked in sun's pre assessment..

lets say, t is a reference to a valid Thread object and run() method is as follows :


if i do


what will happen? isn't it starting same thing 3 times? ( and I tested it, it gives output as "go go go" )

where did I not understand the book?
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what will happen? isn't it starting same thing 3 times? ( and I tested it, it gives output as "go go go" )



Simply.... no. The first two calls are just two calls of the run() method. It is not starting a new thread to call the run() method. Your calling thread is calling the run() method. This is no different that your method calling any other method.

On the other hand, when you call the start() method -- a thread is kicked off to call the run() method. And the start() method immediate returns.

Henry
 
Ranch Hand
Posts: 38
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Actually when you invoke t.run you do not start the Thread t! Instead, you just call a method like you do it with a normal object.
The only way to start a Thread is to invoke method start on Thread object! So if you try

you will get a Runtime Exception.
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

what will happen? isn't it starting same thing 3 times? ( and I tested it, it gives output as "go go go" )



Simply.... no. The first two calls are just two calls of the run() method. It is not starting a new thread to call the run() method. Your calling thread is calling the run() method. This is no different that your method calling any other method.

Henry



so does it mean that for the first 2 times, it is the "main" thread printing "go" twice? and thus the third "go" is printed by the third invocation.. ? did i get this right?
 
Shrinath M Aithal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to valentine and henry, i understood this by trying this piece of code :



I saw that its actually running the main itself as you both said.. I was under a wrong impression that " run() " and " start() " are same..
Thank you

and as valentin said, I cant use 2 starts and now i understand that thanks for answering such silly question
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shrinath M Aithal wrote:

so does it mean that for the first 2 times, it is the "main" thread printing "go" twice? and thus the third "go" is printed by the third invocation.. ? did i get this right?



Yup you got it right, first 2 times it is the main thread which does the job. The third one will be printed by the second thread which runs the run() method by start() method.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!