JDK 1.5 使用java.util.concurrent 开发并发应用

简单的并发应用

JDK 1.5 API 中增加了新的包.

java.util.concurrent  在并发编程中很常用的实用工具类。 
java.util.concurrent.locks  为锁定和等待条件提供一个框架的接口和类,它不同于内置同步和监视器。

此包下的类图

 

常用的类 :

Executors             提供建立线程池或线程工厂的方法.

ExecutorService   提供了管理终止的方法,以及可为跟踪一个或多个异步任务执行状况而生成 Future 的方法。

Executor               可以简单理解为一个线程的执行者.

Future                  表示异步计算的结果,对执行的线程的状态检查,可以获得执行结果.

FutureTask           此类提供了对 Future 的基本实现,可使用 FutureTask 包装Callable 或Runnable对象。所以可将 FutureTask 提交给Executor 执行。

Callable                线程要执行而去实现的接口

Semaphore          一个计数信号量,通常用于限制可以访问某些资源(物理或逻辑的)的线程数目。

更加详细类说明和方法的应用,请可以查阅API

以下是简单的例子 :

(限制并发线程数量)

  1. public   static   void  main(String[] args) {   
  2.          // 线程池     
  3.         ExecutorService exec = Executors.newCachedThreadPool();     
  4.          // 只能5个线程同时访问     
  5.          final  Semaphore semp =  new  Semaphore( 5 );     
  6.          // 模拟20个客户端访问     
  7.          for  ( int  index =  0 ; index <  20 ; index++) {     
  8.              final   int  NO = index;   
  9.             Runnable run =  new  Runnable() {   
  10.                  public   void  run() {   
  11.                      try  {   
  12.                          // 获取许可   
  13.                         semp.acquire();   
  14.                         System.out.println( "thread:"  + NO);   
  15.                         Thread.sleep(( long ) (Math.random() *  1000 ));   
  16.                          // 访问完后,释放   
  17.                         semp.release();   
  18.                     }  catch  (InterruptedException e) {   
  19.                     }   
  20.                 }   
  21.             };   
  22.             exec.execute(run);     
  23.         }     
  24.          // 退出线程池     
  25.         exec.shutdown();     
  26.     }    

 

简单的线程池管理类:

  1. import  java.util.concurrent.Callable;   
  2. import  java.util.concurrent.ExecutionException;   
  3. import  java.util.concurrent.ExecutorService;   
  4. import  java.util.concurrent.Executors;   
  5. import  java.util.concurrent.Future;   
  6. import  java.util.concurrent.FutureTask;   
  7. import  java.util.concurrent.Semaphore;   
  8. /**  
  9.  * 线程池的管理类  
  10.  * 通过静态方法获得此类的实例  
  11.  */   
  12. public   class  ThreadPoolManager<T> {   
  13.      private  ExecutorService service =  null ;   
  14.      private   final  Semaphore semp ;     
  15.        
  16.      private   int  maxNum  =  3 ;   
  17.        
  18.      private   static  ThreadPoolManager me =  null ;   
  19.      public   static  ThreadPoolManager getInstance(){   
  20.          if (me ==  null ){   
  21.             me =  new  ThreadPoolManager();   
  22.         }   
  23.          return  me;   
  24.     }   
  25.      public   static  ThreadPoolManager getInstance( int  maxNum){   
  26.          if (me ==  null ){   
  27.             me =  new  ThreadPoolManager(maxNum);   
  28.         }   
  29.          return  me;   
  30.     }   
  31.      private  ThreadPoolManager(){   
  32.         service = Executors.newCachedThreadPool();   
  33.         semp =  new  Semaphore(maxNum);   
  34.     }   
  35.      private  ThreadPoolManager( int  maxNum){   
  36.          this .maxNum = maxNum;   
  37.         service = Executors.newCachedThreadPool();   
  38.         semp =  new  Semaphore(maxNum);   
  39.     }   
  40.        
  41.      /**  
  42.      * 执行线程任务,返回结果.  
  43.      * 如果线程池数量全部占用,则此线程阻塞.  
  44.      * 线程运行的传入参数类的实现方法.  
  45.      * @param task 实现java.util.concurrent.Callable接口的类  
  46.      * @return 返回线程运行的结果  
  47.      * @throws InterruptedException  
  48.      * @throws ExecutionException  
  49.      */   
  50.      public  T runTask(Callable<T> task)  throws  InterruptedException, ExecutionException{   
  51.         semp.acquire(); //使用信号标记,如果超过个数会造成线程阻塞   
  52.         Future<T> f = service.submit(task); //加载线程任务,并且执行   
  53.         semp.release(); //信号释放   
  54.          return  f.get(); //返回线程处理的结果,如果此线程尚未处理完成,则线程阻塞直到返回结果   
  55.     }   
  56.        
  57.      /**  
  58.      * @return 返回线程可用数量  
  59.      */   
  60.      public   synchronized   int  getavailableNum(){   
  61.          return   this .semp.availablePermits();   
  62.     }   
  63.        
  64.      /**  
  65.      * 关闭线程池   
  66.      */   
  67.      public   final   void  close(){   
  68.         service.shutdown();   
  69.     }   

 

博文来源:http://ajava.org/course/java/12954.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值