isAlive():
- 作用:判断当前线程是否是存活状态,如果是返回true,否则返回false
- 什么是存活状态?如果线程启动后并未终止,就认为线程是存活的。(注意这个终止,后面会说到)
- 如果线程已经start()后,当线程执行完时,调用isAlive()返回false,该线程已经被销毁,不能再次start().
- 代码演示:
public class Run_1 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread_1();
t1.start();
Thread.sleep(1000);//让主线程多睡会儿,好让Thread_1能执行完。
System.out.println("我主线程都睡这么久了,Thread_1线程应该执行完了吧,来看看他的状态:"+t1.isAlive());
}
}
class Thread_1 extends Thread{
public void run(){
System.out.println("线程Thread_1在执行。。。");
System.out.println("Thread_1线程没执行完,所以我是"+this.isAlive());
System.out.println("Thread_1执行完毕");
}
}
线程Thread_1在执行。。。
Thread_1线程没执行完,所以我是true
Thread_1执行完毕
我主线程都睡这么久了,Thread_1线程应该执行完了吧,来看看他的状态:false
interrupt()
- 作用:调用该方法可使当前线程被打上一个停止标记,但是线程并没有真正的停止,也就是说线程会继续运行 (后面所说的打断一律都是在说打上一个标记。)例如:
public class Run_1 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread_1();
t1.start();
}
}
class Thread_1 extends Thread{
public void run(){
System.out.println("线程Thread_1在执行。。。");
this.interrupt();
System.out.println("Thread_1执行完毕");
}
}
线程Thread_1在执行。。。
Thread_1执行完毕
静态方法interrupted()
- 作用:
- 查看线程是否是停止状态(是否有停止标记),如果有就返回true,否则返回false。
- 该方法在哪个线程中运行就检测哪个线程的状态,(注意:不是哪个线程对象调用该方法,而是在哪个线程中运行)。
- 调用一次该方法后,将标志重置为false。
- 代码演示0:
public class Run_1 {
public static void main(String[] args) throws InterruptedException {
Thread.currentThread().interrupt();
System.out.println("主线程被标记上停止标志,Thread.interrupted()又是在主线程中运行,所以打印出"+Thread.interrupted());
System.out.println("标志被清除,重置"+Thread.interrupted());
}
}
主线程被标记上停止标志,Thread.interrupted()又是在主线程中运行,所以打印出true
标志被清除,重置false
- 代码演示1:
public class Run_1 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread_1();
t1.start();
/*
*经过实践,Thread_1中的代码5-6ms就完了
*,所以,主线程睡的时间不能超过5ms。因为超过
*5ms后,Thread_1线程执行完了,再在主线程中调用
*interrupt()方法是没有意义的。
*/
Thread.sleep(2);
/*
* 主线程睡2ms,cpu先分配给Thread_1线程,
* 2ms后再次分配给主线程时,Thread_1已经
* 执行了一段时间,但还没执行完,在这个过程中
* 间使用interrupt()打断才有说服力。
*/
t1.interrupt();
System.out.println(t1.interrupted());
}
}
class Thread_1 extends Thread{
public void run(){
long startTime = System.currentTimeMillis();
for(int i = 0;i<900000000;i++){}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
}
false
5
这个结果应该是在意料之中的吧,虽然打断了,但是监测的并不是打断的线程,而是主线程,因为该方法在主线程中运行。
isInterrupted()
- 作用:也是检测线程是否打断,但是不同的是,哪个线程对象调用该方法就检测哪个而且不重置标志
- 演示:还是用代码演示1的代码,但是使用的方法改变了
public class Run_1 {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread_1();
t1.start();
/*
*经过计算,Thread_1中的代码5-6ms就完了
*,所以,主线程睡的时间不能超过5ms。因为超过
*5ms后,Thread_1线程执行完了,再在主线程中调用
*interrupt()方法是没有意义的。
*/
Thread.sleep(2);
/*
* 主线程睡2ms,cpu先分配给Thread_1线程,
* 2ms后再次分配给主线程时,Thread_1已经
* 执行了一段时间,但还没执行完,在这个过程中
* 间使用interrupt()打断才有说服力。
*/
t1.interrupt();
System.out.println(t1.isInterrupted());
}
}
class Thread_1 extends Thread{
public void run(){
long startTime = System.currentTimeMillis();
for(int i = 0;i<900000000;i++){}
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime);
}
}
true
5
t1被打断了,所以true,另外,想想代码的注释。
如何停止线程
- stop()方法
- return语句
- 抛异常,
run(){
......
if(this.interrupted()){throw new InterruptedException();}
.......
}
随想:
isAlive()方法与interrupt()有没有关系呢?
public class Run_5 {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread_5();
System.out.println("是否活着3"+t.isAlive());
t.start();
Thread.currentThread().sleep(1000);
System.out.println("是否活着4"+t.isAlive());
System.out.println(t.isInterrupted());
}
}
class Thread_5 extends Thread{
public void run(){
System.out.println("THread_5线程正在执行");
System.out.println("是否活着1"+this.isAlive());//线程是否在活着
this.interrupt();//打断该线程
System.out.println(this.isInterrupted());//看看是否打断
System.out.println("是否活着2"+this.isAlive());//看看打断后是否还活着
}
}
是否活着3false
THread_5线程正在执行
是否活着1true
true
是否活着2true
是否活着4false
false
瞎想来想去的结果:
- 如果isAlive()返回false,interrupt()是没有意义的,调用isInterrupted()方法的结果也只会有false。
- 如果isAlive()返回true,interrupt()后还是true,不会造成影响。所以不要把isAlive中的终止和interrupt()搞混淆了。