首页 > 编程语言 > 详细

Java使用线程池

时间:2019-04-16 12:37:10      阅读:107      评论:0      收藏:0      [点我收藏+]

多线程主函数

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;
    }
}

 

Java使用线程池

原文:https://www.cnblogs.com/littlehb/p/10716028.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!