• 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

Threads

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Q
-------
What will be the o\p n y???
--------
class B implements Runnable
{
int i = 10;
public void run()
{
i = 20;
}
}
public class H
{
public static void main(String [] argv)
{
B b = new B();
Thread t = new Thread(b);
b.i = 30;
System.out.println("Value is " + b.i);
}
}
---------------
Want the thread alter the value of i???
Gunjan
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I feel the out put should be 30 only.Because ur not calling the instanciating the thread.i,e thread will start executing only when you call t.start().
I feel the out put should be 30.
Thiru
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order for the thread to alter the value of i it's start method has to be called. t.start().
 
GK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gunjan kuwadia:
My Q
well freinds a mistake , making changes in the code
-------
What will be the o\p n y???
--------
class B implements Runnable
{
int i = 10;
public void run()
{
i = 20;
}
}
public class H
{
public static void main(String [] argv)
{
B b = new B();
Thread t = new Thread(b);
t.start();
b.i = 30;
System.out.println("Value is " + b.i);
}
}
---------------
Want the thread alter the value of i???
Gunjan


 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi gunjan,
(I have already replied to u for both cases in Threads and synchronisation section!!!pasting it here)
It will be simply 30 coz after you call
Thread t = new Thread(b);
a new thread is not started unless you write t.start();
Now If you start the thread(t.start()) it will still print 30 unless you stop main thread for some time after b.i=30;(say by calling sleep).Then it will print 20;
It is main that is still running and start() just makes the other thread ready to start not start .Actually it depends on Thread Sheduler to decide which thread should use the cpu.
I think since both have same priority the main thread continues to use the cpu .

 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with 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