wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Execution " Watch "Thread Execution " New topic
Author

Thread Execution

Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91




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

Joined: Aug 02, 2006
Posts: 40
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

Joined: Jun 16, 2006
Posts: 19
Hello,
I am totally agree wiht you
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
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

Joined: Jul 24, 2006
Posts: 48
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

Joined: Jul 04, 2006
Posts: 235
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

Joined: Aug 02, 2006
Posts: 40
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

Joined: Feb 02, 2006
Posts: 91
yes it will 0 1 2
 
Don't get me started about those stupid light bulbs.
 
subject: Thread Execution
 
Similar Threads
join() method in Thread doesn't work as expected
threads and uncaught exceptions
Thread - The # between static synchronized and non static synchronized
i++ is not a singular operation"?
Threads: synchronizing a block of code (K&B 1.5)