Java线程池

一、线程池——单线程

    public static void runSingleThreadPool() {
        ExecutorService threadPool0 = Executors.newSingleThreadExecutor();
        threadPool0.execute(new FastTask("task0"));
        threadPool0.execute(new FastTask("task1"));
        threadPool0.execute(new FastTask("task2"));
    }
    public static void runSingleThreadPoolWithFactory() {
        ExecutorService threadPool0 = Executors.newSingleThreadExecutor(new MyThreadFactory());
        threadPool0.execute(new FastTask("task0"));
        threadPool0.execute(new FastTask("task1"));
        threadPool0.execute(new FastTask("task2"));
    }
    private static class MyThreadFactory implements ThreadFactory {
        private static int count = 0;

        @Override
        public Thread newThread(Runnable r) {
            Log.i(TAG, "第 " + (count++) + " 个线程创建");
            return new Thread(r);
        }
    }

二、线程池——固定线程数

    public static final void runFixedThreadPool() {
        ExecutorService threadPool = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 10; i++) {
            threadPool.execute(new FastTask("task" + i));
        }
    }
    public static void runFixedThreadPoolWithFactory() {
        ExecutorService threadPool = Executors.newFixedThreadPool(2, new MyThreadFactory());
        for (int i = 0; i < 10; i++) {
            threadPool.execute(new FastTask("task" + i));
        }
    }

三、线程池——动态线程数

    public static void runCachedThreadPool() {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            threadPool.execute(new FastTask("task" + i));
        }
    }

    public static void runCachedThreadPoolWithFactory() {
        ExecutorService threadPool = Executors.newCachedThreadPool(new MyThreadFactory());
        for (int i = 0; i < 10; i++) {
            threadPool.execute(new FastTask("task" + i));
        }
    }

四、自定义线程池

    public static void runCustomThreadPoolWithFactory() {
        // 创建一个线程执行
        ExecutorService threadPool = new ThreadPoolExecutor(0, 5,
                60L, TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(20),
                new MyThreadFactory());
        }

这里写图片描述

五、线程池关闭

    public static void runShutdown() {
        ExecutorService threadPool = new ThreadPoolExecutor(2, 10,
                60L, TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(10),
                new MyThreadFactory(),
                new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        Log.i(TAG, "被拒任务名称是: " + r.toString());
                    }
                });

        Log.i(TAG, "threadPool isShutdown: " + threadPool.isShutdown());

//        threadPool.shutdown();
        threadPool.shutdownNow();
        Log.i(TAG, "thread shutdown!!!");

        Log.i(TAG, "threadPool isShutdown: " + threadPool.isShutdown());

        threadPool.execute(new SlowTask("Task lastest"));
    }

这里写图片描述

六、线程池—移除任务

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 10,
                60L, TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(100));

        for (int i = 0; i < 20; i++) {
            try {
                threadPool.execute(new SlowTask("Task" + i));
            } catch (Exception e) {
                Log.e(TAG, e.toString());
            }
        }

        Runnable lastTask = new SlowTask("Task should be removed");
        threadPool.execute(lastTask);
        threadPool.getQueue().remove(lastTask);

七、线程池——获取任务执行结果

    //获取任务执行结果
    public static void runSubmit() {

        ExecutorService threadPool = Executors.newFixedThreadPool(2);

        Future<String> future0 = threadPool.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return "Callable result";
            }
        });
        Future<?> future1 = threadPool.submit(new SlowTask("SlowTask1"));
        Future<?> future2 = threadPool.submit(new SlowTask("SlowTask2"),
                "Runnable result");

        try {
            Log.i(TAG, "future0: " + future0.get());
            Log.i(TAG, "future1: " + future1.get());
            Log.i(TAG, "future2: " + future2.get());
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值