你的位置:首页 > 软件开发 > Java > 1.4 isAlive()方法

1.4 isAlive()方法

发布时间:2017-11-30 20:00:09
方法isAlive()是判断当前线程是否处于活动状态。线程代码:public class TestThread extends Thread{ @Override public void run() { System.out.println("run=" + ...

1.4 isAlive()方法

方法isAlive()是判断当前线程是否处于活动状态。

线程代码:

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

运行代码:

 

public class Main { public static void main(String[] args) {  TestThread tt = new TestThread();  System.out.println("Begin == " + tt.isAlive());  tt.start();  //这行代码的结果是不确定的,打印true表示tt线程还未执行完毕,打印false表示tt线程已经执行完毕。下面会给出结果为false的情况  System.out.println("end == " + tt.isAlive()); }}

 

运行结果:

1.4 isAlive()方法

方法isAlive()是测试线程是否处于活跃状态的方法,

活跃状态:线程已经启动且尚未终止。线程处于正在运行或准备开始运行的状态,就认为线程是“存活的”。

结果为false的执行代码:

public class Main { public static void main(String[] args) {  try {   TestThread tt = new TestThread();   System.out.println("Begin == " + tt.isAlive());   tt.start();   //通过这行代码使当前线程睡眠,让tt线程完成并结束   Thread.sleep(1000);   System.out.println("end == " + tt.isAlive());  } catch (Exception e) {   e.printStackTrace();  } }}  

执行结果:

1.4 isAlive()方法

 

在使用isAlive()方法时,如果将线程对象以构造参数的方式传递给Thread对象进行start()启动时,运行的结果和前面会有差异。产生差异的原因在于:Thread.currentThread()和this的差异。

测试线程代码:

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"); } @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"); }}

执行代码:

public class Main { public static void main(String[] args) {  countOperate co = new countOperate();  Thread t = new Thread(co);  System.out.println("main begin t isAlive = " + t.isAlive());  t.setName("A");  t.start();  System.out.println("main end t isAlive = " + t.isAlive()); }}

执行结果:

1.4 isAlive()方法

 

最后这个运行结果, 真的是惊了。我觉得run里面的this.isAlive()应该为true的,我认为this指代的就是开启的另一条线程,而main跟a其实都是主线程。后来发现不太对:

修改执行代码:

public class Main { public static void main(String[] args) {  countOperate co = new countOperate();  Thread t = new Thread(co);  System.out.println("main begin t isAlive = " + t.isAlive());  //t.setName("A");  t.start();  System.out.println("main end t isAlive = " + t.isAlive()); }}

执行结果:

1.4 isAlive()方法

然后我思考了一下:在个人的见解上认为了Thread.currentThread()获取的线程与this的线程的区别。

Thread.currentThread()获取的为当前运行的线程

    注:(在完成CountOperate的构造方法时,运行的是主线程,在调用start()时运行的是t线程。)。

this获取的为当前所在的线程

    注:(所以不论是构造方法中还是run方法中他都位于同一个线程,且这个线程从头到尾都没有被开启)。

源码地址:https://github.com/lilinzhiyu/threadLearning

 

本文内容是书中内容兼具自己的个人看法所成。可能在个人看法上会有诸多问题(毕竟知识量有限,导致认知也有限),如果读者觉得有问题请大胆提出,我们可以相互交流、相互学习,欢迎你们的到来,心成意足,等待您的评价。

 

 

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:1.4 isAlive()方法

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录