wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Two Threads same run method: SCJP questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Two Threads same run method: SCJP questions" Watch "Two Threads same run method: SCJP questions" New topic
Author

Two Threads same run method: SCJP questions

John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
Hi all,
First post here. Hoping for a warmer reception than the java.sun boards which seem to be full of people writing 'that's a stupid post' posts.
It's the old chestnut question, i'm afraid: same Runnable, two Threads. I have noticed this question or variations thereof are very popular on SCJP mock exam papers. Considering this class:
class Test implements Runnable
{
static int x;
public void run()
{
x = 0;
for (int i = 0; i < 10; i++)
{
System.out.println (x++);
}
}
public static void main (String [] args)
{
Test t = new Test();
new Thread (t).start();
new Thread (t).start();
}
}
Now, as the run method is not synchronized and x is not a local variable, we can theoretically have interference... can't we? Correct me if I'm wrong but one Thread could halt just before the x=0; while the other is incrementing, and then the other could run, thus reseting x. In reality, it will almost always just print 0-9 0-9 because the program is so small. This is the problem. Most of the SCJP mock questions I've seen have two possible answers:
a) Program prints 0-9 0-9
b) Values are incremented upwards a total of 18 times but not necessarily in order.
Now we know a) is the more likely because the program is so small it probably won't be interleaved. However, b) is theoretically possible. What do you think they are after in an answer? Has anyone done SCJP?
thanks
john
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi John,
Welcome to JavaRanch, where there are no stupid posts
The right answer is, as you're already aware, B. I don't think this needs much further discussion; tests aren't generally looking for what happens most of the time, but instead for the correct answer.
There's a whole forum here (a very active one!) devoted to SCJP.


[Jess in Action][AskingGoodQuestions]
John Summers
Ranch Hand

Joined: Oct 06, 2003
Posts: 125
Thanks Ernest!
You confirmed what I thought, which is what I was after.
I'm sorry I'm suffering from PET (pre exam tension) and by 11 at night find myself giberring and barely even able to remember what an Object is...
I'll try the scjp forum
john
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two Threads same run method: SCJP questions
 
Similar Threads
Traps to be aware of in any SCJP test !!!!! :)
Hi all giving scjp 1.4 on this sat help needed
Thread help please!
A suggestion for list of Mock Questions
Threads : calculating minimum time, does the SCJP have such questions?