Re: Query:multithread about java
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!