日期:2014-05-20 浏览次数:20742 次
public class ThreadPoolTwo {
public static void main(String[] args) {
try{
//线程的池阻塞队列。
LinkedBlockingQueue<Runnable> tasks = new LinkedBlockingQueue<Runnable>(
10);
ExecutorService pool = new ThreadPoolExecutor(2, 2, 0L,
TimeUnit.MILLISECONDS, tasks);
CountDownLatch begin=new CountDownLatch(1);
for(int i=0;i<12;i++){
TaskOne task=new TaskOne(begin);
pool.submit(task);
}
begin.countDown();
pool.shutdown();
}catch(Exception e){
e.printStackTrace();
}
}
}
class TaskOne implements Runnable{
private CountDownLatch begin;
public TaskOne(CountDownLatch latch){
this.begin=latch;
}
@Override
public void run() {
try {
begin.await();
System.out.println(Thread.currentThread().getName()+"欢迎您");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class ThreadPoolTwo {
public static void main(String[] args) {
try{
//线程的池阻塞队列。
LinkedBlockingQueue<Runnable> tasks = new LinkedBlockingQueue<Runnable>(
10);
ExecutorService pool = new ThreadPoolExecutor(2, 2, 0L,
TimeUnit.MILLISECONDS, tasks);
CountDownLatch begin=new CountDownLatch(1);
for(int i=0;i<12;i++){
TaskOne task=new TaskOne(begin);
pool.submit(task);
}
begin.countDown();
pool.shutdown();
}catch(Exception e){
e.printStackTrace();
}
}
}
class TaskOne implements Runnable{
private CountDownLatch begin;
public TaskOne(CountDownLatch latch){
this.begin=latch;
}
@Override
public void run() {
try {
begin.await();
System.out.println(Thread.currentThread().getName()+"欢迎您");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}