Thinking in Java---如何正確的終止子執行緒

acm_lkl發表於2020-04-04

在進行多執行緒程式設計的時候,我們經常會啟動多個子執行緒來完成一個任務,那麼如何在任務完成的時候或則是在任務進行到一定程度的時候正確的退出這些執行緒就成了一個問題。下面就對這個問題進行一些探討。

一.無阻塞任務的終止
無阻塞任務的終止是最簡單的情況,在這種情況下我們可以通過設定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異常。

相關文章