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 JQ+  Question 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 "JQ+  Question" Watch "JQ+  Question" New topic
Author

JQ+ Question

Anon Ning
Greenhorn

Joined: Mar 09, 2001
Posts: 19
hi all:
See this code:
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?
a) 5
b) 10
c) It will not compile
d) Exception at runtime
e) The output can not be determined
I choosed a)5. However, the correct answer is: e) The output can not be determined. I don't know why. I think according to Java's "Pass by Value" theory,it always print 5.
Please help me.
Thanks.
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
hi!
I compiled the code and the output was 5. I don't understand this.In this code when we call the start method it will be calling the run method.In the run() method we are accessing the reference variable x by this. So it should give the value of 10.
certainly the answer e is wrong. Please explain me what i am missing
hi all:
See this code:
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?
a) 5
b) 10
c) It will not compile
d) Exception at runtime
e) The output can not be determined
I choosed a)5. However, the correct answer is: e) The output can not be determined. I don't know why. I think according to Java's "Pass by Value" theory,it always print 5.
Please help me.
Thanks.

Philosopher
Greenhorn

Joined: Mar 15, 2001
Posts: 6
Actually, when you call the start() method of the object tc, the object goes to the Ready state and waits for CPU to be allocated to it. So if the CPU gets allocated promptly, the new thread executes the run() method and changes the value of x, in this case you will get the value 10. Now look at the other case, if the new Thread based on tc doesn't get the CPU immediately, the current thread which was executing main method already keeps on going and executes the printout statement. In this case, it prints the value 5 because run() method is not executed yet.
Anon Ning
Greenhorn

Joined: Mar 09, 2001
Posts: 19
Thanks Philosopher.I got it. When I change the abovement code into the following code , I get the value 10.
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
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
System.out.println(tc.x);
}
}
The output is 10.
That means when the current thread(main thread) is sleeping,the another thread which was ready to run is executed. So the value is changed to 10.Therefore,the correct answer is e). Is that right?
Mudassar Shafique
Greenhorn

Joined: Mar 16, 2001
Posts: 10
Yes, you got it:-)
PS: I am Philosopher, I changed my name as per suggested by some of moderators.
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
in my opinion the reason why the answer is 'e' :
because there are two thread running in the same time, tc & main(),
JVM can not guarantee which thread will be the first, so it could be tc, or it could be main(), and hence the output cannot be determine .
Ishaan Mohan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
Yes Steivie is right and also the given answer(e). As its on JVM that which thread get execution first. You can not predict output just running once.
Pam Doucette
Greenhorn

Joined: Mar 13, 2001
Posts: 27
I'm thinking the only way to guarantee that you print 10 is if you move System.out.println(x); into the run() method
Joseph Russell
Ranch Hand

Joined: Jan 08, 2001
Posts: 290
Good question. Thanks for the input... It's somehthing that I haven't even considered yet. It will be handy to know as I am preparing for the exam.
Joe
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JQ+ Question
 
Similar Threads
Thread question......
Threads
Which method??
jq+ question
Thread ques.