どうやってスレッドプールの一時停止と回復機能を実現しますか?

3842 ワード

多くの場合、私たちは休止スレッドが必要です。shutdownスレッドプールではなく、一時停止スレッドが私たちのために保存されます。後で実行できます。無駄な出費を避けるために。
ここではスレッドプールを一時停止する方法を提供します。
まず、ThreadPool Exector.javaのソースコードを入手して、それを自分のバッグ内の私有類に変えます。
次にスレッドプールを修正します。
まずスレッドクラスに方法と変数を追加します。
 
BlockingQueue pauseQueue=new ArrayBlockingQueue<>(1);//               
 
 
isPause=true;//  
 
public void pause(){//     ,        
    
    isPause=true;
    System.out.println("   "+isPause+exit);
}
public void resume(){//     ,        

    isPause=false;
    
    if (workQueue.isEmpty()) {
        return;
    }
    pauseQueue.offer(workQueue.poll());
}
次の方法を修正します。
 
 
private Runnable getTask() {
    boolean timedOut = false; // Did the last poll() time out?

    for (;;) {
        int c = ctl.get();
        int rs = runStateOf(c);

        // Check if queue empty only if necessary.
        if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
            decrementWorkerCount();
            
            return null;
        }

        int wc = workerCountOf(c);

        if (isPause){
            try {
                
                return pauseQueue.take();

            } catch (InterruptedException e) {
                
               
            }
            
        }
        // Are workers subject to culling?
        
        boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

        if ((wc > maximumPoolSize || (timed && timedOut))
                && (wc > 1 ||workQueue.isEmpty())) {
            if (compareAndDecrementWorkerCount(c)) {
                
                return null;

            }
            
            continue;
        }

        try {

            Runnable r = timed ?// TODO: 2017/5/14 keepAliveTime          
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :// TODO: 2017/5/14                    ,          null
                    workQueue.take();// TODO: 2017/5/14                      ;
            if (r != null) {
                
                return r;
            }
            timedOut = true;// TODO: 2017/5/14             ,             ,              ,             
            
        } catch (InterruptedException retry) {
            timedOut = false;
            
        }
    }
}
public void execute(Runnable command) {
    if (command == null)
        throw new NullPointerException();
    
if (isPause) { if (!workQueue.offer(command)) reject(command); return; }
//Procesed in 3 steps:*1.If fewer than corePoolSize threads arrunning、try to*start a new thread with the given command first*task.The cal to adWorkers atworlically cheks andarth andatworkatworkators*privator。by returning false.**2.If a task can be succcefully queued、then we still need*to doublle- chcheck wheheheheher shoud have added a thread*(because eexisinininininininininininininininininininined sitetetetedededededededededededededededededededededededededededededededededededededededededededededededededededelast last last last last last last stststststststststststststststststststststststststif*stopped、or start a new thread if there re none.**3.If we cannot queue task,then we try to add a new*thread.If it fails,we know are shut down or saturated*and so reject the task.tk.*ctif(workCountOf) 
ダウンロード