Java多线程——线程池使用示例
Posted <・)))><<
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java多线程——线程池使用示例相关的知识,希望对你有一定的参考价值。
示例代码:
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();
}
}
}
}
以上是关于Java多线程——线程池使用示例的主要内容,如果未能解决你的问题,请参考以下文章