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;
}
}
上一篇:Mac终端使用技巧 切换到其他路径和目录


下一篇:(转)利用JConsole工具监控java程序内存和JVM