本文共 3781 字,大约阅读时间需要 12 分钟。
/** * 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(); }}
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()); }}
/** * 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构造方法BeginThread.currentThread().getName()=mainThread.currentThread().isAlive()=truethis.getName()=Thread-0this.isAlive=()falseCountOperate构造方法Endmain begin thread isAlive=falsemain end thread isAlive=trueRun方法BeginThread.currentThread().getName()=AAAThread.currentThread().isAlive()=truethis.getName()=Thread-0this.isAlive()=falseRun方法EndProcess finished with exit code 0
分析问题就是我们将线程对象已构造参数的形式传递给Thread对象时进行start启动时, 使用Thread.currentThread()和this或者的信息是不一样的, 这也证实我们CurrentThread方法的使用, 因为我们是将一个线程对象传递给一个线程thread, 那我们启动的时候启动的是thread这个线程, 执行的是被传递的这个线程的run方法, 所以我们在这个对象中使用this的时候, 获得的是这个没有被启动的线程对象的信息, 使用Thread.currentThread的时候, 获得的是调用这个代码段的线程的信息, 也就是thread的信息
转载地址:http://ijsci.baihongyu.com/