关于线程池使用的很有启示的代码

这是我在学习线程池过程中边码的代码,涉及如何按阿里标准创建线程池,什么情况会出现Exception。

知识背景

在这里插入图片描述

在这里插入图片描述

代码

请认真看注释

package Chapter3.AboutThreadPool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.junit.Test;

import java.util.concurrent.*;


public class T1 {

//        Executors.newFixedThreadPool source code use ThreadPoolExecutor :

//        public static ExecutorService newFixedThreadPool(int nThreads) {
//            return new ThreadPoolExecutor(nThreads, nThreads,
//                    0L, TimeUnit.MILLISECONDS,
//                    new LinkedBlockingQueue<Runnable>());
//        }
    // ExecutorService es1 = Executors.newFixedThreadPool(1);

    final BlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1);
    final ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Orders-%d").setDaemon(true).build();
    ExecutorService es = new ThreadPoolExecutor(1, 1, 0L,
            TimeUnit.MILLISECONDS, queue, threadFactory);
//    ExecutorService es = new ThreadPoolExecutor(6, 10, 0L,
//            TimeUnit.MILLISECONDS, queue, threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());

    /**
     * if ExecutorService use AbortPolicy, Exception raised: <br/>
     * “RejectedExecutionException: <br/>
     * FutureTask rejected from ThreadPoolExecutor <br/>
     * [Running, pool size = 10, active threads = 10, queued tasks = 5, completed tasks = 0]” <br/>
     * <br/>
     * if ExecutorService use CallersRunsPolicy, no exception. <br/>
     * If no resources available, the next set of tasks will block to wait until the previous set finish. <br/>
     *
     */
    @Test
    public void test1()  {
        for (int i = 0; i < 1000000; i++) {
            es.submit(()->{
                try {
                    Thread.sleep(5000);
                    System.out.println("Thread " + Thread.currentThread().getId() + ": " + "finished");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            System.out.println("Task " + i + "submited");

        }
        es.shutdown();
    }

    /**
     * raise RejectedExecutionException
     */
    @Test
    public void test2() {
        for (int i = 0; i < 1000000; ++i) {
            int finalI = i;
            es.submit(()->{
                try {
                    Thread.sleep(5000);
                    System.out.println(Thread.currentThread().getName() + " processing: " + finalI);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            System.out.println("Task " + i + " submitted");
        }
    }


    /**
     * 为什么这里不会报错RejectedExecutionException???<br/>
     * 因为语句:res.get()导致了线程的阻塞执行,线程1没执行完,不会开始提交线程2,所以最后看起来像是同步执行,并且也永远不会报错。因为每次只有一个线程在同时执行。
     */
    @Test
    public void test3() {
        for (int i = 0; i < 1000000; ++i) {
            int finalI = i;
            Future<Boolean> res = es.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() {
                    try {
                        String name = Thread.currentThread().getName();
                        Thread.sleep(5000);
                        System.out.println(name + " processing: " + finalI);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return finalI % 2 == 0;
                }
            });
            try {
                System.out.println(res.get());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * RejectedExecutionException
     */
    @Test
    public void test4() {
        for (int i = 0; i < 1000000; ++i) {
            int finalI = i;
            Future<Boolean> res = es.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() {
                    try {
                        String name = Thread.currentThread().getName();
                        Thread.sleep(5000);
                        System.out.println(name + " processing: " + finalI);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return finalI % 2 == 0;
                }
            });
            System.out.println("Task " + i + " submitted");
        }

    }



}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值