java 怎么实现线程池

发布网友 发布时间:2022-04-23 18:01

我来回答

2个回答

热心网友 时间:2022-04-15 09:30

最简单的可以利用java.util.concurrent.Executors
调用Executors.newCachedThreadPool()获取缓冲式线程池
Executors.newFixedThreadPool(int nThreads)获取固定大小的线程池

热心网友 时间:2022-04-15 10:48

给你一个可以实现暂停的线程池的代码:
------------------------------------------------------------------------------------------------------------------
package com.koeiol.budgetSpy.pools;

import java.util.concurrent.BlockingQueue;
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 PausableThreadPoolExecutor extends ThreadPoolExecutor {

private boolean isPaused;
private ReentrantLock pauseLock = new ReentrantLock();
private Condition unpaused = pauseLock.newCondition();

public PausableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}

public PausableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}

public PausableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}

public PausableThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}

protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
pauseLock.lock();
try {
while (isPaused)
unpaused.await();
} catch (InterruptedException ie) {
t.interrupt();
} finally {
pauseLock.unlock();
}
}

public void pause() {
pauseLock.lock();
try {
isPaused = true;
} finally {
pauseLock.unlock();
}
}

public void resume() {
pauseLock.lock();
try {
isPaused = false;
unpaused.signalAll();
} finally {
pauseLock.unlock();
}
}
}
-----------------------------------------------------------------------------------------------------
下面这个是调用:
-----------------------------------------------------------------------------------------------------
package com.koeiol.budgetSpy.pools;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class DeepAnalysisManager {

private static BlockingQueue<Runnable> workQueue = null;
private static PausableThreadPoolExecutor executor = null;

public static void init(int corePoolSize, int maximumPoolSize, long keepAliveTime) {
workQueue = new LinkedBlockingQueue<Runnable>();
executor = new PausableThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue);
System.out.println("PausableThreadPoolExecutor.init()");
// DeepAnalysisManager.pause();
}

public static void execute(Runnable task) {
System.out.println(task.getClass());
executor.execute(task);
}

public static PausableThreadPoolExecutor getExecutor() {
return executor;
}

public static void pause() {
executor.pause();
}

public static void resume() {
executor.resume();
}

public static void shutdown() {
executor.shutdown();
}

public static void shutdownNow() {
executor.shutdownNow();
}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com