Java 线程池内容,全在这里了

在这里插入图片描述
在这里插入图片描述

/**
 * 1.未使用线程池
 */
public class ThreadDemo {
    public static void main(String[] args) {
        List<Integer> list = new LinkedList<Integer>();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            new Thread(()->{
                list.add(new Random().nextInt());
            }).start();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:"+(endTime-startTime));
    }
}
/**
 1. 2.使用线程池
 */
public class ThreadDemo {
    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 1000,
                0L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());

        List<Integer> list = new LinkedList<Integer>();
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            executor.execute(new Thread(()->{
                list.add(new Random().nextInt());
            }));
        }
        //线程执行完成后,关闭线程池(此处仅为效果使用,开发用到不多)
        executor.shutdown();
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:"+(endTime-startTime));
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Fork/Join框架介绍

4.ThreadPoolExecutor 核心参数

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler)

在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
}

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

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

当然,可以重写线程工厂方法来修改线程名称

 ExecutorService pool = Executors.newFixedThreadPool(2, new ThreadFactory() {
           private AtomicInteger t = new AtomicInteger(1);
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"mypool_t"+t.getAndIncrement());
            }
        });

在这里插入图片描述
在这里插入图片描述

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
}

在这里插入图片描述
在这里插入图片描述

public static ExecutorService newWorkStealingPool() {
    return new ForkJoinPool
        (Runtime.getRuntime().availableProcessors(),
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
}

在这里插入图片描述
Fork/join框架介绍
在这里插入图片描述

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}

public ScheduledThreadPoolExecutor(int corePoolSize) {
	super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue());
}

在这里插入图片描述

//1.以固定的频率
scheduledExecutor.scheduleAtFixedRate(() -> {
    System.out.println("线程执行方法");
}, 0, 5, TimeUnit.SECONDS);

//2.以固定的延时
scheduledExecutor.scheduleWithFixedDelay(() -> {
    System.out.println("线程执行方法");
}, 1, 5, TimeUnit.SECONDS);

在这里插入图片描述

public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);

在这里插入图片描述

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);

在这里插入图片描述

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
	return new DelegatedScheduledExecutorService
		(new ScheduledThreadPoolExecutor(1));
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//拒绝策略使用(通过 new ThreadPoolExecutor.xxx 的方式引入)
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(), new ThreadPoolExecutor.AbortPolicy());

本文转载自:https://blog.csdn.net/lzb348110175/article/details/105843243


欢迎关注公众号Java技术大本营,会不定期分享BAT面试资料等福利。

在这里插入图片描述


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值