java多线程入门学习(三)

来源:互联网 发布:加拿大 软件 研究生 编辑:程序博客网 时间:2024/05/29 18:28
java多线程入门学习(三)
在java中有三种方式可以终止正在运行的线程:
        (1)使用退出标志,使线程正常退出,也就是当前run()方法执行完成后线程终止
        (2)使用stop()方法强行终止,不推荐这个方法,因为stop和suspend及resume一样,都是过期的方法
        (3)使用interrupt方法终止线程

一.停不了的线程

        这里主要讲解的是interrupt()方法去停止线程,但是这种方式仅仅是在当前线程中打了一个停止标记,并不是真正的停止线程。
我们新建了一个线程类:
package cn.sun.interrupt;
 
public class MyThread extends Thread{
 
@Override
public void run() {
super.run();
for(int i=0; i<5000; i++){
System.out.println("i="+(i+1));
}
}
}
使用测试类:
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
myThread.sleep(2000);
myThread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果发现并没有任何改变,控制台输出依然是从1到5000。从这里我们可以学习到调用interrupt()方法并没有停止线程,那怎么办?

二.判断线程是否停止

        java的SDK中Thread.java提供了两种方法:
        (1)this.interrupt() :测试当前线程是否已经中断    静态方法
        (2)this.isInterrupt() :测试线程是否已经中断     非静态方法
我们试试看,奂时尚一个的MyThread.java,测试如下:
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
System.out.println("是否停止1?="+ myThread.interrupted());
System.out.println("是否停止2?="+ myThread.interrupted());
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果为:
i=4997
i=4998
i=4999
i=5000
是否停止1?=false
是否停止2?=false
为什么?因为:
interrupt()方法判断的是当前线程是否中断,这里很明显当前正在运行的是main线程,这个线程从未停止过,那既然这样,我们就试试能不能把main给中断了:
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
Thread.currentThread().interrupt();
System.out.println("是否停止1?="+Thread.interrupted());
System.out.println("是否停止2?="+Thread.interrupted());
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果是:
是否停止1?=true
是否停止2?=false
发现真的把main给中断了,但是很奇怪第二遍输出为什么又变成false了呢,究其原因是因为interrupt()方法具有清除状态功能
下面我们试试isInterrupted()方法:
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
System.out.println("是否停止1?="+myThread.isInterrupted());
System.out.println("是否停止2?="+myThread.isInterrupted());
} catch (Exception e) {
e.printStackTrace();
}
}
}
即如果如下:
i=168989
是否停止1?=true
是否停止2?=true
i=168990
从结果中得到:isInterruptes()方法并没有清除状态标识

总结一下:
        (1)this.interrupted() : 测试当前线程是否已经是中断状态,执行后将状态标识清除为false。
        (2)this.isInterrupted() : 测试线程Thread对象是否已经是中断状态,但不清除标志
3.能停止的线程——异常法

三.能停止的线程——异常法

首先恢复到刚开始的问题,如何停止线程,上面已经学了两种方法去判断是否是停止的线程,那么我们就用进去,还是上一个例子,我们在for循环时每次去查看是否线程已停止,如果停止了那就退出,不再继续执行:
package cn.sun.interrupt;
 
public class MyThread extends Thread{
 
@Override
public void run() {
super.run();
try {
for(int i=0; i<500000; i++){
if(this.interrupted()){
System.out.println("已经停止运行,将退出");
throw new InterruptedException();
}
System.out.println("i="+(i+1));
}
System.out.println("这是for循环外面的一句话");
} catch (InterruptedException e) {
System.out.println("进入到MyThread.java的run方法的catch块了");
}
}
}

测试类:
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果:
i=159582
i=159583
i=159584
已经停止运行,将退出
进入到MyThread.javarun方法的catch块了
        有的人疑惑为什么在for循环里不放break而要放exception,这是因为break仅仅停掉了for循环,它会跳出for循环继续执行后面的代码,而用异常便可以直接退出,不执行之后的代码

四.在沉睡中停止

让我们看看在沉睡中被终止的话控制台是如何显示的:
package cn.sun.interrupt;
 
public class Athread extends Thread{
 
@Override
public void run() {
super.run();
try {
System.out.println("run begin...");
Thread.sleep(2000);
System.out.println("run over...");
} catch (InterruptedException e) {
System.out.println("在沉睡中被终止,进入catch = "+this.interrupted());
}
}
}
测试:
package cn.sun.interrupt;
 
public class Test02 {
public static void main(String[] args) {
try {
 
Athread thread = new Athread();
thread.start();
Thread.sleep(200);
thread.interrupt();
} catch (InterruptedException e) {
System.out.println("main catch...");
e.printStackTrace();
}
System.out.println("end!");
}
 
}
结果是:
run begin...
end!
在沉睡中被终止,进入catch = false
结论:从沉睡状态中断会进入catch,同时停滞状态值已被清除,显示false 。

以下是相反的步骤:
package cn.sun.interrupt;
 
public class Athread extends Thread{
 
@Override
public void run() {
super.run();
try {
for(int i=0; i<5; i++){
System.out.println("i="+(i+1));
}
System.out.println("run begin...");
Thread.sleep(2000);
System.out.println("run over...");
} catch (InterruptedException e) {
System.out.println("先终止,再遇到了sleep,进入catch = "+this.interrupted());
}
}
}

测试:
package cn.sun.interrupt;
 
public class Test02 {
public static void main(String[] args) {
Athread thread = new Athread();
thread.start();
thread.interrupt();
System.out.println("end!");
}
 
}
这是结果:
end!
i=1
i=2
i=3
i=4
i=5
run begin...
先终止,再遇到了sleep,进入catch = false


五.能停止的线程——暴力停止


这种方法是直接使用stop()方法,就像突然掐断了一样停止在那个位置。

六.释放锁的不良后果和使用return停止线程

这里主要还是强调stop方法会导致数据不一致后果,不要使用这个方法了
另外,有上面可知其实有效使用return也可以停止线程:
package cn.sun.interrupt;
 
public class Bthread extends Thread{
 
@Override
public void run() {
while(true){
if(this.isInterrupted()){
System.out.println("停住了");
return ;
}
System.out.println("=======");
}
}
}
测试:
package cn.sun.interrupt;
 
public class Test02 {
public static void main(String[] args) {
try {
 
Bthread thread = new Bthread();
thread.start();
Thread.sleep(200);
thread.interrupt();
    } catch (InterruptedException e) {
System.out.println("main catch...");
e.printStackTrace();
}
System.out.println("end!");
}
 
}

结果为:
=======
=======
=======
停住了
end!

























































0 0
原创粉丝点击