java多线程(2)

获取线程名 getName()

调用这个方法有两种方式,一种是通过线程的Thread类的本身对象this.getName(),另一种是Thread.currentThread(),
this.getName() 是获取继承Thread对象的属性,也就是说谁继承的Thead,名字就是谁,默认是 Thread-0
Thread.currentThread() 是获取当前线程的属性,当前获取了cpu执行时间片段的线程的属性。

public class MyThread extends Thread {


    public MyThread(){
        System.out.println(" Mythread this name: "+this.getName());
        System.out.println(" Mythread Thread name: "+Thread.currentThread().getName());
    }

    @Override
    public void run() {
        super.run();
        System.out.println(" run this name: "+this.getName());
        System.out.println(" run Thread name: "+Thread.currentThread().getName());
    }
}

测试方法:
public class MyThreadTest {

    @Test
    public void threadTest(){
        MyThread myThread = new MyThread();
        myThread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

结果:
 Mythread this name: Thread-0
 Mythread Thread name: main
 run this name: Thread-0
 run Thread name: Thread-0

可以看出 MyThread 执行构造函数的时候 实际运行的线程是 main 线程,this.getName() 只是获取MyThread对象的属性,Thread.currentThread()才是获取当前执行线程的属性
当执行start()方法之后,run()进入线程运行阶段,获取cpu执行片段的变成 myThread(默认名是Thread-0) 线程,所以Thread.currentThread().getName() 是 Thread-0

判断线程是都存活 isAlive()

我们可以通过isAlive(),判断线程是否存活。
先给列子

public class ThreadIsAlive extends Thread {
    @Override
    public void run() {
        super.run();
        System.out.println("thread name "+this.getName()+" "+this.isAlive());
        System.out.println("thread name "+Thread.currentThread().getName()+" "+Thread.currentThread().isAlive());
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

测试方法:

public void testAlive(){
        ThreadIsAlive threadIsAlive = new ThreadIsAlive();
        Thread.currentThread().isAlive();
        System.out.println("thread 对象名 "+threadIsAlive.getName()+" "+threadIsAlive.isAlive());
        threadIsAlive.start();
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("thread 对象名 ===="+threadIsAlive.getName()+" "+threadIsAlive.isAlive());

        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("线程结束 thread 对象名 "+threadIsAlive.getName()+" "+threadIsAlive.isAlive());
}
结果:
thread 对象名 Thread-0 false
thread name Thread-0 true
thread name Thread-0 true
thread 对象名 ====Thread-0 true
线程结束 thread 对象名 Thread-0 false

在start() 方法之前实行 threadIsAlive 是false 说明线程的状态还不是存活状态,主线程阻塞 300毫秒,子线程阻塞 500 毫秒,这时候在去判断,则得到是true,说明子线程是存活的
最后子线程结束,又变成死忙状态

注意:用 Thread.currentThread() 去调 isAlive 和用对象 threadIsAlive 去调 在不同区间 是不一样的。Thread.currentThread() 永远是判断当前线程,

  /**
     * Returns a reference to the currently executing thread object.
     *
     * @return  the currently executing thread.
     */
    public static native Thread currentThread();

    注释的意思是返回当前正在执行的线程的对象的引用

在 run() 方法中 用Thread.currentThread().isAlive() ,当前运行的线程是 threadIsAlive,所以判断的是threadIsAlive 线程的存活状态,
但是 如果在 测试方法中用 Thread.currentThread().isAlive() 则这个就判断的是 测试方法的线程的存活状态了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值