多线程主函数
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; } }
原文:https://www.cnblogs.com/littlehb/p/10716028.html