• Post Reply Bookmark Topic Watch Topic
  • New Topic

threads  RSS feed

 
Shrinath M Aithal
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?
 
Henry Wong
author
Sheriff
Posts: 23292
125
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
 
Valentin Ivanov
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
 
Parashuram Hallur
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!