Thank you so much!Ramesh!
Would you mind doing me a favour again?
There are two Demos using different way to creat a thread,one is by
inheriting class Thread while another is by implementing interface
Runnable,but the results of these two examples are quite different.
Demo1:
class MultiThread4 implements Runnable{
private int ticket = 100;
public void run(){
while(ticket>0)
System.out.println(ticket-- +"is saled by " +
Thread.currentThread().getName());
}
}
class MultiThreadDemo4{
public static void main(String[] name){
MultiThread4 m =new MultiThread4();
Thread t1 = new Thread(m,"Window 1");
Thread t2 = new Thread(m,"Window 2");
Thread t3 = new Thread(m,"Window 3");
t1.start();
t2.start();
t3.start();
}
}
Demo2:
class NMultiThread4 extends Thread{
NMultiThread4(String name){
super(name);
}
private int ticket = 100;
public void run(){
while(ticket>0)
System.out.println(ticket-- +"is saled by " +
Thread.currentThread().getName());
}
}
class NMultiThreadDemo4{
public static void main(String[] name){
NMultiThread4 t1 = new NMultiThread4("Window 1");
NMultiThread4 t2 = new NMultiThread4("Window 1");
NMultiThread4 t3 = new NMultiThread4("Window 1");
t1.start();
t2.start();
t3.start();
}
}
Why?
Thanks in advance!
Lew - 03 May 2007 16:37 GMT
> There are two Demos using different way to creat a thread,one is by
> inheriting class Thread while another is by implementing interface
[quoted text clipped - 20 lines]
> }
> }
Your Runnable example reuses the same Runnable object for all threads. Your
Thread example did not do that.
Try this instead:
class MultiThreadDemo4
{
public static void main(String[] name)
{
Thread t1 = new Thread( new MultiThread4(), "Window 1" );
Thread t2 = new Thread( new MultiThread4(), "Window 2" );
Thread t3 = new Thread( new MultiThread4(), "Window 3" );
t1.start();
t2.start();
t3.start();
}
}

Signature
Lew
Jack Dowson - 03 May 2007 17:12 GMT
Thank you very much!Lew!
I've tried and it's the same result as before modified!
> Your Runnable example reuses the same Runnable object for all threads.
> Your Thread example did not do that.
Why?
Ramesh - 04 May 2007 07:53 GMT
> > Your Runnable example reuses the same Runnable object for all threads.
You have only a single Runnable object created and that objects
method(run()) is called from three threads. And there is only a single
shared variable 'ticket' of that same Runnable object for all the
threads.
> Why?
So the three threads decrement the same 'ticket' variable and this
runs 1/3 times faster(the 3 threads combined, needs to decrement the
'ticket' only a hundred times) than the way where you create a new
runnable object for each thread(each thread need to decrement its own
Runnable objects 'ticket' variable 100 times). During the latter case
each runnable object has its own 'ticket' variable and this is passed
on to each of the thread and it is not shared.
These are some of the rudimentary concepts in Java. If you are not
familiar with these concepts I recommend you to get a copy of 'The
Java Programming Language' by James Gosling et al.
Thanks,
Ramesh