forked from qiurunze123/threadandjuc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qiurunze
committed
Mar 18, 2020
1 parent
85f8af2
commit 860c4b6
Showing
3 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
threadpoolexecutor-example/src/main/java/com/executor/PauseableThreadPool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.executor; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.RejectedExecutionHandler; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* 描述: 演示每个任务执行前后放钩子函数 | ||
*/ | ||
public class PauseableThreadPool extends ThreadPoolExecutor { | ||
|
||
private final ReentrantLock lock = new ReentrantLock(); | ||
private Condition unpaused = lock.newCondition(); | ||
private boolean isPaused; | ||
|
||
|
||
public PauseableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, | ||
TimeUnit unit, | ||
BlockingQueue<Runnable> workQueue) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); | ||
} | ||
|
||
public PauseableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, | ||
TimeUnit unit, BlockingQueue<Runnable> workQueue, | ||
ThreadFactory threadFactory) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); | ||
} | ||
|
||
public PauseableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, | ||
TimeUnit unit, BlockingQueue<Runnable> workQueue, | ||
RejectedExecutionHandler handler) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); | ||
} | ||
|
||
public PauseableThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, | ||
TimeUnit unit, BlockingQueue<Runnable> workQueue, | ||
ThreadFactory threadFactory, RejectedExecutionHandler handler) { | ||
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, | ||
handler); | ||
} | ||
|
||
@Override | ||
protected void beforeExecute(Thread t, Runnable r) { | ||
super.beforeExecute(t, r); | ||
lock.lock(); | ||
try { | ||
while (isPaused) { | ||
unpaused.await(); | ||
} | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
private void pause() { | ||
lock.lock(); | ||
try { | ||
isPaused = true; | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public void resume() { | ||
lock.lock(); | ||
try { | ||
isPaused = false; | ||
unpaused.signalAll(); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
PauseableThreadPool pauseableThreadPool = new PauseableThreadPool(10, 20, 10l, | ||
TimeUnit.SECONDS, new LinkedBlockingQueue<>()); | ||
Runnable runnable = new Runnable() { | ||
@Override | ||
public void run() { | ||
System.out.println("我被执行"); | ||
try { | ||
Thread.sleep(10); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}; | ||
for (int i = 0; i < 10000; i++) { | ||
pauseableThreadPool.execute(runnable); | ||
} | ||
Thread.sleep(1500); | ||
pauseableThreadPool.pause(); | ||
System.out.println("线程池被暂停了"); | ||
Thread.sleep(1500); | ||
pauseableThreadPool.resume(); | ||
System.out.println("线程池被恢复了"); | ||
|
||
} | ||
} |