Thread和Runnable
1、简介
Runnable是一个接口,该接口中只包含了一个run()方法。它的定义为:
public interface Runnable { public abstract void run() }
Runnable的作用为实现多线程。我们可以定义一个类A,实现Runnable接口;然后通过new Thread(new A())方式新建线程
Thread是一个类,它背身就实现了Runnable接口。它的声明为:
public class Thread implements Runnable{}
Thread类的作用,也是实现多线程
2、Thread和Runnable的异同点
相同点:都是“多线程”的实现方式
不同点:(1)、Thread是类,而Runnable是接口;Thread本身是实现了Runnable接口的类。而一个类只能继承一个父类,却可以实现多个接口,因此Runnable具有更好的扩展性;(2)、Runnable还可以用于“资源共享”,即,多个线程都是基于某一个Runnable对象建立的,它们会共享Runnable对象上的资源。
3、Thread的多线程示例
class MyThread extends Thread { private int ticket = 10; public void run() { for(int i = 0;i < 20;i++) { if(this.ticket > 0) System.out.println(this.getName()+" sells a ticket. Before this we have:"+this.ticket--); } } } public class problem1 { public static void main(String[] args) { MyThread t1 = new MyThread(); MyThread t2 = new MyThread(); MyThread t3 = new MyThread(); t1.start(); t2.start(); t3.start(); } }
运行结果为:
Thread-1 sells a ticket. Before this we have:10 Thread-0 sells a ticket. Before this we have:10 Thread-0 sells a ticket. Before this we have:9 Thread-0 sells a ticket. Before this we have:8 Thread-0 sells a ticket. Before this we have:7 Thread-0 sells a ticket. Before this we have:6 Thread-0 sells a ticket. Before this we have:5 Thread-0 sells a ticket. Before this we have:4 Thread-0 sells a ticket. Before this we have:3 Thread-0 sells a ticket. Before this we have:2 Thread-2 sells a ticket. Before this we have:10 Thread-2 sells a ticket. Before this we have:9 Thread-2 sells a ticket. Before this we have:8 Thread-2 sells a ticket. Before this we have:7 Thread-2 sells a ticket. Before this we have:6 Thread-2 sells a ticket. Before this we have:5 Thread-2 sells a ticket. Before this we have:4 Thread-2 sells a ticket. Before this we have:3 Thread-2 sells a ticket. Before this we have:2 Thread-2 sells a ticket. Before this we have:1 Thread-0 sells a ticket. Before this we have:1 Thread-1 sells a ticket. Before this we have:9 Thread-1 sells a ticket. Before this we have:8 Thread-1 sells a ticket. Before this we have:7 Thread-1 sells a ticket. Before this we have:6 Thread-1 sells a ticket. Before this we have:5 Thread-1 sells a ticket. Before this we have:4 Thread-1 sells a ticket. Before this we have:3 Thread-1 sells a ticket. Before this we have:2 Thread-1 sells a ticket. Before this we have:1
结果说明:
(1)、MyThread类继承于Thread类,它是自定义的线程。每个MyThread会卖出10张票
(2)、主线程main创建并启动了3个MyThread子线程。每个子线程都各自卖出了10张票
4、Runnable的多线程示例
class MyThread implements Runnable { private int ticket = 10; public void run() { for(int i = 0;i < 20;i++) { if(this.ticket > 0) System.out.println(Thread.currentThread().getName() + " sells a ticket. Before this we have:"+this.ticket--); } } } public class problem1 { public static void main(String[] args) { MyThread mt = new MyThread(); Thread t1 = new Thread(mt); Thread t2 = new Thread(mt); Thread t3 = new Thread(mt); t1.start(); t2.start(); t3.start(); } }
运行结果为:
Thread-0 sells a ticket. Before this we have:10 Thread-0 sells a ticket. Before this we have:7 Thread-2 sells a ticket. Before this we have:8 Thread-2 sells a ticket. Before this we have:5 Thread-1 sells a ticket. Before this we have:9 Thread-2 sells a ticket. Before this we have:4 Thread-0 sells a ticket. Before this we have:6 Thread-2 sells a ticket. Before this we have:2 Thread-1 sells a ticket. Before this we have:3 Thread-0 sells a ticket. Before this we have:1
结果说明:
(1)、和上面的MyThread类继承于Thread类不同,这里的MyThread实现了Runnable接口
(2)、主线程main创建并启动了三个子线程,而且这三个子线程都是基于"mt"这个Runnable对象创建的。运行结果是这三个子线程一共卖出了10张票,这说明它们是共享资源的