对线程的一些理解

先看一下面这段代码 

package com.xwiam.threadprogram;

/**
 * @author xiwam
 * @Date 2018/11/10 11:12
 * @Desc
 */
public class ThreadInterrupt implements Runnable{

    private volatile int count;

    private final Object object = new Object();

    public void run() {
        test();
    }

    public void test() {
        synchronized (object) {
            count++;
            count++;
            System.out.println("count ===" + count);
        }
    }


    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(new ThreadInterrupt());
        thread.setName("interrupt thread:");
        ThreadTest test = new ThreadTest(thread);
        Thread thread1 = new Thread(test);
        thread1.setName("test thread:");
        thread.start();
        thread1.start();
    }

}

class ThreadTest implements Runnable {

    private Thread thread;

    public ThreadTest(Thread thread) {
        this.thread = thread;
    }

    public void run() {
        thread.run();
    }
}

这段代码会打印什么,如果你运行main()方法的话,会发现打印结果如下:

count = 2
count = 4

但是你多运行几次main()方法的话,会发现打印结果可能如下:

count = 2

为什么会出现这种情况呢?首先得了解下线程的生命周期。

线程从开始到结束有6种状态。

new  state:新建一个线程,这时候线程处于就绪状态,run()方法中的代码还没有执行。

runnable state:线程可能已经在运行或者在等待运行。新建线程在调用start()方法后即进入线程调度的线程池中,状态从新建状态变成runnable状态,每个处于runnable状态的线程都需要线程调度器根据调度算法进行调度分配运行时间。

blocked state:线程在等待monitor锁,比如在进入synchronized块或者方法之前需要获取对应的对象锁,如果这时对象锁已经被其他线程获取了,当前线程就只能阻塞在这儿,等待对象锁被释放然后再去竞争对象锁。

waiting state:线程处于等待状态,等待另一个线程的action。线程进入等待状态有三种情况:Object.wait()、Thread.join()、LockSupport.spark()。其中thread.join(),如果看源码的话其实最终调用了Object.wait(0),而Obejct即thread对象,本质上和Object.wait()一样。

public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

time waiting state:线程处于等待状态直到超时、被唤醒或者被打断。比如线程处于sleep状态,另一个线程调用interrupt()方法,会让当前线程结束睡眠状态,抛出InterruptedException异常。

terminated state:线程run()方法执行完成,或者出现不能处理的异常,比如段错误。

了解完了线程的生命周期,回头看这段代码。这段代码为什么会出现第二种情况呢?首先,thread1对test线程有依赖,而test线程对thread有依赖,因而thread1间接地对thread有依赖。由于thread.run()方法执行的时间很短,所以thread1线程在执行run()方法时,很可能thread线程已经执行结束了,进入terminated state状态。虽然thread1线程有对thread线程的引用,但是thread线程已经进入死亡状态。看jdk源码的话,Thread.run()会去判断target字段是否为null,由于thread线程已经进入死亡状态,所以这时候target为null,run()方法将不会执行。count = 4 也就没有打印出来。

public void run() {
        if (target != null) {
            target.run();
        }
    }

多线程编程的话,对线程的生命周期一定要理解透彻,我还只是了解皮毛,并没有深入到JVM中去,后续继续深挖。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值