线程的停止方式
停止线程的方式有一下几种,分别展示一下
1. 使用退出标识
使得线程正常退出,即当run方法完成后进程终止。
、public class test {
public static void main(String[] args) throws CloneNotSupportedException {
MyThread myThread = new MyThread();
myThread.start();
}
}
class MyThread extends Thread {
@Override
public void run() {
boolean flag = true;
int i = 1;
while (flag) {
System.out.println(i++);
if (i == 10) {
flag = false;
}
}
}
}
利用标识符flag判定线程是否继续执行。
2. 使用stop强行中断线程
(此方法为作废过期方法),不推荐使用,暴力终止,可能使一些清理性的工作得不到完成。还可能对锁定的内容进行解锁,容易造成数据不同步的问题。
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
MyThread myThread = new MyThread();
myThread.start();
}
}
class MyThread extends Thread {
@Override
public void run() {
for (int i = 1; i < 10; i++) {
System.out.println(i);
if (i == 5) {
this.stop();
}
}
}
}
3. 使用interrupt方法
在Thread.java类里提供了两种方法判断线程是否为停止的。
this.interrupted():测试当前线程是否已经中断(静态方法)。如果连续调用该方法,则第二次调用将返回false。在api文档中说明interrupted()方法具有清除状态的功能。执行后具有将状态标识清除为false的功能。
this.isInterrupted():测试线程是否已经中断,但是不能清除状态标识。
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
MyThread myThread = new MyThread();
myThread.start();
}
}
class MyThread extends Thread {
@Override
public void run() {
int i = 1;
while (!this.isInterrupted()) {
System.out.println(i++);
if (i == 10) {
this.interrupt();
}
}
System.out.println("interrupt之后,我还会执行一次!");
}
}
这种防止导致线程终止以后,还会继续将本次循环后续代码执行完才会结束。可以使用下面方法来优化,使用抛异常的方式。
4.使用抛异常停止
public class test {
public static void main(String[] args) throws CloneNotSupportedException {
MyThread myThread = new MyThread();
myThread.start();
}
}
class MyThread extends Thread {
@Override
public void run() {
int i = 1;
try {
while (!this.isInterrupted()) {
System.out.println(i++);
if (i == 10) {
throw new IllegalAccessException("线程已经终止!");
}
}
System.out.println("interrupt之后,我还会执行一次!");
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
5. 在沉睡中停止
public class test {
public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(20);
myThread.interrupt();
}
}
class MyThread extends Thread {
@Override
public void run() {
try {
//此时为false
System.out.println(this.isInterrupted());
this.sleep(5000);
} catch (InterruptedException e) {
System.out.println("在沉睡中停止");
//此时依然为false
System.out.println(this.isInterrupted());
e.printStackTrace();
}
}
}
sleep状态下停止某一个线程,会进入catch语句,并清除状态值,变成false
6. 先停止后沉睡
public class test {
public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
myThread.interrupt();
}
}
class MyThread extends Thread {
@Override
public void run() {
try {
//此时为false
System.out.println(this.isInterrupted());
this.sleep(5000);
} catch (InterruptedException e) {
System.out.println("先沉睡,后停止");
//此时依然为false
System.out.println(this.isInterrupted());
e.printStackTrace();
}
}
}
任务执行完成后,才抛出异常!
》》》博主长期更新学习心得,推荐点赞关注!!!
》》》若有错误之处,请在评论区留言,谢谢!!!