Runnable与Thread的区别
从别人的博客中看到的关于Runnable与Thread的区别,举了如下代码的例子:继承Thread:
public class ThreadTest {
public static void main(String[] args) {
MyThread m1 = new MyThread("一号窗口");
MyThread m2 = new MyThread("二号窗口");
MyThread m3 = new MyThread("三号窗口");
m1.start();
m2.start();
m3.start(); }
}
class MyThread extends Thread{
private int ticket = 10;
private String name;
MyThread(String s){
this.name = s;
}
public void run(){
for(int i=0;i<10;i++){
if(this.ticket > 0){
System.out.println(Thread.currentThread().getName()+"售票"+ticket--);
}
}
}
}
实现Runnable:
public class ThreadTest {
public static void main(String[] args) {
MyThread m1 = new MyThread();
Thread t1 = new Thread(m1,"一号窗口");
Thread t2 = new Thread(m1,"二号窗口");
Thread t3 = new Thread(m1,"三号窗口");
t1.start();
t2start();
t3start();
}
}
class MyThread implements Runnable{
private int ticket = 10;
private String name;
MyThread(String s){
this.name = s;
}
public void run(){
for(int i=0;i<10;i++){
if(this.ticket > 0){
System.out.println(Thread.currentThread().getName()+"售票"+ticket--);
}
}
}
}
解释说Runnable是三个人可以完成一件事,Thread是个人做三件事,不知道说的对不对,总感觉不大对,求解释!!!!
[此贴子已经被作者于2016-4-28 22:34编辑过]