zoukankan      html  css  js  c++  java
  • Java创建线程池的三种方法

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadDemo {
        public static void main(String[] args) throws InterruptedException {
    
            ExecutorService threadPool1 = Executors.newSingleThreadExecutor();// 单个线程池
            ExecutorService threadPool2 = Executors.newFixedThreadPool(6);// 固定线程池
            ExecutorService threadPool3 = Executors.newCachedThreadPool();// 可大可小线程池(线程池大小不固定)
            for (int i = 1; i < 10; i++) {
                threadPool1.execute(() -> {
                    System.out.println("Single当前线程池名称:" + Thread.currentThread().getName());
                });
            }
            TimeUnit.SECONDS.sleep(5);
            System.out.println("===================================================");
    
            for (int i = 1; i < 10; i++) {
                threadPool2.execute(() -> {
                    System.out.println("Fixed当前线程池名称:" + Thread.currentThread().getName());
                });
            }
            TimeUnit.SECONDS.sleep(5);
            System.out.println("===================================================");
    
            for (int i = 1; i < 10; i++) {
                threadPool3.execute(() -> {
                    System.out.println("Cached当前线程池名称:" + Thread.currentThread().getName());
                });
            }
        }
    }
  • 相关阅读:
    Redis面试题 总结
    C++ 自由存储区是否等价于堆?(转)
    线程同步方式
    epoll的原理 (一)(转)
    C/C++ 中 volatile 关键字详解(转)
    Linux堆内存管理
    找出数组中出现次数超过一半的数
    剑指offer-复杂链表的复制
    已知二叉树前序中序遍历重建二叉树
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/nginxTest/p/15745346.html
Copyright © 2011-2022 走看看