多线程之isAlive()方法

一:简介

                  方法isAlive()的功能是判断当前的线程是否处于活动状态。什么是活动状态呢?
          活动状态就是线程已经启动且尚未终止。线程处于正在运行或准备开始运行的状态,
          就认为线程是“存活”的。

二:继承Thread线程类实例Demo如下

  1. 创建继承Thread的线程类MyThread
         
    package com.kgf.test;
    
    public class MyThread extends Thread {
    
    	@Override
    	public void run() {
    		System.out.println("run-----------BEGIN");
    		
    		System.out.println("this.getName()= "+this.getName());
    		System.out.println("this.isAlive()= "+this.isAlive());
    		
    		System.out.println("Thread.currentThread().getName()= "
    		        +Thread.currentThread().getName());
    		System.out.println("Thread.currentThread().isAlive()= "
    		        +Thread.currentThread().isAlive());
    		
    		System.out.println("run-----------END");
    	}
    	
    }
    

     

  2. 创建测试类
      
    package com.kgf.test;
    
    public class Run {
    
    	public static void main(String[] args) {
    		MyThread myThread = new MyThread();
    		System.out.println("begin=="+myThread.isAlive());
    		myThread.start();
    		System.out.println("end=="+myThread.isAlive());
    	}
    }
    

     

  3.  效果
      
    begin==false
    end==true
    run-----------BEGIN
    this.getName()= Thread-0
    this.isAlive()= true
    Thread.currentThread().getName()= Thread-0
    Thread.currentThread().isAlive()= true
    run-----------END
    

     

  4. 分析结果
            a:
                  begin==false
                  end==true

                  刚开始是false,表示我们仅仅只是创建了线程类MyThread对象,但是并未启动线程,所以
                  线程处于未活动状态。那么第二个为什么为true呢?这个值是不确定的,打印值为true的原因
                  是因为myThread线程还未执行完毕,所以输出true。如果我们将测试代码修改如下:
                   
                  最后结果:end==false,原因是myThread线程对象在1秒之内执行完毕了。
            b:
                  this.getName()= Thread-0
                  this.isAlive()= true
                  Thread.currentThread().getName()= Thread-0
                  Thread.currentThread().isAlive()= true

                   this.getName():
                         
    this它代表的其实就是MyThread类对象的引用,是个线程类(它继承了Thread线程类),
                   但是这个线程类并没有设置名字,所以Thread默认给了一个Thread-0
                   this.isAlive()
                         
    我们使用的是myThread线程对象启动的线程,所以线程是处于活动状态的。
                   Thread.currentThread().getName() 
                           Thread.currentThread()和this是相同的,因为我们启动用的是MyThread对象
                    这个引用,而this代表的就是MyThread类对象的引用。 
  5. 修改测试类,我们将线程对象以构造参数的方式传递给Thread对象去进行start()启动。
        a:修改继承Thread线程类的MyThread类
                
    package com.kgf.test;
    
    public class MyThread extends Thread {
    
    	public MyThread() {
    		System.out.println("构造方法-----------BEGIN");
    		
    		System.out.println("this.getName()= "+this.getName());
    		System.out.println("this.isAlive()= "+this.isAlive());
    		
    		System.out.println("Thread.currentThread().getName()= "
    		        +Thread.currentThread().getName());
    		System.out.println("Thread.currentThread().isAlive()= "
    		        +Thread.currentThread().isAlive());
    		
    		System.out.println("构造方法-----------END");
    	}
    	
    	@Override
    	public void run() {
    		System.out.println("run-----------BEGIN");
    		
    		System.out.println("this.getName()= "+this.getName());
    		System.out.println("this.isAlive()= "+this.isAlive());
    		
    		System.out.println("Thread.currentThread().getName()= "
    		        +Thread.currentThread().getName());
    		System.out.println("Thread.currentThread().isAlive()= "
    		        +Thread.currentThread().isAlive());
    		
    		System.out.println("run-----------END");
    	}
    	
    }
    

        b:测试类
                
    package com.kgf.test;
    
    public class Run {
    
    	public static void main(String[] args) throws InterruptedException {
    		MyThread myThread = new MyThread();
    		Thread thread = new Thread(myThread,"A");
    		System.out.println("main begin thread isAlive="+thread.isAlive());
    		thread.start();
    		System.out.println("main end thread isAlive="+thread.isAlive());
    	}
    }
    

        c:效果
                
    构造方法-----------BEGIN
    this.getName()= Thread-0
    this.isAlive()= false
    Thread.currentThread().getName()= main
    Thread.currentThread().isAlive()= true
    构造方法-----------END
    main begin thread isAlive=false
    main end thread isAlive=true
    run-----------BEGIN
    this.getName()= Thread-0
    this.isAlive()= false
    Thread.currentThread().getName()= A
    Thread.currentThread().isAlive()= true
    run-----------END
    

        d: 上面的结果理解了this和Thread.currentThread的差异,就明白了
        e:分析原因
               ①构造方法中打印结果分析
                    构造方法-----------BEGIN
                            this.getName()= Thread-0
                            this.isAlive()= false
                            Thread.currentThread().getName()= main
                            Thread.currentThread().isAlive()= true

                    构造方法-----------END
                    首先我们这个构造方法打印的结果,是我们在测试类中创建MyThread线程对象
                    时调用的无参构造造成的结果。this它代表的其实就是MyThread类对象的引用,
                    是个线程类(它继承了Thread线程类),但是这个线程类并没有设置名字,
                    所以Thread默认给了一个Thread-0,并且目前只是初始化MyThread对象,所以
                    线程并没有运行this.isAlive()=false,而目前是主线程正在运行,所以后面的两个
                    结果是main和true,
                    
               ② run方法中打印结果分析    
                     this.getName()= Thread-0
                     this.isAlive()= false
                     Thread.currentThread().getName()= A
                     Thread.currentThread().isAlive()= true 

                     由于我们并不是使用MyThread对象引用启动的线程,所以造成上面这样的打印结果。
                     具体原因还是this和Thread.currentThread的差异造成的。           
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值