talent-thread-pool-2.0.0发布:java线程池框架

talent-thread-pool是什么?

talent-thread-pool是基于jdk5内置线程池的封装,省却你一些事件的框架
1、帮你完成使用线程池所带来的繁琐的同步安全工作
2、为你提供一个更靠谱的RejectedExecutionHandler(jdk自带的是抛异常,本框架默认的是用定时继续提交)
3、为你提供一个更友好的ThreadFactory(jdk自带的Factory产生出来的Thread名字是形如thread-pool-1的,本框架默认的是形如:myname-1,其中“myname”是应用提供的参数)
4、提供更简单的ThreadPoolExecutor构造器,当然你也可以根据业务需要构造更细化的ThreadPoolExecutor对象。

快速使用
1、构造SynRunnableThreadPoolExecutor
SynRunnableThreadPoolExecutor threadExecutor = new SynRunnableThreadPoolExecutor("quickstart-thread-pool");

2、实现你的业务Runnable(理论上只需要实现AbstractSynRunnable即可,但一般都是带上队列的,所以本框架也提供了队列AbstractQueueRunnable)
public class QuickStartRunnable<T> extends AbstractQueueRunnable<T>
{
    private static java.util.concurrent.atomic.AtomicLong atomicLong = new AtomicLong();

    @Override
    public void run()
    {
        checkSyn();

        T t = null;
        while ((t = this.getMsgQueue().poll()) != null)
        {
            System.out.println(t.toString() + "--" + atomicLong.incrementAndGet());
        }
    }

    /**
     * 检查线程池是不是同步调用runnable的(在同一时刻,只有同一个runnable对象被调用)。
     */
    private static void checkSyn()
    {
        String threadName = Thread.currentThread().getName();
        File dir = new File("d:/log/dfd/");
        dir.mkdirs();
        File f = new File(dir, threadName + ".txt");
        if (!f.exists())
        {
            try
            {
                f.createNewFile();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}

3、提交业务Runnable到SynRunnableThreadPoolExecutor
threadExecutor.execute(quickStartRunnable);

具体可以参看com.talent.platform.threadpool.quickstart.QuickStartMain和com.talent.platform.threadpool.quickstart.QuickStartRunnable

谁在使用
1、某通讯设备商的网管系统,
2、本人自己在项目中使用,下面的这个图可以让大家放心使用(本框架+talent-socket框架实现的最小smpp协议)。这个图展示了每秒收发3.2W条数据的场景(3.2w=8K*4)

图1:收发速度3.2w/s

图2:稳定性:用5K的速度跑了3000多万数据,没有任何丢包

猜你喜欢

转载自tywo45.iteye.com/blog/1536159