Thinking in Java---如何正确的终止子线程

在进行多线程编程的时候,我们经常会启动多个子线程来完成一个任务,那么如何在任务完成的时候或则是在任务进行到一定程度的时候正确的退出这些线程就成了一个问题。下面就对这个问题进行一些探讨。

一.无阻塞任务的终止
无阻塞任务的终止是最简单的情况,在这种情况下我们可以通过设定run()方法中while()循环结束的条件来很容易的正确终止这个任务。下面的这段代码演示了一般终止一个非阻塞任务的方法:

package lkl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**同时启动三个线程对一个计数对象进行计数,
 * 当计数达到一百时所有子线程终止*/

//计数类
class Count{
    private int count=0;
    //自增函数
    public synchronized void increment(){
        count++;
    }
    public synchronized int get(){
        return count;
    }
}

class Task implements Runnable{

    private Count count;
    //用于判断任务是否要结束的标志,是一个需要被多个线程读取的变量
    //所以用volatile关键进行修饰;volatile可以保证变量的可视性
    //即当任意一个任务修改了canceled后,该变化对所有任务都是可见的
    public static volatile boolean canceled=false;
    //修改canceled标志
    public  static void cancel(){
        canceled=true;
    }
    public Task(Count count){
        this.count=count;
    }
    //每个任务都会不断调用count的increment()方法,当值等于100时所有任务退出
    public void run(){

        while(!canceled){

        //必须保证下面这段代码是原子操作,才能在恰好加到100时正确的退出
          synchronized(count){ //这个同步块不加会出问题
                if(canceled){ //加了上面的同步后,不加这句话也会出问题
                    return;
                }
                count.increment();
            //  System.out.println(count.get());
                if(count.get()==100){
                    cancel();
               }
            }
            try{
                TimeUnit.MILLISECONDS.sleep(100);
            }catch(InterruptedException ex){
                System.out.println("中断");
            }
        }
    }
}
public class Test {

    public static void main(String[] args) throws Exception{
        ExecutorService exec =Executors.newCachedThreadPool();
        Count count = new Count();
        //同时启动三个线程来进行计数
        for(int i=0;i<3;i++){
            exec.execute(new Task(count));
        }
        exec.shutdown();
        TimeUnit.SECONDS.sleep(4);
        System.out.println(count.get());
        System.exit(1);
    }
}

这段代码非常简单,但是要注意我们在while循环所加的那个同步块,如果那个地方不进行同步,那么虽然count对象的increment方法和get()方法都是同步的,但是由于这两条语句之间还是会存在切换,从而会导致大多数情况下都不能在恰好达到100时结束任务;同时还要注意在每次递增之前也会有一个检查,这也是必要的,原因在于虽然有线程将canceled标志置为true,但是当前线程却可能刚从阻塞状态苏醒,需要执行下面所有的语句以后才会去检查是否退出while循环,这样就会造成多加的问题。

二.阻塞任务的终止
与非阻塞的任务相比,终止一个阻塞的任务要棘手的多。当一个线程阻塞的时候,是停在run()方法中的某一点的,我们就不能像上面那样通过程序自己检查某个条件而自动跳出循环了。我们可以大致把阻塞分为三类:调用sleep()引起的阻塞,等待同步锁引起的阻塞,等待某种资源引起的阻塞(如IO阻塞)。Thread类提供了一个interrupt()方法,用于中断阻塞;这个方法有两种作用,第一是产生一个InterruptedException,另一个是重置线程的interrupted状态为true。但是可惜的是这种中断只对sleep()引起的阻塞是有效的,对其它两种阻塞是无效的。下面的代码示范了这点。

package lkl;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
/**演示通过试图通过Thread.interrupt()方法终止三种不同阻塞的情形*/

//调用sleep而引起的阻塞
class SleepBlocked implements Runnable{
    public void run(){
        try{
            TimeUnit.SECONDS.sleep(1000);
        }catch(InterruptedException e){
            System.out.println("InterruptedException");
        }
        System.out.println("Exiting SleepBlocked.run()");
    }
}

//IO阻塞
class IOBlocked implements Runnable{
    private InputStream in;
    public IOBlocked(InputStream is){
        in=is;
    }
    public void run(){
        try{
            System.out.println("waiting for read(): ");
            in.read();
        }catch(IOException e){
            if(Thread.currentThread().isInterrupted()){
                System.out.println("Interrupted from blocked I/O");
            }else{
                throw new RuntimeException();
            }
        }
        System.out.println("Exiting IOBlocked.run()");
    }
}

//同步锁阻塞
class SynchronizedBlocked implements Runnable{
    public synchronized void f(){
        while(true){
            Thread.yield();
        }
    }
    public SynchronizedBlocked(){
        new Thread(){ //启动一个线程,通过执行永不退出的同步方法获取自身的锁
            public void run(){
                f();
            }
        }.start();
    }
    public void run(){
        System.out.println("Trying to call f()");
        f();
        System.out.println("Exiting synchronizedBlocked.run()");
    }
}

public class Interrupting {

    private static ExecutorService exec = Executors.newCachedThreadPool();
    public static void test(Runnable r) throws InterruptedException{
        Future<?> f =exec.submit(r); 
        TimeUnit.MICROSECONDS.sleep(100);
        System.out.println("Interrupting "+r.getClass().getName());
        f.cancel(true); //通过cancel()方法结束线程
        System.out.println("Interrupt sent to "+r.getClass().getName());
    }


    public static void main(String[] args) throws Exception{

          ExecutorService exec = Executors.newCachedThreadPool();
          ServerSocket server = new ServerSocket(8080);
          InputStream socketInput  = new Socket("localhost",8080).getInputStream();
          exec.execute(new IOBlocked(socketInput));
          exec.execute(new IOBlocked(System.in));
          TimeUnit.MILLISECONDS.sleep(100);
          System.out.println("Shutting down all threads");
          exec.shutdownNow(); //向所有线程发送interrupted信息
          TimeUnit.SECONDS.sleep(1);
          System.out.println("Closing "+socketInput.getClass().getName());
          socketInput.close(); //通过关闭底层阻塞的IO而结束线程
          TimeUnit.SECONDS.sleep(1);
          System.out.println("Closing "+System.in.getClass().getName());
          System.in.close();
          System.out.println("end");
    }
}
/*outPut
waiting for read(): 
waiting for read(): 
Shutting down all threads
Closing java.net.SocketInputStream
Interrupted from blocked I/O
Exiting IOBlocked.run()
Closing java.io.BufferedInputStream
end
*/

从输出来看确实只有sleep()造成的阻塞通过调用Thread.interrupt()方法中断了,而其余两种阻塞并不能并不能通过这种方法中断。但是也有例外的情况
1)对于IO阻塞,如上所示我们肯定可以通过自己关闭底层的IO资源来中断阻塞;但是如果我们使用的是nio,那么它可以自动响应interrupt()中断。如下面的代码所示:

package lkl;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ClosedByInterruptException;
import java.nio.channels.SocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**演示如何通过interrupt中断nio造成的阻塞*/
class NIOBlocked implements Runnable{
    private final SocketChannel sc;
    public NIOBlocked(SocketChannel sc){
        this.sc=sc;
    }
    public void run(){
        try{
            System.out.println("Waiting for read() in"+this);
            sc.read(ByteBuffer.allocate(1)); //此处阻塞
        }catch(ClosedByInterruptException e){
            System.out.println("ClosedByInterruptedException");
        }catch(AsynchronousCloseException e){
            System.out.println("AsynchronousCloseException");
        }catch(IOException e){
            throw new RuntimeException(e);
        }
        System.out.println("Exiting NIOBlocked.run()"+this);
    }
}
public class NIOInterruption {

    public static void main(String[] args) throws IOException, InterruptedException{
        ExecutorService exec = Executors.newCachedThreadPool();
        ServerSocket server = new ServerSocket(8080);
        InetSocketAddress isa = new InetSocketAddress("localhost",8080);
        SocketChannel sc1 = SocketChannel.open(isa);
        SocketChannel sc2=SocketChannel.open(isa);
        Future<?> f = exec.submit(new NIOBlocked(sc1));
        exec.execute(new NIOBlocked(sc2));
        exec.shutdown();
        //exec.shutdownNow()//通过这一句就可以正确的结束掉子线程
        TimeUnit.SECONDS.sleep(1);

        //下面通过两种方法
        f.cancel(true);//通过产生InterruptedException中断来退出阻塞
        TimeUnit.SECONDS.sleep(1);
        sc2.close(); //通过手动关闭底层资源来中断阻塞,与上面的方法形成对比
    }
}
/*outPut
   Waiting for read() inlkl.NIOBlocked@3801167a
Waiting for read() inlkl.NIOBlocked@601a013b
ClosedByInterruptedException
Exiting NIOBlocked.run()lkl.NIOBlocked@601a013b
AsynchronousCloseException
Exiting NIOBlocked.run()lkl.NIOBlocked@3801167a
*/

2).对于同步锁阻塞,如果我们使用的是ReentrantLock,那么也是可以被中断的,这与synchronized造成的阻塞不一样。下面的代码示范了这一点:

package lkl;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**展示使用ReentrantLock锁定而导致的阻塞,
 * 可以使用Thread.interruted()方法退出*
 */
class BlockedMutex{
    private ReentrantLock lock = new ReentrantLock();
    public BlockedMutex(){
        lock.lock(); //在构造器中给自己加锁,并且不进行释放
    }
    public void f(){
        try{
            //当本线程获得锁时,该方法立即返回
            lock.lockInterruptibly();
            System.out.println("lock acquired in f()");
        }catch(InterruptedException ex){
            System.out.println("Interrupted from lock acquisition in f()");
        }
    }
}

class Blocked2 implements Runnable{
    BlockedMutex blocked = new BlockedMutex();
    public void run(){
        System.out.println("Waiting for f() in BlockedMutex");
       blocked.f();
       System.out.println("Broken out of blocked call");
    }
}

public class Interrupting2 {
       public static void main(String[] args)throws Exception{
           Thread t = new Thread(new Blocked2());
           t.start();
           TimeUnit.SECONDS.sleep(1);
           System.out.println("t.interrupted");
           t.interrupt();
       }
}
/*outPut
Waiting for f() in BlockedMutex
t.interrupted
Interrupted from lock acquisition in f()
Broken out of blocked call
*/

三.一种统一的表示
我们知道Thread的interrupt()方法不但可以抛出InterruptedException的异常,也可以重置线程的中断状态,我们可以通过interrupted()来检查是否调用过interrupt()方法;因此我们可以通过检查interrupted()的状态来决定是否退出子线程;这种方式可以和捕捉中断异常的方法结合使用。值得注意的是被设计为响应interrupt()的类,必须要有相应的资源清理机制。如下面的代码所示:

package lkl;

import java.util.concurrent.TimeUnit;

/**使用线程的Interrupted()来控制子线程的结束*/

//模拟一个需要进行分配以后就要进行清理的对象
class NeedsCleanup{
    private final int id;
    public NeedsCleanup(int ident){
        id=ident;
        System.out.println("NeedsCleanup "+id);
    }
    public void cleanup(){
        System.out.println("Cleaning up "+ id);
    }
}

class Blocked3 implements Runnable{
    private volatile double d=0.0;
    public void run(){
        try{
        while(!Thread.interrupted()){
             //对于这种需要进行清理的对象,其后必须要紧跟try finally语句          
            NeedsCleanup clean1 = new NeedsCleanup(1);   
            try{
                System.out.println("Sleeping");
                TimeUnit.MILLISECONDS.sleep(500);
                NeedsCleanup clean2 = new NeedsCleanup(2);
                try{
                    System.out.println("Calculating");
                    for(int i=1; i<250000000; i++){ 
                        d=d+(Math.PI+Math.E)/d; 
                    }
                    System.out.println("Finished time-consuming operation");
                }finally{
                    clean2.cleanup();
                }
            }finally{
                clean1.cleanup();
            }
            }
        System.out.println("Exiting by Interrupted");
        }catch(InterruptedException ex){
            System.out.println("Exiting by InterruptedException");
        }
    }
}

public class Interrupted3 {

    public static void main(String[] args) throws Exception{
        Thread t = new Thread(new Blocked3());
        t.start();
        TimeUnit.MILLISECONDS.sleep(1800);
        t.interrupt();
    }
}
/*
  NeedsCleanup 1
Sleeping
NeedsCleanup 2
Calculating
Finished time-consuming operation
Cleaning up 2
Cleaning up 1
Exiting by Interrupted
*/

总之使用Thread.interrupted()来控制线程的循环是一个不错的选择,因为我们总是可以通过Thread.interrupt()来终止这种线程(包括ExecutorService的shutdownNow()方法和线程的cancel()方法);如果线程中没有循环只有阻塞,那么大多数情况下,Thread.interrupt()也可以中断这些阻塞并抛出InterruptedException异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值