• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

JQ+ Question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you got it:-)
PS: I am Philosopher, I changed my name as per suggested by some of moderators.
 
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm thinking the only way to guarantee that you print 10 is if you move System.out.println(x); into the run() method
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic