ThreadPoolManager类:负责管理线程池,调用轮询的线程来访问字符串缓冲区的内容,维护缓冲区,当线程池溢出时抛出的Runnable任务被加入到字符缓冲区。
public class ThreadPoolManager
{
private static ThreadPoolManager tpm = new ThreadPoolManager();
// 线程池维护线程的最少数量
private final static int CORE_POOL_SIZE = 4;
// 线程池维护线程的最大数量
private final static int MAX_POOL_SIZE = 10;
// 线程池维护线程所允许的空闲时间
private final static int KEEP_ALIVE_TIME = 0;
// 线程池所使用的缓冲队列大小
private final static int WORK_QUEUE_SIZE = 10;
// 消息缓冲队列
Queue msgQueue = new LinkedList();
// 访问消息缓存的调度线程
final Runnable accessBufferThread = new Runnable()
{
public void run()
{
// 查看是否有待定请求,如果有,则创建一个新的AccessDBThread,并添加到线程池中
if( hasMoreAcquire() )
{
String msg = ( String ) msgQueue.poll();
Runnable task = new AccessDBThread( msg );
threadPool.execute( task );
}
}
};
final RejectedExecutionHandler handler = new RejectedExecutionHandler()
{
public void rejectedExecution( Runnable r, ThreadPoolExecutor executor )
{
System.out.println(((AccessDBThread )r).getMsg()+"消息放入队列中重新等待执行");
msgQueue.offer((( AccessDBThread ) r ).getMsg() );
}
};
// 管理数据库访问的线程池
final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
new ArrayBlockingQueue( WORK_QUEUE_SIZE ), this.handler );
// 调度线程池
final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool( 1 );
final ScheduledFuture taskHandler = scheduler.scheduleAtFixedRate(
accessBufferThread, 0, 1, TimeUnit.SECONDS );
public static ThreadPoolManager newInstance()
{
return tpm;
}
private ThreadPoolManager(){}
private boolean hasMoreAcquire()
{
return !msgQueue.isEmpty();
}
public void addLogMsg( String msg )
{
Runnable task = new AccessDBThread( msg );
threadPool.execute( task );
}
}
public class AccessDBThread implements Runnable
{
private String msg;
public String getMsg()
{
return msg;
}
public void setMsg( String msg )
{
this.msg = msg;
}
public AccessDBThread(){
super();
}
public AccessDBThread(String msg){
this.msg = msg;
}
public void run()
{
// 向数据库中添加Msg变量值
System.out.println("Added the message: "+msg+" into the Database");
}
}
public class TestDriver
{
ThreadPoolManager tpm = ThreadPoolManager.newInstance();
public void sendMsg( String msg )
{
tpm.addLogMsg( msg + "记录一条日志 " );
}
public static void main( String[] args )
{
for( int i = 0; i <100; i++ )
{
new TestDriver().sendMsg( Integer.toString( i ) );
相关推荐
NULL 博文链接:https://j2ee2009.iteye.com/blog/689161
NULL 博文链接:https://hoochiang.iteye.com/blog/2357124
本篇文章主要介绍了Java利用Redis实现消息队列的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
java.util.concurrent 使用jdk内置的包实现一个线程池的操作
java多线程加队列上传文件_后台处理
并发-线程池和阻塞队列 并发-线程池和阻塞队列 并发-线程池和阻塞队列
ThreadPool 线程池类 DEFAULT_POOL_SIZE 默认线程池大小 threadPool 线程队列 taskQueue 任务队列 poolSize 自定义线程池大小 通过构造启动该线程池,调用addTask 方法将task任务传入,线程池会自动分配线程去执行...
1. 初始化线程执行提交的任务 2. 任务执行不过来,放入工作队列 3. 任务过多,线程和队列均处理不过来,拒绝执行(本文中将抛出RejectedExecuti
主要介绍了java线程池工作队列饱和策略代码示例,涉及线程池的简单介绍,工作队列饱和策略的分析及代码示例,具有一定参考价值,需要的朋友可以了解下。
所有的可以显示出来的图形元素都称为Component,Component代表了所有的可见的图形元素,... Pannel应用比较典型的就是Applet(JAVA的页面小应用程序),现在基本上已经不用了,AJAX和JAVASCRIPT完全取代了它的应用。
线程池远不是服务器应用程序内使用多线程的唯一方法。如同上面所提到的,有时,为每个新任务生成一个新线程是十分明智的。然而,如果任务创建过于频繁而任务的平均处理时间过短,那么为每个任务生成一个新线程将会...
中文文档,其目录如下: 线程中一些基本术语和概念 线程之间的通讯 Java线程调度 线程池 工作队列
Java Queue,scheduler,ThreadPoolManager,两套例子,直接可以运行的。
一个比较简单的线程池至少应包含线程池管理器、工作线程、任务队列、任务接口等部分。其中线程池管理器(ThreadPool Manager)的作用是创建、销毁并管理线程池,将工作线程放入线程池中;工作线程是一个可以循环执行...
几乎在每个服务器应用程序中都会出现线程池和工作队列问题。本文中,Brian Goetz 探讨了线程池的动机、一些基本实现和调优技术以及一些要避免的常见危险。
Java通过Executors提供四种线程池,分别为: newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。 newFixedThreadPool 创建一个定长线程池...
定义全局线程池,将用户的请求放入自定义队列中,排队等候线程调用,等待超时则自动取消该任务,实现超时可取消的异步任务
1.媲美java线程池框架,整套源码资源,使用Intellij Idea开发工具,JDK1.8以上 2.带有测试代码 3.可以根据项目实际情况任意调整代码 4.任务队列、拒绝策略 5.BasicThreadPool.java、LinkedRunnableQueue.java、...