• 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

Which method??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
incandescent light gives off an efficient form of heat. You must be THIS smart to ride this ride. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic