Java对象的线程状态

来源:互联网 发布:淘宝联盟官网登录网址 编辑:程序博客网 时间:2024/06/04 01:23

概述

在工作中,看到在一个单例模式的对象中,有获取线程中断状态的操作,然而这个对象又被多个线程访问,于是对同一个对象调用Thread.currentThread().interrupt()当前线程状态产生了好奇。

测试代码

  1. 两个单例模式的对象
  2. 均被两个线程调用

单例对象A的代码

public class SingleAAAObj {    //创建 SingleObject 的一个对象    private static SingleAAAObj instance = new SingleAAAObj();    //让构造函数为 private,这样该类就不会被实例化    private SingleAAAObj(){}    //获取唯一可用的对象    public static SingleAAAObj getInstance(){       return instance;    }    public void showThreadStatus(){        //打印对象hashcode,确定是同一个对象        System.out.println(this);        System.out.println(Thread.currentThread().getName() + "SingleObject isInterrupted:" + Thread.currentThread().isInterrupted());    } }

单例对象B的代码

public class SingleBBBObj {    private static SingleBBBObj singleBBBObj = new SingleBBBObj();    //B对象的单例中有一个A对象的单例    private SingleAAAObj singleAAAObj = SingleAAAObj.getInstance();    private SingleBBBObj(){}    public static SingleBBBObj getInstance(){        return singleBBBObj;    }    public SingleAAAObj getSingAAAObj() {        return singleAAAObj;    }    public void setSingAAAObj(SingleAAAObj singAAAObj) {        this.singleAAAObj = singAAAObj;    }}

测试用的线程类

public class TestThreadInterrupt implements Runnable{    public SingleBBBObj singleBBBObj;    public String name;    public TestThreadInterrupt(SingleBBBObj singleBBBObj, String name){        this.singleBBBObj = singleBBBObj;        this.name = name;    }    public void run(){        Thread.currentThread().setName(name);        System.out.println(Thread.currentThread().getName() + "before:");        //线程设置中断标志前查看线程中断状态        singleBBBObj.getSingAAAObj().showThreadStatus();        System.out.println(Thread.currentThread().getName() + "Set currentThread interrupt");        //线程设置中断标志        Thread.currentThread().interrupt();        System.out.println(Thread.currentThread().getName() + "after:");        //线程设置中断标志后查看线程中断状态        singleBBBObj.getSingAAAObj().showThreadStatus();        long nowTime = System.currentTimeMillis();        while(true){            //让线程执行5秒中            if(System.currentTimeMillis() - 5000 > nowTime){                break;            }        }        System.out.println("After 5 seconds Thread status:");        singleBBBObj.getSingAAAObj().showThreadStatus();    }}

测试程序

public class Main {    public static void main(String [] args){        //拿到B对象的单例,也就是拿到了A对象的单例        SingleBBBObj singleBBBObj = SingleBBBObj.getInstance();        TestThreadInterrupt tti1 = new TestThreadInterrupt(singleBBBObj, "111");        TestThreadInterrupt tti2 = new TestThreadInterrupt(singleBBBObj, "222");        Thread t1 = new Thread(tti1);        t1.start();        Thread t2 = new Thread(tti2);        try {            //让线程2先睡1秒,目的是让线程1先执行到“工作5秒”时,再启动线程2            t2.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        t2.start();    }}

结论

程序的执行结果:
111before:
SingleAAAObj@4059e324
111SingleObject isInterrupted:false
111Set currentThread interrupt
111after:
SingleAAAObj@4059e324
111SingleObject isInterrupted:true
222before:
SingleAAAObj@4059e324
222SingleObject isInterrupted:false
222Set currentThread interrupt
222after:
SingleAAAObj@4059e324
222SingleObject isInterrupted:true
After 5 seconds Thread status:
SingleAAAObj@4059e324
111SingleObject isInterrupted:true
After 5 seconds Thread status:
SingleAAAObj@4059e324
222SingleObject isInterrupted:true
从执行结果中看出来:
1. 当线程1的中断状态设置为true后,线程1继续工作,
2. 线程2自己独立的线程状态还是false,只有当线程2自己设置自己的线程中断标志为true时,才为true.
这个Demo证实了当Thread.currentThread().interrupt()时,对象和线程的状态是相互不影响的,影响的只是内存中同一个的那个对象。当然,这也是我把线程状态和对象状态搞混了。
https://github.com/wayss000/PracticeCode/tree/master/thread/threadStat

原创粉丝点击