任务
public class Task implements Runnable {
/** taskId */
private int taskId;
/**
* construct
*
* @param taskId
*/
public Task(int taskId) {
this.taskId = taskId;
}
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()
+ " : looping --> " + i + " : taskId --> " + taskId);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
app
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool {
public static void main(String[] args) {
int limit = 3;
ExecutorService threadPool = Executors.newFixedThreadPool(limit);
for (int i = 0; i < 10; i++) {
threadPool.execute(new Task(i));
}
threadPool.shutdown();
}
}