Java~在使用isAlive时, 将线程对象已构造参数的形式传递给Thread对象时进行start启动时, 使用this和Thread.currentThread的差异

currentThread()方法

  • 该方法可返回代码段正在被哪个线程调用的信息
  • 代码示例
/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-09-28
 * Time: 21:54
 */
public class CurrentThreadTest extends Thread{
    
    


    public CurrentThreadTest() {
    
    
        System.out.println("构造方法name:" + Thread.currentThread().getName());
    }

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

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-09-28
 * Time: 21:55
 */
public class Run {
    
    

    public static void main(String[] args) {
    
    
        CurrentThreadTest threadTest = new CurrentThreadTest();
        System.out.println("main方法name:" + Thread.currentThread().getName());
        threadTest.start();
    }
}

在这里插入图片描述

  • 在构造方法中输出的依旧是mian 但是在run方法输出的就是thread-0

isAlive()方法

  • 判断当前线程是否处于活动状态
  • 代码示例
public class IsAliveTest extends Thread {
    
    

    @Override
    public void run() {
    
    
        System.out.println("run=" + this.isAlive());
    }
}
public class Run2 {
    
    

    public static void main(String[] args) throws InterruptedException {
    
    
        IsAliveTest test = new IsAliveTest();
        System.out.println("begin=" + test.isAlive());
        test.start();
        Thread.sleep(100);
        System.out.println("end=" + test.isAlive());
    }
}

在这里插入图片描述

使用this和Thread.currentThread的差异

  • 代码
/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-09-28
 * Time: 22:07
 */
public class CountOperate extends Thread {
    
    

    public CountOperate() {
    
    
        System.out.println("CountOperate构造方法Begin");
        System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());
        System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive());
        System.out.println("this.getName()=" + this.getName());
        System.out.println("this.isAlive()=" + this.isAlive());
        System.out.println("CountOperate构造方法End");
        System.out.println();
    }

    @Override
    public void run() {
    
    
        System.out.println("Run方法Begin");
        System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());
        System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive());
        System.out.println("this.getName()=" + this.getName());
        System.out.println("this.isAlive()=" + this.isAlive());
        System.out.println("Run方法End");
        System.out.println();
    }
}

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-09-28
 * Time: 22:13
 */
public class Run3 {
    
    

    public static void main(String[] args) {
    
    
        CountOperate countOperate = new CountOperate();
        Thread thread = new Thread(countOperate);
        System.out.println("main begin thread isAlive=" + thread.isAlive());
        thread.setName("AAA");
        thread.start();
        System.out.println("main end thread isAlive=" + thread.isAlive());
    }
}

CountOperate构造方法Begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName()=Thread-0
this.isAlive=()false
CountOperate构造方法End

main begin thread isAlive=false
main end thread isAlive=true

Run方法Begin
Thread.currentThread().getName()=AAA
Thread.currentThread().isAlive()=true
this.getName()=Thread-0
this.isAlive()=false
Run方法End


Process finished with exit code 0

分析问题就是我们将线程对象已构造参数的形式传递给Thread对象时进行start启动时, 使用Thread.currentThread()和this或者的信息是不一样的, 这也证实我们CurrentThread方法的使用, 因为我们是将一个线程对象传递给一个线程thread, 那我们启动的时候启动的是thread这个线程, 执行的是被传递的这个线程的run方法, 所以我们在这个对象中使用this的时候, 获得的是这个没有被启动的线程对象的信息, 使用Thread.currentThread的时候, 获得的是调用这个代码段的线程的信息, 也就是thread的信息

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/108858620
今日推荐