概述
Runnable 是接口。
Thread 是类,且实现了Runnable接口。
Thread部分源码
public class Threadimplements Runnable
{private static class Caches{static final ConcurrentMap subclassAudits = new ConcurrentHashMap();static final ReferenceQueue subclassAuditsQueue = new ReferenceQueue();
在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。
public interface Runnable
{public abstract void run();
}
Thread类,有一个构造方法:public Thread(Runnable targer)
public Thread(Runnable runnable){daemon = false;stillborn = false;threadLocals = null;inheritableThreadLocals = null;threadStatus = 0;blockerLock = new Object();init(null, runnable, (new StringBuilder()).append("Thread-").append(nextThreadNum()).toString(), 0L);}
此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。
使用情况
在程序开发中只要是多线程肯定永远以实现Runnable接口为主。
实现Runnable接口相比继承Thread类有如下好处:
1、避免继承的局限,一个类可以继承多个接口。
2、适合于资源的共享。
实例
以卖票为例,总共只有10张动车票了,全国3个窗口在卖。
继承Thread类的方法
package multithreading;public class MyThreadWithExtends extends Thread {private int tickets = 10;@Overridepublic void run() {for (int i = 0; i <= 100; i++) {if(tickets>0){System.out.println(Thread.currentThread().getName()+"--卖出票:" + tickets--);}}}public static void main(String[] args) {MyThreadWithExtends thread1 = new MyThreadWithExtends();MyThreadWithExtends thread2 = new MyThreadWithExtends();MyThreadWithExtends thread3 = new MyThreadWithExtends();thread1.start();thread2.start();thread3.start();//每个线程都独立,不共享资源,每个线程都卖出了10张票,总共卖出了30张。如果真卖票,就有问题了。}}
运行结果:
Thread-0--卖出票:10
Thread-2--卖出票:10
Thread-1--卖出票:10
Thread-2--卖出票:9
Thread-0--卖出票:9
Thread-2--卖出票:8
Thread-1--卖出票:9
Thread-2--卖出票:7
Thread-0--卖出票:8
Thread-2--卖出票:6
Thread-2--卖出票:5
Thread-2--卖出票:4
Thread-1--卖出票:8
Thread-2--卖出票:3
Thread-0--卖出票:7
Thread-2--卖出票:2
Thread-2--卖出票:1
Thread-1--卖出票:7
Thread-0--卖出票:6
Thread-1--卖出票:6
Thread-0--卖出票:5
Thread-0--卖出票:4
Thread-1--卖出票:5
Thread-0--卖出票:3
Thread-1--卖出票:4
Thread-1--卖出票:3
Thread-1--卖出票:2
Thread-0--卖出票:2
Thread-1--卖出票:1
Thread-0--卖出票:1
每个线程都独立,不共享资源,每个线程都卖出了10张票,总共卖出了30张。如果真卖票,就有问题了。
实现Runnable接口方式
package multithreading;public class MyThreadWithImplements implements Runnable {private int tickets = 10;@Overridepublic void run() {for (int i = 0; i <= 100; i++) {if(tickets>0){System.out.println(Thread.currentThread().getName()+"--卖出票:" + tickets--);}}}public static void main(String[] args) {MyThreadWithImplements myRunnable = new MyThreadWithImplements();Thread thread1 = new Thread(myRunnable, "窗口一");Thread thread2 = new Thread(myRunnable, "窗口二");Thread thread3 = new Thread(myRunnable, "窗口三");thread1.start();thread2.start();thread3.start();}}
运行结果:
窗口二--卖出票:10
窗口三--卖出票:9
窗口一--卖出票:8
窗口三--卖出票:6
窗口三--卖出票:4
窗口三--卖出票:3
窗口三--卖出票:2
窗口三--卖出票:1
窗口二--卖出票:7
窗口一--卖出票:5
每个线程共享了对象myRunnable的资源,卖出的总票数是对的,但是顺序是乱的,怎么办?
见:同步关键词synchronized