多線程範例

package com.zdnst.common.infra.utils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolUtils {

    private static final int POOL_SIZE = 15;

    private static ExecutorService threadPool;


    public static ExecutorService getPool(){
        if(null == threadPool) {
            threadPool = Executors.newFixedThreadPool(POOL_SIZE);
        }
        return threadPool;
    }

}
public void setMinTimeValue(final Serializable key, final Serializable value,
      final Integer mins) {
   pool.submit(new Runnable(){
      @Override
      public void run() {
         try {

            redisTemplate.opsForValue().set(key, value);
            if(mins!=null&&mins>0){
               redisTemplate.expire(key,mins,TimeUnit.MINUTES);
            }
         } catch (ZdnstException e) {
            //打印服務層異常詳情,注意第二個參數必須傳打印異常堆棧
            logger.error(Constants.EX_SERVICE_EXCEPTION + e.getMessage(), e);
            //必須拋出通知調用者
            throw new ZdnstException(e.getCode(),e.getMessage());
         } catch (Exception e) {
            //打印服務層異常詳情,注意第二個參數必須傳打印異常堆棧
            logger.error(Constants.EX_SERVICE_EXCEPTION + e.getMessage(), e);
            //封裝爲服務層異常110網絡超時後再拋出,必須拋出通知調用者
            throw new ZdnstException(BaseCode.ERROR_CODE110,e.getMessage());
         }
      }
      
   });


}
相關文章
相關標籤/搜索