zoukankan      html  css  js  c++  java
  • java多线程那些事之中的一个

    1.  Callable 接口

     获取线程运行状态(get、get(long timeout)),取消线程(cancel(boolean  mayinterruptifrunning))。isCancel,isDone等。

    1.  实现callable 接口,

    2.  创建线程池。submit thread


    例如以下所看到的:

     Future< ? > processFuture = submitTask(
                new ProcessPbxPhoneThread(queue, phoneHandler, runningLock, runningFlagMap));

     private class ProcessPbxPhoneThread implements Callable<Boolean>
       {
          private BlockingQueue<PbxPhone> queue;
          private IPbxNotificationHandler<PbxPhone> phoneHandler;
          private ReadWriteLock runningLock;
          private Map<String, Boolean> runningFlagMap;
          private int processedPhoneCount = 0;
          private long currentTime;
    
          public ProcessPbxPhoneThread(BlockingQueue<PbxPhone> queue,
                IPbxNotificationHandler<PbxPhone> phoneHandler, ReadWriteLock runningLock,
                Map<String, Boolean> runningFlagMap)
          {
             this.queue = queue;
             this.phoneHandler = phoneHandler;
             this.runningFlagMap = runningFlagMap;
             this.runningLock = runningLock;
          }
    
          @Override
          public Boolean call()
             throws Exception
          {
             while (true)
             {
                PbxPhone phone = queue.take();
                if (processedPhoneCount % 100 == 0)
                {
                   long now = System.currentTimeMillis();
                   if (currentTime != 0)
                   {
                      double speed = processedPhoneCount * 1000.0 * 60 / (now - currentTime);
                      log.debug("ProcessPbxPhoneThread phone process speed:{}*m", speed);
                   }
                   currentTime = now;
                   processedPhoneCount = 0;
                }
                processedPhoneCount++;
    
                if (null != phone.getLines())
                {
                   currentTime("invokePhoneHander");
                   PbxNotification<PbxPhone> notification = new PbxNotification<PbxPhone>(
                         phoneHandler.getNotificationType(), NotificationOperation.INSERT,
                         phone.getUuid().toString(), phone);
                   if (!phoneHandler.objectChanged(notification))
                   {
                      logSpentTime("invokePhoneHander");
                      try
                      {
                         runningLock.writeLock().lock();
                         runningFlagMap.put("runningFlag", false);
                         break;
                      }
                      finally
                      {
                         runningLock.writeLock().unlock();
                      }
                   }
                   else
                   {
                      logSpentTime("invokePhoneHander");
                   }
                }
                else
                {
                   break;
                }
             }
             return true;
          }
       }
    

    submitTask 利用封装好的线程池提交线程:

     protected Future< ? > submitTask(Callable< ? > task)
       {
          return this.cucmDriverFactory.<strong style="background-color: rgb(255, 0, 0);">getExecutorService</strong>().submit(task);
       }
    

    线程池创建例如以下代码:

      @Override
       public void initialize()
       {
          executorService = Executors.newCachedThreadPool(new DefaultThreadFactory("CucmDriverFactor-"));
       } 
    @Override
       public ExecutorService getExecutorService()
       {
          if (executorService.isShutdown() || executorService.isTerminated())
          {
             initialize();
          }
          return this.executorService;
       }

    ThreadFactory 创建例如以下:

     /**
        * The thread factory, with name Prefix. Copied from ThreadPoolExecutor.DefaultThreadFactory
        */
       public static class DefaultThreadFactory implements ThreadFactory {
           private final ThreadGroup group;
           private final AtomicInteger threadNumber = new AtomicInteger(1);
           private final String namePrefix;
    
           public DefaultThreadFactory(String namePrefix) {
               SecurityManager s =
    
  • 相关阅读:
    C++ 什么是多态
    *和&的使用
    静态链接库与动态链接库
    利尔达CC3200模块烧写程序笔记
    创龙TMS320C6748开发找不到 tl.dsp.evm6748的问题研究
    RTSC和XDCTool的理解
    创龙DSP6748开发板SYS/BIOS的LED闪烁-第2篇
    Coap协议学习笔记-第一篇
    linux进程的学习笔记(未完)
    创龙DSP6748开发板LED闪烁-第一篇
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7189646.html
Copyright © 2011-2022 走看看