Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic


sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
This is a question on the topic 'thread' in a mock exam
public class TestClass implements Runnable
int x = 5;
public void run()
this.x = 10;
public static void main(String[] args)
TestClass tc = new TestClass();
new Thread(tc).start(); // 1
The ans given is 'output cannot be predicted'. Pl. explain to me..
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Hi sanjana,
We can't predict the output because we can't guarantee when the method run() will be executed (after or before System.out.println(tc.x)).

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
Vivek Nidhi
Ranch Hand

Joined: Aug 10, 2003
Posts: 133
Invoking a start() method on a thread object, this will move the thread to the running state, this will be in a pool of threads waiting to run. Here the Thread Scheduler will select a thread in pool for running, so which Thread will be selected depends on the Thread Scheduler
So invoking a Start method on a thread only says that thread moves from new state to ready state and may move to running state.
Hope this helps
Vivek Nidhi
It is sorta covered in the JavaRanch Style Guide.
subject: Thread
It's not a secret anymore!