示例代码:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolTest {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
String[] nameArr = new String[] { "孙悟空", "猪八戒", "至尊宝", "唐三藏", "沙悟净", "白晶晶", "zifeiy" };
for (String name : nameArr) {
pool.submit(new ThreadPoolTestWorker(name));
}
}
}
class ThreadPoolTestWorker implements Runnable {
private String name;
public ThreadPoolTestWorker(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 1; i <= 3; i ++) {
System.out.println(name + " call you " + i + " times ...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
原文地址:https://www.cnblogs.com/zifeiy/p/9785659.html
时间: 2024-10-11 04:59:37