停止线程在java语言中不并不像break语句哪样干脆,不是说停止就停止。
在java中有3种停止线程的方式:
1.线程正常退出,也就是当run方法正常完成后终止线程。
2.使用stop方法强制终止线程,但是不推荐使用这个方法,它是一个作废过期的方法,使用它们会产生不可预料的后果。
3.使用interrupt方法中断线程。
但是使用interrupt()方法的使用并不像for +break那样马上就停止循环
例子:
public class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<1000;i++) {
System.out.println("i:"+i +" 当前线程:"+Thread.currentThread().getName());
}
}
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread);
thread.start();
thread.interrupt();
}
}
可以看出他依然打印到了999没有停止该线程
要停止线程,先要知道如何判断线程状态是不是停止的
1.interrupted
public static boolean interrupted()
测试当前线程是否已经中断
2.isInterrupted
public boolean isInterrupted()
测试线程是否已经中断
看看例子
1.interrupted
public class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<1000;i++) {
System.out.println("i:"+i );
}
}
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread,"Mythread");
thread.start();
thread.interrupt();
System.out.println("线程是否停止1="+Thread.interrupted()+" 当前线程:"+Thread.currentThread().getName());
System.out.println("线程是否停止2="+Thread.interrupted()+" 当前线程:"+Thread.currentThread().getName());
}
}
因为interrupted判断的是当前线程 而当前线程是main线程,而interrupt是中断当前线程也就是中断main线程,那如何中断main线程呢
public static void main(String[] args){
Thread.currentThread().interrupt(); //Thread.currentThread()获取当前线程信息
System.out.println(“是否停止1:”+Thread.interrupted());
System.out.println(“是否停止2:”+Thread.interrupted());
System.out.println(“end”);
}
而为什么第二次又位false了呢,它的官方文档中解释
测试当前线程是否已经中断。线程的中断状态由该方法清除,换句话说,如果连续调用该方法,则第二次调用将返回false,所以interrupted方法具有清除状态的功能
如果没有中断main线程的话,两次调用都会返回false。清除这种状态是相对的,意思是如果线程是中断的(true),那么第二次调用该方法会清除这种状态,返回false,如果线程没有中断,那么第二次依然不会改变这种状态
2.isInterrupted
此方法不是静态
1.
public class MyThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<1999;i++) {
System.out.println(i+1);
}
}
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread,"Mythread");
thread.start();
thread.interrupt();
System.out.println("线程是否停止1="+thread.isInterrupted());
System.out.println("线程是否停止2="+thread.isInterrupted());
}
可以看到我將Mythread线程interrupt停止了,两者返回的都是true,并且不会清除这种状态。
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread,"Mythread");
thread.start();
thread.interrupt();
System.out.println("线程是否停止1="+Thread.currentThread().isInterrupted());
System.out.println("线程是否停止2="+Thread.currentThread().isInterrupted());
}
输出语句中我是判断的main线程是否停止我并没有停止main线程所以返回的是false
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
Thread thread = new Thread(myThread,"Mythread");
thread.start();
Thread.currentThread().interrupt();
System.out.println("线程是否停止1="+Thread.currentThread().isInterrupted());
System.out.println("线程是否停止2="+Thread.currentThread().isInterrupted());
}
Thread.currentThread().interrupt();可以看到我将main线程停止了,所以输出语句返回的true