java线程stop_如何停止java线程

如何停止java的线程一直是一个困恼我们开发多线程程序的一个问题。这个问题最终在Java5的java.util.concurrent中得到了回答:使用interrupt(),让线程在run方法中停止。

简介

建议使用的方法

private volatile Thread blinker;

public void stop() {

blinker = null;

}

public void run() {

Thread thisThread = Thread.currentThread();

while (blinker == thisThread) {

try {

thisThread.sleep(interval);

} catch (InterruptedException e){

}

repaint();

}

}

当线程处于非运行(Run)状态

当线程处于下面的状况时,属于非运行状态: 当sleep方法被调用。

当wait方法被调用。

当被I/O阻塞,可能是文件或者网络等等。

当线程处于上述的状态时,使用前面介绍的方法就不可用了。这个时候,我们可以使用interrupt()来打破阻塞的情况,如:

public void stop() {

Thread tmpBlinker = blinker;

blinker = null;

if (tmpBlinker != null) {

tmpBlinker.interrupt();

}

}

当interrupt()被调用的时候,InterruptedException将被抛出,所以你可以再run方法中捕获这个异常,让线程安全退出:

try {

....

wait();

} catch (InterruptedException iex) {

throw new RuntimeException("Interrupted",iex);

}

阻塞的I/O

当线程被I/O阻塞的时候,调用interrupt()的情况是依赖与实际运行的平台的。在Solaris和Linux平台上将会抛出InterruptedIOException的异常,但是Windows上面不会有这种异常。所以,我们处理这种问题不能依靠于平台的实现。如:

package com.cnblogs.gpcuster

import java.net.*;

import java.io.*;

public abstract class InterruptibleReader extends Thread {

private Object lock = new Object( );

private InputStream is;

private boolean done;

private int buflen;

protected void processData(byte[] b, int n) { }

class ReaderClass extends Thread {

public void run( ) {

byte[] b = new byte[buflen];

while (!done) {

try {

int n = is.read(b, 0, buflen);

processData(b, n);

} catch (IOException ioe) {

done = true;

}

}

synchronized(lock) {

lock.notify( );

}

}

}

public InterruptibleReader(InputStream is) {

this(is, 512);

}

public InterruptibleReader(InputStream is, int len) {

this.is = is;

buflen = len;

}

public void run( ) {

ReaderClass rc = new ReaderClass( );

synchronized(lock) {

rc.start( );

while (!done) {

try {

lock.wait( );

} catch (InterruptedException ie) {

done = true;

rc.interrupt( );

try {

is.close( );

} catch (IOException ioe) {}

}

}

}

}

}

package com.cnblogs.gpcuster

import java.io.BufferedReader;

import java.io.FileDescriptor;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.nio.channels.Channels;

public class InterruptInput {

static BufferedReader in = new BufferedReader(

new InputStreamReader(

Channels.newInputStream(

(new FileInputStream(FileDescriptor.in)).getChannel())));

public static void main(String args[]) {

try {

System.out.println("Enter lines of input (user ctrl+Z Enter to terminate):");

System.out.println("(Input thread will be interrupted in 10 sec.)");

// interrupt input in 10 sec

(new TimeOut()).start();

String line = null;

while ((line = in.readLine()) != null) {

System.out.println("Read line:'"+line+"'");

}

} catch (Exception ex) {

System.out.println(ex.toString()); // printStackTrace();

}

}

public static class TimeOut extends Thread {

int sleepTime = 10000;

Thread threadToInterrupt = null;

public TimeOut() {

// interrupt thread that creates this TimeOut.

threadToInterrupt = Thread.currentThread();

setDaemon(true);

}

public void run() {

try {

sleep(10000); // wait 10 sec

} catch(InterruptedException ex) {/*ignore*/}

threadToInterrupt.interrupt();

}

}

}

这里还需要注意一点,当线程处于写文件的状态时,调用interrupt()不会中断线程。

参考资料

Java Threads, Third Edition O'Reilly

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值