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());
                });
            }
        }
    }
  • 相关阅读:
    Dangling Javadoc comment
    IntelliJ IDEA :Error(1, 1) java 非法字符 'ufeff'
    什么是webhook
    智能DNS
    filebeat 乱码
    windows,交换机syslog收集
    Rsyslog
    ntp
    centos7 -lvm卷组
    nginx安装
  • 原文地址:https://www.cnblogs.com/nginxTest/p/15745346.html
Copyright © 2011-2022 走看看