1.含义
isAlive()的功能是判断当前的线程是否处于活动状态
2.代码例子
(1)在不同的线程运行同一个方法
package com.ray.deepintothread.ch01.topic_4;
public class IsAliveSample_1 {
public static void main(String[] args) {
ThreadFive threadFive = new ThreadFive();
Thread thread = new Thread(threadFive);
System.out.println("Thread name:" + Thread.currentThread().getName());
System.out.println("Thread isAlive:" + Thread.currentThread().isAlive());
thread.setName("myThread");// 设置运行线程的名称
threadFive.setName("threadFive");// 设置实例的名称
threadFive.myTest();
thread.start();
}
}
class ThreadFive extends Thread {
public void myTest() {
show();
}
private void show() {
System.out.println("----------begin----------");
// 这里指的是执行这个线程的名称
System.out.println("Thread name:" + Thread.currentThread().getName());
System.out.println("Thread isAlive:" + Thread.currentThread().isAlive());
// this.getName指的是这个实例的名称
// 由于是继承Thread,Thread本身可以设置名称
System.out.println("Instance name:" + this.getName());
System.out.println("Instance isAlive:" + this.isAlive());
System.out.println("----------end----------");
}
@Override
public void run() {
super.run();
show();
}
}
输出:
Thread name:main
Thread isAlive:true
----------begin----------
Thread name:main
Thread isAlive:true
Instance name:threadFive
Instance isAlive:false
----------end----------
----------begin----------
Thread name:myThread
Thread isAlive:true
Instance name:threadFive
Instance isAlive:false
----------end----------
解释:
(a)第一部分的输出,代表是main这一个线程的名字,还有代表main线程正在激活状态
(b)第二部分的输出,代表由main线程执行show方法,然后实例的名称是threadFive,但是threadFive这个实例所代表的线程并没有激活,所以isAlive=false
(c)第三部分的输出,代表由新建的线程myThread来执行show方法,然后实例的名称是threadFive,但是threadFive这个实例所代表的线程并没有激活,所以isAlive=false
(2)查看线程的状态
package com.ray.deepintothread.ch01.topic_4;
public class IsAliveSample_2 {
public static void main(String[] args) throws InterruptedException {
ThreadSix threadSix = new ThreadSix();
Thread thread = new Thread(threadSix);
System.out.println("thread.isAlive()" + thread.isAlive());
thread.start();
System.out.println("thread.isAlive()" + thread.isAlive());
Thread.sleep(1000);
System.out.println("thread.isAlive()" + thread.isAlive());
}
}
class ThreadSix extends Thread {
@Override
public void run() {
super.run();
}
}
输出:
thread.isAlive():false
thread.isAlive():true
thread.isAlive():false
这个例子主要目的,就是查看执行任务的线程的状态
从输出可以看出,第一个输出没有激活线程,第二个输出激活了线程,而第三个是由于线程在等待一秒之后才输出的,任务已经在这一秒里面已经执行完毕。
从并行的角度来说,上面由两个线程并行执行,一个是main,负责激活执行任务的线程和输出线程的状态,另一个是单独的执行任务的线程,由于在main线程里面等上了1秒,另一个线程在这1秒里面已经执行完毕所有的任务,因此才有第三个false的出现。
sleep():在指定的毫秒数内让当前“正在执行的线程”休眠(暂停执行)。这个“正在执行的线程”是指this.currentThread()返回的线程。
package mythread;
public class MyThread1 extends Thread {
@Override
public void run() {
try {
System.out.println("run threadName="
+ this.currentThread().getName() + " begin");
Thread.sleep(2000);
System.out.println("run threadName="
+ this.currentThread().getName() + " end");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package run;
import mythread.MyThread1;
public class Run1 {
public static void main(String[] args) {
MyThread1 mythread = new MyThread1();
System.out.println("begin =" + System.currentTimeMillis());
mythread.run();
System.out.println("end =" + System.currentTimeMillis());
}
}
直接调用run()方法,结果如下:
package mythread;
public class MyThread2 extends Thread {
@Override
public void run() {
try {
System.out.println("run threadName="
+ this.currentThread().getName() + " begin ="
+ System.currentTimeMillis());
Thread.sleep(2000);
System.out.println("run threadName="
+ this.currentThread().getName() + " end ="
+ System.currentTimeMillis());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package run;
import mythread.MyThread2;
public class Run2 {
public static void main(String[] args) {
MyThread2 mythread = new MyThread2();
System.out.println("begin =" + System.currentTimeMillis());
mythread.start();
System.out.println("end =" + System.currentTimeMillis());
}
}
使用start()启动线程: