当前位置:编程学习 > JAVA >>

java类库

 1CountDownLatch
CountDownLatch是一个倒数计数的锁,当倒数到0时触发事件,也就是开锁,其他人就可以进入了。
在一些应用场合中,需要等待某个条件达到要求后才能做后面的事情;同时当线程都完成后也会触发事件,以便进行后面的操作。
CountDownLatch最重要的方法是countDown()和await(),前者主要是倒数一次,后者是等待倒数到0,如果没有到达0,就只有阻塞等待了。
下面的例子简单的说明了CountDownLatch的使用方法,模拟了100米赛跑,10名选手已经准备就绪,只等裁判一声令下。当所有人都到达终点时,比赛结束。
package com.eyesmore.concurrent;   
  
import java.util.concurrent.CountDownLatch;   
import java.util.concurrent.ExecutorService;   
import java.util.concurrent.Executors;   
  
public class CountDownLatchDemo {   
  
    private static final int PLAY_AMOUNT = 10;   
       
    public static void main(String[] args) {   
  
        /*
          * 比赛开始:只要裁判说开始,那么所有跑步选手就可以开始跑了
          * */  
         CountDownLatch begin = new CountDownLatch(1);   
           
        /*
          * 每个队员跑到末尾时,则报告一个到达,所有人员都到达时,则比赛结束
          * */  
         CountDownLatch end = new CountDownLatch(PLAY_AMOUNT);   
         Player[] plays = new Player[PLAY_AMOUNT];   
        for(int i = 0;i<PLAY_AMOUNT;i++) {   
             plays[i] = new Player(i+1,begin,end);   
         }   
         ExecutorService exe = Executors.newFixedThreadPool(PLAY_AMOUNT);   
        for(Player p : plays) {//各就各位   
             exe.execute(p);   
         }   
         System.out.println("比赛开始");   
         begin.countDown();//宣布开始   
        try {   
             end.await();//等待结束   
         } catch (InterruptedException e) {   
             e.printStackTrace();   
         } finally {   
             System.out.println("比赛结束");   
         }   
           
        //注意:此时main线程已经要结束了,但是exe线程如果不关闭是不会结束的   
         exe.shutdown();   
     }   
  
}   
  
  
class Player implements Runnable {   
  
    private int id;   
       
    private CountDownLatch begin;   
       
    private CountDownLatch end;   
  
    public Player(int id, CountDownLatch begin, CountDownLatch end) {   
        super();   
        this.id = id;   
        this.begin = begin;   
        this.end = end;   
     }   
  
    public void run() {   
        try {   
             begin.await();//必须等到裁判countdown到0的时候才开始   
             Thread.sleep((long)(Math.random()*100));//模拟跑步需要的时间   
             System.out.println("Play "+id+" has arrived. ");   
               
         } catch (InterruptedException e) {   
             e.printStackTrace();   
         } finally {   
             end.countDown();//向评委报告跑到终点了   
         }   
     }   
}  
 
2ThreadPoolExecutor
线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize,

long keepAliveTime, TimeUnit unit,

BlockingQueue<Runnable> workQueue,

RejectedExecutionHandler handler)

corePoolSize: 线程池维护线程的最少数量

maximumPoolSize:线程池维护线程的最大数量

keepAliveTime: 线程池维护线程所允许的空闲时间

unit: 线程池维护线程所允许的空闲时间的单位

workQueue: 线程池所使用的缓冲队列

handler: 线程池对拒绝任务的处理策略

一个任务通过 execute(Runnable)方法被添加到线程池,任务就是一个 Runnable类型的对象,任务的执行方法就是 Runnable类型对象的run()方法。

当一个任务通过execute(Runnable)方法欲添加到线程池时:

l  如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。

2  如果此时线程池中的数量等于 corePoolSize,但是缓冲队列 workQueue未满,那么任务被放入缓冲队列。

3  如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。

4  如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过 handler所指定的策略来处理此任务。也就是:处理任务的优先级为:核心线程corePoolSize、任务队列workQueue、最大线程maximumPoolSize,如果三者都满了,使用handler处理被拒绝的任务。

5  当线程池中的

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,