1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。3. 使用interrupt方法中断线程。1. 使用退出标志终止线程 当run方法执行完后,线程就会退出。但有时run方法是永远不...
如何关闭java线程
关闭线程有几种方法,
一种是调用它里面的stop()方法
另一种就是你自己设置一个停止线程的标记 (推荐这种)
代码如下:
package com.demo;
//测试Thread的stop方法和自己编写一个停止标记来停止线程;
public class StopThread implements Runnable{
//停止线程的标记值boolean;
private boolean flag = true;
public void stopThread(){
flag = false;
}
public void run(){
int i=0;
while(flag){
i++;
System.out.println(Thread.currentThread().getName()+":"+i);
try{
Thread.sleep(1000);
}catch(Exception e){
}
System.out.println(Thread.currentThread().getName()+"==>"+i);
}
}
public static void main(String args[]){
StopThread st = new StopThread();
Thread th = new Thread(st);
Thread th1 = new Thread(st);
th.start();
th1.start();
try{
Thread.sleep(5500);
}catch(Exception e){
}
/*
如果使用Thread.stop方法停止线程,不能保证这个线程是否完整的运行完成一次
run方法;但是如果使用停止的标记位,那么可以保正在真正停止之前完整的运行完
成一次run方法;
*/
th.stop();
st.stopThread();
}
}2012-01-14
有三种方法可以使终止线程
1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止
2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)
3. 使用interrupt方法中断线程2018-07-04
终止线程的三种方法
1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。
2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。
3. 使用interrupt方法中断线程。
1. 使用退出标志终止线程
当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子。
package chapter2;
public class ThreadFlag extends Thread
{
public volatile boolean exit = false;
public void run()
{
while (!exit);
}
public static void main(String[] args) throws Exception
{
ThreadFlag thread = new ThreadFlag();
thread.start();
sleep(5000); // 主线程延迟5秒
thread.exit = true; // 终止线程thread
thread.join();
System.out.println("线程退出!");
}
}
在上面代码中定义了一个退出标志exit,当exit为true时,while循环退出,exit的默认值为false.在定义exit时,使用了一个Java关键字volatile,这个关键字的目的是使exit同步,也就是说在同一时刻只能由一个线程来修改exit的值,
2. 使用stop方法终止线程
使用stop方法可以强行终止正在运行或挂起的线程。我们可以使用如下的代码来终止线程:
thread.stop();
虽然使用上面的代码可以终止线程,但使用stop方法是很危险的,就象突然关闭计算机电源,而不是按正常程序关机一样,可能会产生不可预料的结果,因此,并不推荐使用stop方法来终止线程。
3. 使用interrupt方法终止线程
使用interrupt方法来终端线程可分为两种情况:
(1)线程处于阻塞状态,如使用了sleep方法。
(2)使用while(!isInterrupted()){……}来判断线程是否被中断。
在第一种情况下使用interrupt方法,sleep方法将抛出一个InterruptedException例外,而在第二种情况下线程将直接退出。下面的代码演示了在第一种情况下使用interrupt方法。
package chapter2;
public class ThreadInterrupt extends Thread
{
public void run()
{
try
{
sleep(50000); // 延迟50秒
}
catch (InterruptedException e)
{
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws Exception
{
Thread thread = new ThreadInterrupt();
thread.start();
System.out.println("在50秒之内按任意键中断线程!");
System.in.read();
thread.interrupt();
thread.join();
System.out.println("线程已经退出!");
}
}
上面代码的运行结果如下:
在50秒之内按任意键中断线程!
sleep interrupted
线程已经退出!
在调用interrupt方法后, sleep方法抛出异常,然后输出错误信息:sleep interrupted.
注意:在Thread类中有两个方法可以判断线程是否通过interrupt方法被终止。一个是静态的方法interrupted(),一个是非静态的方法isInterrupted(),这两个方法的区别是interrupted用来判断当前线是否被中断,而isInterrupted可以用来判断其他线程是否被中断。因此,while (!isInterrupted())也可以换成while (!Thread.interrupted())。2015-07-01
方案1、就你说的方案2,步骤如下1、建立可被关闭的线程public class MyTask implements Callable{...}2、建立主动关闭的线程public class KillThread extends Thread{...}在第二个线程中,主要调用 future.get(seconds, TimeUnit.SECONDS);用超时来关闭第一个线程代码太多,需要的话,可私信
代码部分图片
1、任务类,线程类,将被关闭的类
2、工具类,可以调用这个来关闭上面的线程
2018-06-27
多写几个判断的地方了,直接跳出run(){} 就停止了。2012-01-13
先判断在RUN。
if(flag){
new Thread(Runnable).start();
}else{
.........................
}2012-01-13
Thread.seleep2015-06-10