java并发库样例

package com.suning;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PoolTest {

/**
* @param args
*/
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(3);
for(int i=1;i<10;i++){
final int task = i;
threadPool.execute(new Runnable(){
public void run(){
for(int j=0;j<10;j++){
try {
Thread.sleep(20);
System.out.println(Thread.currentThread().getName() + " is loop of " + j + " for task " + task);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(task == 9 && j == 9){
return ;
}
}

}

});
}
System.out.println("all of 10 tasks have committed! ");
threadPool.shutdown();
}

}

猜你喜欢

转载自xiyuhanfei.iteye.com/blog/2151699
今日推荐