zoukankan      html  css  js  c++  java
  • Java使用线程池

    多线程主函数

    package UnitTest;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.*;
    
    @SuppressWarnings("unchecked")
    public class test {
        public static void main(String[] args) throws InterruptedException {
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
            List<Future> resultList = new ArrayList<>();
            for (int i = 1; i < 101; i++) {
                Future future = fixedThreadPool.submit(new ThreadWithRunnable(i));
                resultList.add(future);
            }
            fixedThreadPool.shutdown();
            fixedThreadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
            System.out.println("所有进程成功结束!");
        }
    }

    小函数

    package UnitTest;
    
    import java.util.concurrent.Callable;
    
    public class ThreadWithRunnable implements Callable {
        private int number;
        public ThreadWithRunnable(int number){
            this.number = number;
        }
    
        @Override
        public Object call() {
            System.out.println("开始线程:"+Thread.currentThread().getName()+"       传入参数:"+number);
            return number;
        }
    }
  • 相关阅读:
    NOIP2020 游记
    李超线段树
    选举「elections」
    Alt+数字输入
    素数
    CSP-S2020 爆炸记
    [CF487C] Prefix Product Sequence
    [CF489E] Hiking
    L2-019 悄悄关注 (25 分)
    L2-032 彩虹瓶 (25 分)
  • 原文地址:https://www.cnblogs.com/littlehb/p/10716028.html
Copyright © 2011-2022 走看看