Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Execution

 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




A : The output will be:
1
0
2
B : The output will be:
0
1
2
C : The output will be:
1
2
0
D : The output is unpredictable.


I think output will be "D"

Am i right??
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand my recent reading correctly, you are correct. The JVM decides which thread runs when. You can use the sleep method to *try* to control which thread runs at what time, but it is not guaranteed to work.
 
Justin Yao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am totally agree wiht you
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output will be 0 1 2.

Note that you are not starting a new thread of execution, but just calling the method run().

To start a thread, you have to use start() method.
 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are correct surendra. I have a question ..when we start a thread by extending the Thread class, should we not override the start method ?
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Only run() method code would run in a separate thread of execution.
start() method is to start a new thread of execution.
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that was a good catch.

Originally posted by Surendra Nichenametla:
The output will be 0 1 2.

Note that you are not starting a new thread of execution, but just calling the method run().

To start a thread, you have to use start() method.
 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it will 0 1 2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic