异步线程代码

来源:互联网 发布:淘宝网 其他淘宝流量 编辑:程序博客网 时间:2024/06/11 16:22

首先写一个工具,


public class CustomThreadService
{
    
    private static CustomThreadService instance = new CustomThreadService();
    
    private ThreadPoolExecutor service = null;
    
    private CustomThreadService()
    {
        BlockingQueue<Runnable> workQueue =
            new LinkedBlockingQueue<Runnable>(3000);
        
        this.service =
            new ThreadPoolExecutor(10, 30, 30L, TimeUnit.SECONDS, workQueue,
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
    
    public static synchronized CustomThreadService getInstance()
    {
        return instance;
    }
    
    public void execute(Runnable command)
    {
        this.service.execute(command);
    }
}


调用这个工具类,

CustomThreadService.getInstance().execute(new Runnable()
                    {
                        public void run()
                        {
                            try
                            {
                                if (logger.isDebugEnabled())
                                {
                                    logger.debug("Enter syncMethod,msisdn is empty!!");
                                }
                                //此处编写自己的demo                       
                            }
                            catch (Throwable e)
                            {
                                logger.error("unbindaccount or send mq execption:",
                                    e);
                            }
                        }
                    });

原创粉丝点击