zoukankan      html  css  js  c++  java
  • 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());
        }
    }

    运行结果:

    方法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();
            }
        }
    }    

    执行结果:

    在使用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());
        }
    }

    执行结果:

    最后这个运行结果, 真的是惊了。我觉得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());
        }
    }

    执行结果:

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

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

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

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

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

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

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

     

  • 相关阅读:
    leetcode-hard-array-454 4sum II-NO
    leetcode-hard-array-238. Product of Array Except Self-NO
    leetcode-hard-array-54. Spiral Matrix-NO
    leetcode-easy-trees-98. Validate Binary Search Tree-NO
    leetcode-easy-trees-108. Convert Sorted Array to Binary Search Tree
    leetcode-easy-trees-102. Binary Tree Level Order Traversal-YES
    leetcode-easy-trees-101. Symmetric Tree-YES
    leetcode-easy-trees-Maximum Depth of Binary Tree
    leetcode-easy-others-20 Valid Parentheses
    深度学习变革视觉计算总结(CCF-GAIR)
  • 原文地址:https://www.cnblogs.com/lilinzhiyu/p/7941121.html
Copyright © 2011-2022 走看看