File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

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
System.out.println(tc.x);
}
}
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. <br />- What truth? <br />- That there is no spoon!!!
Vivek Nidhi
Ranch Hand

Joined: Aug 10, 2003
Posts: 133
Hello
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
regs
Vivek Nidhi
 
wood burning stoves
 
subject: Thread