package com.pool;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程队列管理
*
*/
@SuppressWarnings("unchecked")
public class ThreadPoolManager {
private static ThreadPoolManager me = new ThreadPoolManager();
// 线程池维护线程的最少数量
private final static int CORE_POOL_SIZE = 3;
// 线程池维护线程的最大数量
private final static int MAX_POOL_SIZE = 100;
// 线程池维护线程所允许的空闲时间
private final static int KEEP_ALIVE_TIME = 0;
// 线程池所使用的缓冲队列大小,>1000的线程走调度
private final static int WORK_QUEUE_SIZE = 1000;
/** 通知队列 **/
Queue<Runnable> QUEUES = new LinkedList<Runnable>();
public static ThreadPoolManager getInstance() {
return me;
}
final RejectedExecutionHandler handler = new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
QUEUES.offer(r);
}
};
// 管理线程池
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(WORK_QUEUE_SIZE), this.handler);
// 调度线程池
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
// 访问消息缓存的调度线程
final Runnable accessBufferThread = new Runnable() {
public void run() {
if (hasMoreThreads()) {
System.out.println(QUEUES.size());
Runnable task = QUEUES.poll();
threadPool.execute(task);
}
}
};
//任务调度,超过(MAX_POOL_SIZE+WORK_QUEUE_SIZE)数量的线程,自动走调度执行
final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(accessBufferThread, 0, 10, TimeUnit.SECONDS);
private ThreadPoolManager() {
System.out.println("启动线程池...");
}
public void addThread(Thread task) {
threadPool.execute(task);
}
private boolean hasMoreThreads() {
return !QUEUES.isEmpty();
}
}
package com.pool;
import java.util.Date;
/**
*测试调用代码
**/
public class test {
public static void main(String[] args) {
for (int i = 0; i<= 10000;i++){
ThreadPoolManager.getInstance().addThread(new Thread(){
public void run(){
try {
System.out.println(new Date());
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
}
分享到:
相关推荐
主要给大家介绍了关于java线程池使用后到底要不要关闭的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Java线程池使用说明Java线程池使用说明Java线程池使用说明
C# 线程池使用示例,vs2013 + 控制台窗口 简单实现。
c#线程池技术的demo,代码简单易学,对理解线程池有帮助。
线程池使用的几个demo
线程池使用基本框架,快速掌握线程池技术
ThreadPoolExecutor的使用和Android常见的4种线程池使用介绍
线程池使用实例,一个简单的使用线程池的例子
VB线程池使用
Linux线程池使用.docx
以下示例显示如何使用线程池。首先创建 ManualResetEvent 对象,此对象使程序能够知道线程池何时运行完所有的工作项。接着,尝试向线程池添加一个线程。如果添加成功,则添加其余的线程(本例中为 4 个)。然后...
本Demo演示了不同线程池运行的效果图,如果对多线程学习感兴趣,可以下载
linux线程池使用(动态调整).rar
多线程线程池使用 (C#3.5) 高效
java线程池使用说明[借鉴].pdf
1.线程池管理器(ThreadPoolManager):用于创建并管理线程池 2.工作线程(WorkThread): 线程池中线程 3.任务接口(Task):每个任务必须实现的接口,以供工作线程调度任务的执行。 4.任务队列:用于存放没有处理的...
建立TCP、UDP单播、组播 创建服务器端与客户端,服务器端使用select机制接收客户端的请求,并交给线程池处理
简单演示了如何使用线程池,可以再源代码的基础上进行修改进行更高级的操作,适合用于学习
Qt中的QThreadPool线程池的使用例子,包含QRunnable类的使用,代码仅供参考