I'm a newbie to java and I have the following Q -
class mythread implements Runnable {
.
.
.
}
scenario 1
mythread my = new mythread();
Thread t1 = new Thread(my);
Thread t2 = new Thread(my);
t1.start();
t2.start();
scenario 2
mythread my1 = new mythread();
mythread my2 = new mythread();
Thread t1 = new Thread(my1);
Thread t2 = new Thread(my2);
t1.start();
t2.start();
Whats the difference b/w scenario 1 and 2 ?. I'm kind of confused here.
Debugger shows me that two threads are created in both scenarios.
-j
VisionSet - 29 May 2006 18:48 GMT
> scenario 1
>
[quoted text clipped - 15 lines]
> Whats the difference b/w scenario 1 and 2 ?. I'm kind of confused here.
> Debugger shows me that two threads are created in both scenarios.
1/ You create a single instance and run it as 2 threads. So changes made to
instance variables in one thread will be seen by the other thread.
2/ You create two instances of the same class and run them as 2 separate
threads. So instance variables are not shared, but static members of course
still will be.
--
Mike W