Thread和Runnable的异同点
Thread 和 Runnable 的相同点:都是“多线程的实现方式”。
Thread 和 Runnable 的不同点:
Thread 是类,而Runnable是接口;Thread本身是实现了Runnable接口的类。我们知道“一个类只能有一个父类,但是却能实现多个接口”,因此Runnable具有更好的扩展性。
此外,Runnable还可以用于“资源的共享”。即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。
通常,建议通过“Runnable”实现多线程!
Demo1:每个线程都买5张票
package thread;
public class ThreadDemo5 {
class MyThread extends Thread {
private int ticket = 5;
public void run() {
for (int i = 0; i < 5; i++) {
if (this.ticket > 0) {
System.out.println(this.getName() + " 卖票:ticket"
+ this.ticket--);
}
}
}
};
public static void main(String[] args) {
// 启动3个线程t1,t2,t3;每个线程各卖10张票!
MyThread t1 = new ThreadDemo5().new MyThread();
MyThread t2 = new ThreadDemo5().new MyThread();
MyThread t3 = new ThreadDemo5().new MyThread();
t1.start();
t2.start();
t3.start();
}
}
Demo2:三个线程总共卖五张票
Thread-0 卖票:ticket5
Thread-0 卖票:ticket4
Thread-0 卖票:ticket3
Thread-0 卖票:ticket2
Thread-0 卖票:ticket1