jdk1.8源码Thread与Runnable区别

一、概念

Thread实现了Runnable接口

 public class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
} private volatile char name[];
//表示线程的优先级(最大值为10,最小值为1,默认值为5),
private int priority; /* Whether or not the thread is a daemon thread. */
//表示线程是否是守护线程,如果在main线程中创建了一个守护线程,
//当main方法运行完毕之后,守护线程也会随着消亡。在JVM中,垃圾收集器线程就是守护线程。
private boolean daemon = false; /* What will be run. */
//表示要执行的任务。
private Runnable target;
。。。。。。
}

二、创建,启动线程的方法有两种:

1,继承Thread

 class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
} public void run() {
// compute primes larger than minPrime
. . .
}
} PrimeThread p = new PrimeThread(143);
p.start();

2,实现Runnable

 class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
} public void run() {
// compute primes larger than minPrime
. . .
}
} PrimeRun p = new PrimeRun(143);
new Thread(p).start();

3,分析

但都调用thread的start()启动线程,API的注解是:

Causes this thread to begin execution; the Java Virtual Machine calls the <code>run</code> method of this thread.(使该线程开始执行;Java 虚拟机调用该线程的 run 方法。)

用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。

而thread的run()方法只是一个普通方法而已,API的注解是:

If this thread was constructed using a separate <code>Runnable</code> run object, then that <code>Runnable</code> object's <code>run</code> method is called;otherwise, this method does nothing and returns.(如果该线程是使用独立的 Runnable 运行对象构造的,则调用该 Runnable 对象的 run 方法;否则,该方法不执行任何操作并返回。)

如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。

 public class ThreadTest extends Thread{

     @Override
public void run() {
System.out.println("Thread Start !");
} public static void main(String[] args) {
ThreadTest threadTest = new ThreadTest();
threadTest.start();
System.out.println("不用等待!");
}
}

结果:

 不用等待!
Thread Start !
 public class ThreadTest extends Thread{

     @Override
public void run() {
System.out.println("Thread Start !");
} public static void main(String[] args) {
ThreadTest threadTest = new ThreadTest();
threadTest.run();
System.out.println("不用等待!");
}
}

结果:

 Thread Start !
不用等待!

参考博文链接:  https://www.cnblogs.com/renhui/p/6066750.html

三、关于卖票的问题:

1,extends Thread

 public class MyThread extends Thread {
private int tickets = 10;
@Override
public 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) {
MyThread thread1 = new MyThread();
MyThread thread2 = new MyThread();
MyThread thread3 = new MyThread(); thread1.start();
thread2.start();
thread3.start();
}
}

结果:

 Thread-0--卖出票:10
Thread-2--卖出票:10
Thread-2--卖出票:9
Thread-2--卖出票:8
Thread-2--卖出票:7
Thread-2--卖出票:6
Thread-1--卖出票:10
Thread-1--卖出票:9
Thread-2--卖出票:5
Thread-0--卖出票:9
Thread-0--卖出票:8
Thread-0--卖出票:7
Thread-0--卖出票:6
Thread-0--卖出票:5
Thread-0--卖出票:4
Thread-0--卖出票:3
Thread-0--卖出票:2
Thread-0--卖出票:1
Thread-2--卖出票:4
Thread-1--卖出票:8
Thread-2--卖出票:3
Thread-1--卖出票:7
Thread-2--卖出票:2
Thread-1--卖出票:6
Thread-2--卖出票:1
Thread-1--卖出票:5
Thread-1--卖出票:4
Thread-1--卖出票:3
Thread-1--卖出票:2
Thread-1--卖出票:1

问题:每个线程都独立,不共享资源,每个线程都卖出了10张票,总共卖出了30张。

2,implements Runnable

 public class MyRunnable implements Runnable {

     private int tickets = 64100;
@Override
public void run() {
for (int i = 0; i <= 64200; i++) {
if(tickets>0){
System.out.println(Thread.currentThread().getName()+"--卖出票:"+ tickets-- );
}
}
} public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread1 = new Thread(myRunnable, "窗口一");
Thread thread2 = new Thread(myRunnable, "窗口二");
Thread thread3 = new Thread(myRunnable, "窗口三"); thread1.start();
thread2.start();
thread3.start();
}
}

问题:每个线程共享了对象myRunnable的资源,但是当tickets足够大的时候就会出现一张票被卖出去多次的问题,原因是:读取共享变量tickets和减一这两个动作是原子操作,但这两个动作不可能用一条指令完成,一旦在这两个动作之间发生线程的切换,同一个值就会被读取2次,从而发生错误!

解决方案就是在方法上加锁:

 synchronized (this) {
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "--卖出票:" + tickets--);
}
}

四、拓展

关于Thread的详细源码剖析可参考:https://www.cnblogs.com/dennyzhangdd/p/7280032.html

关于Thread多次start一个线程报错:IllegalThreadStateException()

----------------------------------------------多做多解决多总结-----------------------------------

上一篇:Java 多线程实现方式二:实现 Runnable 接口


下一篇:“全栈2019”Java多线程第十章:Thread.State线程状态详解