• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question on the topic 'thread' in a mock exam
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);
}
}
The ans given is 'output cannot be predicted'. Pl. explain to me..
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sanjana,
We can't predict the output because we can't guarantee when the method run() will be executed (after or before System.out.println(tc.x)).
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
Invoking a start() method on a thread object, this will move the thread to the running state, this will be in a pool of threads waiting to run. Here the Thread Scheduler will select a thread in pool for running, so which Thread will be selected depends on the Thread Scheduler
So invoking a Start method on a thread only says that thread moves from new state to ready state and may move to running state.
Hope this helps
regs
Vivek Nidhi
 
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic