Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which method??

 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic