jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread ques. 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 ques." Watch "Thread ques." New topic
Author

Thread ques.

Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
This is from JQplus sample
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);
}
}
What will it print when run?
Why this is printing 5 not 10?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by swati gupta:
Why this is printing 5 not 10?

Because the thread referenced by tc hasn't actually run yet. Sure, you've called the start method so a new thread has been created, but it is in the "ready to run" state, not the "running: state. Therefore, you're outputting the value of x prior to the lines that changes it being executed. If you change the code like this to ensure that the new thread gets a chance to execute:

In this case, you'll get your 10.
I hope that helps,
Corey


SCJP Tipline, etc.
geetha nagarajan
Ranch Hand

Joined: Jul 13, 2001
Posts: 94
How can we determine if the thread is in running state or ready - to - run state?.
Is it not true that once start() is called ,the run will be executed sooner or later,so,the output won't vary?
Pl.clarify.
Swati Gupta
Ranch Hand

Joined: May 28, 2002
Posts: 106
will it always print 5 or there is possibility that the thread may get chance to run (without calling sleep on main) and it might print 10
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
When a thread gets to run is entirely up to the implementation of the underlying OS. If the initial code had been posted on the SCJP exam, the correct answer would have been something along the lines of "The output can not be determined."
What I did was caused the main thread to sleep for a second. This gives the newly spawned thread a chance to get a timeslice on the processor and modify the value of x. Even though I did this, it still isn't "guaranteed" (although quite likely) that the newly spawned thread will get a chance to execute.
To sum up, you have no control over when a thread starts running. You can explicitly halt a thread (i.e. by calling Thread.sleep), but you can't tell a thread to start. That's up to the OS.
I hope that helps,
Corey
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by geetha nagarajan:
How can we determine if the thread is in running state or ready - to - run state?.
Is it not true that once start() is called ,the run will be executed sooner or later,so,the output won't vary?
Pl.clarify.

It's true that, since we called start, the run method will be executed sooner or later. However, what the output is really depends upon if that execution of run happens before or after the System.out.println statement in main. If it happens before, the output will be 10. If it happens after, the output will be 5.
Read my previous post for some more details.
Corey
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Swati,
It is possible for the output to be 10 without calling sleep. the concept is if you run some code between new Thread(tc).start(); // 1
and System.out.println(tc.x)
which cause a big enough delay so the run() get excuted before you print tc.x.
I add one for loop to simulate the delay, then run it 10 times.
possible Result :
10,5,5,5,10,5,5,10,5,5

public class TestClass implements Runnable
{
int x = 5;
public void run()
{
this.x = 10;
}
public static void main(String[] args)
{
for (int j=0; j<10;j++) {
TestClass tc = new TestClass();
new Thread(tc).start(); // 1
for (int i=0; i<500000;i++){ }
System.out.println(tc.x);}
}
}

Originally posted by swati gupta:
will it always print 5 or there is possibility that the thread may get chance to run (without calling sleep on main) and it might print 10


not so smart guy still curious to learn new stuff every now and then
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread ques.