aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Which method?? 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 "Which method??" Watch "Which method??" New topic
Author

Which method??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435

What will it print when run?
Options:
1)5
2)10
3)It will not compile
4)Exception at Runtime
5)Output cannnot be determined
Ans : 5
I feel since run method will be called, the answer should be 10
Please explain
Sonir
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Sonir :

When you call the start method a new thread object is handed over to the thread scheduler.
After this,the rest of the main method maycontinue execution. If this happens the value printed will be the original value of x, i.e. 5.
OR the thread schduler may start running the newly created thread in which case the value of x will be 10.
Based on the operating system's scheduling algorithms and on the other threads that are running/ready at the time you run this code, the output may differ.
HTH
[ January 17, 2002: Message edited by: Shivaji Marathe ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sonir,
after line 1 two threads will be running the user thread and the new thread that is created on line 1. That's why the answer is option 5, the output cannot be determined...
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
if the main method continues then x =5 is printed. this is understood.
But the part, when run method is executed in which this.x = 10; i guess x is different here. or is it referring to the x from the main method ? this is an object. right ? which object is it. i am not clear here.

Originally posted by Shivaji Marathe:
Sonir :

When you call the start method a new thread object is handed over to the thread scheduler.
After this,the rest of the main method maycontinue execution. If this happens the value printed will be the original value of x, i.e. 5.
OR the thread schduler may start running the newly created thread in which case the value of x will be 10.
Based on the operating system's scheduling algorithms and on the other threads that are running/ready at the time you run this code, the output may differ.
HTH
[ January 17, 2002: Message edited by: Shivaji Marathe ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
mark,
the way it goes is as follows.
At the beginning, the only thread we have is the one in which the main program runs.
TestClass tc = new TestClass();
creates a new TestClass object (which is by the a Runnable also). The member x of TestClass is initialized to 5.
new Thread(tc).start();
creates a new Thread with the previously created tc Runnable object and schedules it for execution.
From now on we have two threads. The member x will still have the value 5 until the newly created Thread executes (inside the run method). From that time on, x will be 10. But the thing is, we have no way to know when the newly created Thread will execute. Before or after the folowwing System.out.println invocation ?? We don't know.
If the new Thread executes before the println then x gets changed to 10 before being printed otherwise 5 is printed and then changed. We don;t know how it will be executed since it depends on the OS and the scheduler...
Finally, it is the same member x inside the main method (main thread) and inside the new Thread (tc), so one must pay special care when handling such shared members.
HIH
 
Consider Paul's rocket mass heater.
 
subject: Which method??