多线程主函数
1package UnitTest; 2 3import java.util.ArrayList; 4import java.util.List; 5import java.util.concurrent.*; 6 7@SuppressWarnings("unchecked") 8public class test { 9 public static void main(String[] args) throws InterruptedException { 10 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10); 11 List<Future> resultList = new ArrayList<>(); 12 for (int i = 1; i < 101; i++) { 13 Future future = fixedThreadPool.submit(new ThreadWithRunnable(i)); 14 resultList.add(future); 15 } 16 fixedThreadPool.shutdown(); 17 fixedThreadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 18 System.out.println("所有进程成功结束!"); 19 } 20}
小函数
1package UnitTest; 2 3import java.util.concurrent.Callable; 4 5public class ThreadWithRunnable implements Callable { 6 private int number; 7 public ThreadWithRunnable(int number){ 8 this.number = number; 9 } 10 11 @Override 12 public Object call() { 13 System.out.println("开始线程:"+Thread.currentThread().getName()+" 传入参数:"+number); 14 return number; 15 } 16}