thread help

J

jammytech

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
 
V

VisionSet

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top