aspose file tools*
The moose likes Threads and Synchronization and the fly likes Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads " Watch "Threads " New topic
Author

Threads

GK
Greenhorn

Joined: Jan 25, 2001
Posts: 26
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);
t.start();//Added
b.i = 30;
System.out.println("Value is " + b.i);
}
}
---------------
Want the thread alter the value of i???
Gunjan

[This message has been edited by gunjan kuwadia (edited February 24, 2001).]
sandeep bagati
Ranch Hand

Joined: Feb 22, 2001
Posts: 62

Hi gunjan
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 .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads