少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

 昨天开始研究java.util.concurrent,是出于线程安全的知识懂得不多,对自己写的线程池没有信心,所以就用了包里专家写好的线程池。这个包的功能很强大。有兴趣的朋友可以搜索了解更多的内容。

     今天刚写好了一段200行左右的代码,拿出来跟大家分享我的学习经验。初次实践,不足之处,望能得到高手的指点。

功能说明:一个发送消息模块将消息发送到消息队列中,无需等待返回结果,发送模块继续执行其他任务。消息队列中的指令由线程池中的线程来处理。使用一个Queue来存放线程池溢出时的任务。

TestDriver.java是一个驱动测试,sendMsg方法不间断的向ThreadPoolManager发送数据。


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 ) );
        }
    }
}


 


ThreadPoolManager类:是负责管理线程池的类。同时维护一个Queue和调度进程。

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<String> msgQueue = new LinkedList<String>();

 // 访问消息缓存的调度线程
 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 );
 }
}




AccessDBThread类:线程池中工作的线程。

 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");
 }

}


 


posted on 2011-12-12 15:09 abin 阅读(702) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: