JAVA中终止线程的方法
2013-06-02 22:34
387 查看
Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(), stop(), stop(Throwable) and suspend(), destroy() and resume()。通过这些方法,我们可以对线程进行方便的操作,但是这些方法中,只有start()方法得到了保留。
在Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中详细讲解了舍弃这些方法的原因。
如果真的需要终止一个线程,可以使用以下几种方法:
1、让线程的run()方法执行完,线程自然结束。(这种方法最好)
2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)
public class SomeThread implements Runnable {
private volatile boolean stop = false;
public void terminate() {
stop = ture;
}
public void run() {
while(stop) {
// ... some statements
}
}
}
如果线程因为执行sleep()或是wait()而进入Not Runnable状态,假如是wait() 用标志位就方法就不行了,
public final void wait(long timeout)
throws InterruptedException
此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态
wait() 的标准使用方法
synchronized(obj){
while(<不满足条件>){
obj.wait();
}
满足条件的处理过程
}
而您想要停止它,您可以使用第三种即
3 使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法,
例如:
public class SomeThread {
public static void main(String[] args)
{
Thread thread=new Thread(new Runnable(){
public void run() {
while (!Thread.interrupted()) {
// 处理所要处理的工作
try {
System.out.println("go to sleep");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("i am interrupted!");
}
});
thread.start();
thread.interrupt();
}
}
执行结果为:
go to sleep
i am interrupted!
实际上最好的方法是使用线程池,当线程不用了,就让它sleep并放进队列中,这样可以最大限度地利用资源。
原文链接:点击打开链接
在Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中详细讲解了舍弃这些方法的原因。
如果真的需要终止一个线程,可以使用以下几种方法:
1、让线程的run()方法执行完,线程自然结束。(这种方法最好)
2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)
public class SomeThread implements Runnable {
private volatile boolean stop = false;
public void terminate() {
stop = ture;
}
public void run() {
while(stop) {
// ... some statements
}
}
}
如果线程因为执行sleep()或是wait()而进入Not Runnable状态,假如是wait() 用标志位就方法就不行了,
public final void wait(long timeout)
throws InterruptedException
此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态
wait() 的标准使用方法
synchronized(obj){
while(<不满足条件>){
obj.wait();
}
满足条件的处理过程
}
而您想要停止它,您可以使用第三种即
3 使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法,
例如:
public class SomeThread {
public static void main(String[] args)
{
Thread thread=new Thread(new Runnable(){
public void run() {
while (!Thread.interrupted()) {
// 处理所要处理的工作
try {
System.out.println("go to sleep");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("i am interrupted!");
}
});
thread.start();
thread.interrupt();
}
}
执行结果为:
go to sleep
i am interrupted!
实际上最好的方法是使用线程池,当线程不用了,就让它sleep并放进队列中,这样可以最大限度地利用资源。
原文链接:点击打开链接
相关文章推荐
- JAVA中 终止线程的方法介绍
- java终止线程的三种方法
- java多线程中终止线程的方法的对别
- Java中终止线程的方法详解
- JAVA多线程学习笔记<3>线程传入参数与终止线程方法
- java终止线程的方法
- JAVA的interrupt()方法和线程终止的方式
- 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
- 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
- 【java】终止线程的三种方法
- Java终止线程实例和stop()方法源码阅读
- Java 终止线程方法
- Java 终止线程方法
- 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
- java线程interrupt()方法和线程终止方式
- Java并发编程之三:线程挂起、恢复与终止的正确方法
- Java中安全终止线程的方法
- 【Java并发编程】之三:线程挂起、恢复与终止的正确方法(含代码)
- Java终止当前线程的方法
- Java线程之interrupt终止线程,Thread中的主要方法(五)