java.util.concurrent多线程框架

JDK5中的一个亮点就是将Doug Lea的并发库引入到Java标准库中。DougLea确实是一个牛人,能教书,能出书,能编码,不过这在国外还是比较普遍的,而国内的教授们就相差太远了。

<wbr><wbr><wbr>一般的服务器都需要线程池,比如Web、FTP等服务器,不过它们一般都自己实现了线程池,比如以前介绍过的Tomcat、Resin和Jetty等,现在有了JDK5,我们就没有必要重复造车轮了,直接使用就可以,何况使用也很方便,性能也非常高。<br><br></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. import<wbr>java.util.concurrent.ExecutorService;<wbr><wbr></wbr></wbr></wbr>
  3. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  4. public<wbr></wbr>class<wbr>TestThreadPool<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  5. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String<wbr>args[])<wbr></wbr></wbr></wbr>throws<wbr>InterruptedException<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  6. //<wbr>only<wbr>two<wbr>threads</wbr></wbr></wbr><wbr><wbr></wbr></wbr>
  7. ExecutorService<wbr>exec<wbr>=<wbr>Executors.newFixedThreadPool(</wbr></wbr></wbr>2);<wbr><wbr></wbr></wbr>
  8. for(int<wbr>index<wbr>=<wbr></wbr></wbr></wbr>0;<wbr>index<wbr>&lt;<wbr></wbr></wbr></wbr>100;<wbr>index++)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  9. Runnable<wbr>run<wbr>=<wbr></wbr></wbr></wbr>new<wbr>Runnable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  10. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  11. long<wbr>time<wbr>=<wbr>(</wbr></wbr></wbr>long)<wbr>(Math.random()<wbr>*<wbr></wbr></wbr></wbr>1000);<wbr><wbr></wbr></wbr>
  12. System.out.println(“Sleeping<wbr>”<wbr>+<wbr>time<wbr>+<wbr>“ms”);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  13. try<wbr>{<wbr><wbr></wbr></wbr></wbr>
  14. Thread.sleep(time);<wbr><wbr></wbr></wbr>
  15. }<wbr></wbr>catch<wbr>(InterruptedException<wbr>e)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  16. } <wbr><wbr></wbr></wbr>
  17. } <wbr><wbr></wbr></wbr>
  18. }; <wbr><wbr></wbr></wbr>
  19. exec.execute(run);<wbr><wbr></wbr></wbr>
  20. } <wbr><wbr></wbr></wbr>
  21. //<wbr>must<wbr>shutdown</wbr></wbr><wbr><wbr></wbr></wbr>
  22. exec.shutdown();<wbr><wbr></wbr></wbr>
  23. } <wbr><wbr></wbr></wbr>
  24. }<wbr></wbr>


<wbr><wbr><wbr>上面是一个简单的例子,使用了2个大小的线程池来处理100个线程。但有一个问题:在for循环的过程中,会等待线程池有空闲的线程,所以主线程会阻塞的。为了解决这个问题,一般启动一个线程来做for循环,就是为了避免由于线程池满了造成主线程阻塞。不过在这里我没有这样处理。[重要修正:经过测试,即使线程池大小小于实际线程数大小,线程池也不会阻塞的,这与Tomcat的线程池不同,它将Runnable实例放到一个“无限”的BlockingQueue中,所以就不用一个线程启动for循环,DougLea果然厉害]<br><br><wbr><wbr><wbr>另外它使用了Executors的静态函数生成一个固定的线程池,顾名思义,线程池的线程是不会释放的,即使它是Idle。这就会产生性能问题,比如如果线程池的大小为200,当全部使用完毕后,所有的线程会继续留在池中,相应的内存和线程切换(while(true)+sleep循环)都会增加。如果要避免这个问题,就必须直接使用ThreadPoolExecutor()来构造。可以像Tomcat的线程池一样设置“最大线程数”、“最小线程数”和“空闲线程keepAlive的时间”。通过这些可以基本上替换Tomcat的线程池实现方案。<br><br><wbr><wbr><wbr>需要注意的是线程池必须使用shutdown来显式关闭,否则主线程就无法退出。shutdown也不会阻塞主线程。<br><br><wbr><wbr><wbr>许多长时间运行的应用有时候需要定时运行任务完成一些诸如统计、优化等工作,比如在电信行业中处理用户话单时,需要每隔1分钟处理话单;网站每天凌晨统计用户访问量、用户数;大型超时凌晨3点统计当天销售额、以及最热卖的商品;每周日进行数据库备份;公司每个月的10号计算工资并进行转帐等,这些都是定时任务。通过java的并发库concurrent可以轻松的完成这些任务,而且非常的简单。<br><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. import<wbr></wbr>static<wbr>java.util.concurrent.TimeUnit.SECONDS;<wbr><wbr></wbr></wbr></wbr>
  3. import<wbr>java.util.Date;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>java.util.concurrent.ScheduledExecutorService<wbr>;<wbr><wbr></wbr></wbr></wbr></wbr>
  6. import<wbr>java.util.concurrent.ScheduledFuture;<wbr><wbr></wbr></wbr></wbr>
  7. public<wbr></wbr>class<wbr>TestScheduledThread<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  8. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String[]<wbr>args)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  9. final<wbr>ScheduledExecutorService<wbr><wbr>scheduler<wbr>=<wbr>Executors<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  10. .newScheduledThreadPool(2);<wbr><wbr></wbr></wbr>
  11. final<wbr>Runnable<wbr>beeper<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>Runnable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  12. int<wbr>count<wbr>=<wbr></wbr></wbr></wbr>0;<wbr><wbr></wbr></wbr>
  13. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  14. System.out.println(new<wbr>Date()<wbr>+<wbr>”<wbr>beep<wbr>”<wbr>+<wbr>(++count));<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  15. } <wbr><wbr></wbr></wbr>
  16. }; <wbr><wbr></wbr></wbr>
  17. //<wbr>1秒钟后运行,并每隔2秒运行一次</wbr><wbr><wbr></wbr></wbr>
  18. final<wbr>ScheduledFuture<wbr>beeperHandle<wbr>=<wbr>scheduler.scheduleAtFixedRate(<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  19. beeper,<wbr></wbr>1,<wbr></wbr>2,<wbr>SECONDS);<wbr><wbr></wbr></wbr></wbr>
  20. //<wbr>2秒钟后运行,并每次在上次任务运行完后等待5秒后重新运行</wbr><wbr><wbr></wbr></wbr>
  21. final<wbr>ScheduledFuture<wbr>beeperHandle2<wbr>=<wbr>scheduler<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  22. .scheduleWithFixedDelay(beeper,<wbr></wbr>2,<wbr></wbr>5,<wbr>SECONDS);<wbr><wbr></wbr></wbr></wbr>
  23. //<wbr>30秒后结束关闭任务,并且关闭Scheduler</wbr><wbr><wbr></wbr></wbr>
  24. scheduler.schedule(new<wbr>Runnable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  25. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  26. beeperHandle.cancel(true);<wbr><wbr></wbr></wbr>
  27. beeperHandle2.cancel(true);<wbr><wbr></wbr></wbr>
  28. scheduler.shutdown();<wbr><wbr></wbr></wbr>
  29. } <wbr><wbr></wbr></wbr>
  30. },<wbr></wbr>30,<wbr>SECONDS);<wbr><wbr></wbr></wbr></wbr>
  31. } <wbr><wbr></wbr></wbr>
  32. }<wbr><wbr></wbr></wbr>


<wbr><wbr><wbr>为了退出进程,上面的代码中加入了关闭Scheduler的操作。而对于24小时运行的应用而言,是没有必要关闭Scheduler的。<br><br><wbr><wbr><wbr>在实际应用中,有时候需要多个线程同时工作以完成同一件事情,而且在完成过程中,往往会等待其他线程都完成某一阶段后再执行,等所有线程都到达某一个阶段后再统一执行。<br><br><wbr><wbr><wbr>比如有几个旅行团需要途经深圳、广州、韶关、长沙最后到达武汉。旅行团中有自驾游的,有徒步的,有乘坐旅游大巴的;这些旅行团同时出发,并且每到一个目的地,都要等待其他旅行团到达此地后再同时出发,直到都到达终点站武汉。<br><br><wbr><wbr><wbr>这时候CyclicBarrier就可以派上用场。CyclicBarrier最重要的属性就是参与者个数,另外最要方法是await()。当所有线程都调用了await()后,就表示这些线程都可以继续执行,否则就会等待。<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. import<wbr>java.text.SimpleDateFormat;<wbr><wbr></wbr></wbr></wbr>
  3. import<wbr>java.util.Date;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>java.util.concurrent.BrokenBarrierException;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>java.util.concurrent.CyclicBarrier;<wbr><wbr></wbr></wbr></wbr>
  6. import<wbr>java.util.concurrent.ExecutorService;<wbr><wbr></wbr></wbr></wbr>
  7. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  8. public<wbr></wbr>class<wbr>TestCyclicBarrier<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  9. //<wbr>徒步需要的时间:<wbr>Shenzhen,<wbr>Guangzhou,<wbr>Shaoguan,<wbr>Changsha,<wbr>Wuhan</wbr></wbr></wbr></wbr></wbr></wbr><wbr><wbr></wbr></wbr>
  10. private<wbr></wbr>static<wbr></wbr>int[]<wbr>timeWalk<wbr>=<wbr>{<wbr></wbr></wbr></wbr></wbr>5,<wbr></wbr>8,<wbr></wbr>15,<wbr></wbr>15,<wbr></wbr>10<wbr>};<wbr><wbr></wbr></wbr></wbr>
  11. //<wbr>自驾游</wbr><wbr><wbr></wbr></wbr>
  12. private<wbr></wbr>static<wbr></wbr>int[]<wbr>timeSelf<wbr>=<wbr>{<wbr></wbr></wbr></wbr></wbr>1,<wbr></wbr>3,<wbr></wbr>4,<wbr></wbr>4,<wbr></wbr>5<wbr>};<wbr><wbr></wbr></wbr></wbr>
  13. //<wbr>旅游大巴</wbr><wbr><wbr></wbr></wbr>
  14. private<wbr></wbr>static<wbr></wbr>int[]<wbr>timeBus<wbr>=<wbr>{<wbr></wbr></wbr></wbr></wbr>2,<wbr></wbr>4,<wbr></wbr>6,<wbr></wbr>6,<wbr></wbr>7<wbr>};<wbr><wbr></wbr></wbr></wbr>
  15. <wbr><wbr></wbr></wbr>
  16. static<wbr>String<wbr>now()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  17. SimpleDateFormat<wbr>sdf<wbr>=<wbr></wbr></wbr></wbr>new<wbr>SimpleDateFormat(“HH:mm:ss”);<wbr><wbr></wbr></wbr></wbr>
  18. return<wbr>sdf.format(</wbr>new<wbr>Date())<wbr>+<wbr>“:<wbr>“;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  19. } <wbr><wbr></wbr></wbr>
  20. <wbr><wbr></wbr></wbr>
  21. static<wbr></wbr>class<wbr>Tour<wbr></wbr></wbr>implements<wbr>Runnable<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  22. private<wbr></wbr>int[]<wbr>times;<wbr><wbr></wbr></wbr></wbr>
  23. private<wbr>CyclicBarrier<wbr>barrier;<wbr><wbr></wbr></wbr></wbr></wbr>
  24. private<wbr>String<wbr>tourName;<wbr><wbr></wbr></wbr></wbr></wbr>
  25. public<wbr>Tour(CyclicBarrier<wbr>barrier,<wbr>String<wbr>tourName,<wbr></wbr></wbr></wbr></wbr></wbr>int[]<wbr>times)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  26. this.times<wbr>=<wbr>times;<wbr><wbr></wbr></wbr></wbr></wbr>
  27. this.tourName<wbr>=<wbr>tourName;<wbr><wbr></wbr></wbr></wbr></wbr>
  28. this.barrier<wbr>=<wbr>barrier;<wbr><wbr></wbr></wbr></wbr></wbr>
  29. } <wbr><wbr></wbr></wbr>
  30. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  31. try<wbr>{<wbr><wbr></wbr></wbr></wbr>
  32. Thread.sleep(times[0]<wbr>*<wbr></wbr></wbr>1000);<wbr><wbr></wbr></wbr>
  33. System.out.println(now()<wbr>+<wbr>tourName<wbr>+<wbr>”<wbr>Reached<wbr>Shenzhen”);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  34. barrier.await();<wbr><wbr></wbr></wbr>
  35. Thread.sleep(times[1]<wbr>*<wbr></wbr></wbr>1000);<wbr><wbr></wbr></wbr>
  36. System.out.println(now()<wbr>+<wbr>tourName<wbr>+<wbr>”<wbr>Reached<wbr>Guangzhou”);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  37. barrier.await();<wbr><wbr></wbr></wbr>
  38. Thread.sleep(times[2]<wbr>*<wbr></wbr></wbr>1000);<wbr><wbr></wbr></wbr>
  39. System.out.println(now()<wbr>+<wbr>tourName<wbr>+<wbr>”<wbr>Reached<wbr>Shaoguan”);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  40. barrier.await();<wbr><wbr></wbr></wbr>
  41. Thread.sleep(times[3]<wbr>*<wbr></wbr></wbr>1000);<wbr><wbr></wbr></wbr>
  42. System.out.println(now()<wbr>+<wbr>tourName<wbr>+<wbr>”<wbr>Reached<wbr>Changsha”);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  43. barrier.await();<wbr><wbr></wbr></wbr>
  44. Thread.sleep(times[4]<wbr>*<wbr></wbr></wbr>1000);<wbr><wbr></wbr></wbr>
  45. System.out.println(now()<wbr>+<wbr>tourName<wbr>+<wbr>”<wbr>Reached<wbr>Wuhan”);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  46. barrier.await();<wbr><wbr></wbr></wbr>
  47. }<wbr></wbr>catch<wbr>(InterruptedException<wbr>e)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  48. }<wbr></wbr>catch<wbr>(BrokenBarrierException<wbr>e)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  49. } <wbr><wbr></wbr></wbr>
  50. } <wbr><wbr></wbr></wbr>
  51. } <wbr><wbr></wbr></wbr>
  52. <wbr><wbr></wbr></wbr>
  53. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String[]<wbr>args)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  54. //<wbr>三个旅行团</wbr><wbr><wbr></wbr></wbr>
  55. CyclicBarrier<wbr>barrier<wbr>=<wbr></wbr></wbr></wbr>new<wbr>CyclicBarrier(</wbr>3);<wbr><wbr></wbr></wbr>
  56. ExecutorService<wbr>exec<wbr>=<wbr>Executors.newFixedThreadPool(</wbr></wbr></wbr>3);<wbr><wbr></wbr></wbr>
  57. exec.submit(new<wbr>Tour(barrier,<wbr>“WalkTour”,<wbr>timeWalk));<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  58. exec.submit(new<wbr>Tour(barrier,<wbr>“SelfTour”,<wbr>timeSelf));<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  59. exec.submit(new<wbr>Tour(barrier,<wbr>“BusTour”,<wbr>timeBus));<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  60. exec.shutdown();<wbr><wbr></wbr></wbr>
  61. } <wbr><wbr></wbr></wbr>
  62. }<wbr><wbr></wbr></wbr>


运行结果:
00:02:25: SelfTour Reached Shenzhen
00:02:25: BusTour Reached Shenzhen
00:02:27: WalkTour Reached Shenzhen
00:02:30: SelfTour Reached Guangzhou
00:02:31: BusTour Reached Guangzhou
00:02:35: WalkTour Reached Guangzhou
00:02:39: SelfTour Reached Shaoguan
00:02:41: BusTour Reached Shaoguan

<wbr><wbr><wbr>并发库中的BlockingQueue是一个比较好玩的类,顾名思义,就是阻塞队列。该类主要提供了两个方法put()和take(),前者将一个对象放到队列中,如果队列已经满了,就等待直到有空闲节点;后者从head取一个对象,如果没有对象,就等待直到有可取的对象。<br><br><wbr><wbr><wbr>下面的例子比较简单,一个读线程,用于将要处理的文件对象添加到阻塞队列中,另外四个写线程用于取出文件对象,为了模拟写操作耗时长的特点,特让线程睡眠一段随机长度的时间。另外,该Demo也使用到了线程池和原子整型(AtomicInteger),AtomicInteger可以在并发情况下达到原子化更新,避免使用了synchronized,而且性能非常高。由于阻塞队列的put和take操作会阻塞,为了使线程退出,特在队列中添加了一个“标识”,算法中也叫“哨兵”,当发现这个哨兵后,写线程就退出。<br><br><wbr><wbr><wbr>当然线程池也要显式退出了。<br><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. import<wbr>java.io.File;<wbr><wbr></wbr></wbr></wbr>
  3. import<wbr>java.io.FileFilter;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>java.util.concurrent.BlockingQueue;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>java.util.concurrent.ExecutorService;<wbr><wbr></wbr></wbr></wbr>
  6. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  7. import<wbr>java.util.concurrent.LinkedBlockingQueue;<wbr><wbr></wbr></wbr></wbr>
  8. import<wbr>java.util.concurrent.atomic.AtomicInteger;<wbr><wbr></wbr></wbr></wbr>
  9. <wbr><wbr></wbr></wbr>
  10. public<wbr></wbr>class<wbr>TestBlockingQueue<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  11. static<wbr></wbr>long<wbr>randomTime()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  12. return<wbr>(</wbr>long)<wbr>(Math.random()<wbr>*<wbr></wbr></wbr></wbr>1000);<wbr><wbr></wbr></wbr>
  13. } <wbr><wbr></wbr></wbr>
  14. <wbr><wbr></wbr></wbr>
  15. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String[]<wbr>args)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  16. //<wbr>能容纳100个文件</wbr><wbr><wbr></wbr></wbr>
  17. final<wbr>BlockingQueue<wbr>queue<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>LinkedBlockingQueue(</wbr>100);<wbr><wbr></wbr></wbr>
  18. //<wbr>线程池</wbr><wbr><wbr></wbr></wbr>
  19. final<wbr>ExecutorService<wbr>exec<wbr>=<wbr>Executors.newFixedThreadPool(</wbr></wbr></wbr></wbr>5);<wbr><wbr></wbr></wbr>
  20. final<wbr>File<wbr>root<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>File(“F:\\JavaLib”);<wbr><wbr></wbr></wbr></wbr>
  21. //<wbr>完成标志</wbr><wbr><wbr></wbr></wbr>
  22. final<wbr>File<wbr>exitFile<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>File(“”);<wbr><wbr></wbr></wbr></wbr>
  23. //<wbr>读个数</wbr><wbr><wbr></wbr></wbr>
  24. final<wbr>AtomicInteger<wbr>rc<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>AtomicInteger();<wbr><wbr></wbr></wbr></wbr>
  25. //<wbr>写个数</wbr><wbr><wbr></wbr></wbr>
  26. final<wbr>AtomicInteger<wbr>wc<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>AtomicInteger();<wbr><wbr></wbr></wbr></wbr>
  27. //<wbr>读线程</wbr><wbr><wbr></wbr></wbr>
  28. Runnable<wbr>read<wbr>=<wbr></wbr></wbr></wbr>new<wbr>Runnable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  29. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  30. scanFile(root);<wbr><wbr></wbr></wbr>
  31. scanFile(exitFile);<wbr><wbr></wbr></wbr>
  32. } <wbr><wbr></wbr></wbr>
  33. <wbr><wbr></wbr></wbr>
  34. public<wbr></wbr>void<wbr>scanFile(File<wbr>file)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  35. if<wbr>(file.isDirectory())<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  36. File[]<wbr>files<wbr>=<wbr>file.listFiles(</wbr></wbr></wbr>new<wbr>FileFilter()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  37. public<wbr></wbr>boolean<wbr>accept(File<wbr>pathname)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  38. return<wbr>pathname.isDirectory()<wbr><wbr></wbr></wbr></wbr>
  39. ||<wbr>pathname.getPath().endsWith(“.java”);<wbr><wbr></wbr></wbr></wbr>
  40. } <wbr><wbr></wbr></wbr>
  41. }); <wbr><wbr></wbr></wbr>
  42. for<wbr>(File<wbr>one<wbr>:<wbr>files)<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  43. scanFile(one);<wbr><wbr></wbr></wbr>
  44. }<wbr></wbr>else<wbr>{<wbr><wbr></wbr></wbr></wbr>
  45. try<wbr>{<wbr><wbr></wbr></wbr></wbr>
  46. int<wbr>index<wbr>=<wbr>rc.incrementAndGet();<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  47. System.out.println(“Read0:<wbr>”<wbr>+<wbr>index<wbr>+<wbr>”<wbr>“<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  48. +<wbr>file.getPath());<wbr><wbr></wbr></wbr></wbr>
  49. queue.put(file);<wbr><wbr></wbr></wbr>
  50. }<wbr></wbr>catch<wbr>(InterruptedException<wbr>e)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  51. } <wbr><wbr></wbr></wbr>
  52. } <wbr><wbr></wbr></wbr>
  53. } <wbr><wbr></wbr></wbr>
  54. }; <wbr><wbr></wbr></wbr>
  55. exec.submit(read);<wbr><wbr></wbr></wbr>
  56. //<wbr>四个写线程</wbr><wbr><wbr></wbr></wbr>
  57. for<wbr>(</wbr>int<wbr>index<wbr>=<wbr></wbr></wbr></wbr>0;<wbr>index<wbr>&lt;<wbr></wbr></wbr></wbr>4;<wbr>index++)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  58. //<wbr>write<wbr>thread</wbr></wbr><wbr><wbr></wbr></wbr>
  59. final<wbr></wbr>int<wbr>NO<wbr>=<wbr>index;<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  60. Runnable<wbr>write<wbr>=<wbr></wbr></wbr></wbr>new<wbr>Runnable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  61. String<wbr>threadName<wbr>=<wbr>“Write”<wbr>+<wbr>NO;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  62. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  63. while<wbr>(</wbr>true)<wbr>{<wbr><wbr></wbr></wbr></wbr>
  64. try<wbr>{<wbr><wbr></wbr></wbr></wbr>
  65. Thread.sleep(randomTime());<wbr><wbr></wbr></wbr>
  66. int<wbr>index<wbr>=<wbr>wc.incrementAndGet();<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  67. File<wbr>file<wbr>=<wbr>queue.take();<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  68. //<wbr>队列已经无对象</wbr><wbr><wbr></wbr></wbr>
  69. if<wbr>(file<wbr>==<wbr>exitFile)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  70. //<wbr>再次添加”标志”,以让其他线程正常退出</wbr><wbr><wbr></wbr></wbr>
  71. queue.put(exitFile);<wbr><wbr></wbr></wbr>
  72. break;<wbr><wbr></wbr></wbr>
  73. } <wbr><wbr></wbr></wbr>
  74. System.out.println(threadName<wbr>+<wbr>“:<wbr>”<wbr>+<wbr>index<wbr>+<wbr>”<wbr>“<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  75. +<wbr>file.getPath());<wbr><wbr></wbr></wbr></wbr>
  76. }<wbr></wbr>catch<wbr>(InterruptedException<wbr>e)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  77. } <wbr><wbr></wbr></wbr>
  78. } <wbr><wbr></wbr></wbr>
  79. } <wbr><wbr></wbr></wbr>
  80. }; <wbr><wbr></wbr></wbr>
  81. exec.submit(write);<wbr><wbr></wbr></wbr>
  82. } <wbr><wbr></wbr></wbr>
  83. exec.shutdown();<wbr><wbr></wbr></wbr>
  84. } <wbr><wbr></wbr></wbr>
  85. }<wbr><wbr></wbr></wbr>


<wbr><wbr><wbr>从名字可以看出,CountDownLatch是一个倒数计数的锁,当倒数到0时触发事件,也就是开锁,其他人就可以进入了。在一些应用场合中,需要等待某个条件达到要求后才能做后面的事情;同时当线程都完成后也会触发事件,以便进行后面的操作。</wbr></wbr></wbr>

<wbr>CountDownLatch最重要的方法是countDown()和await(),前者主要是倒数一次,后者是等待倒数到0,如果没有到达0,就只有阻塞等待了。<br><br><wbr><wbr><wbr>一个CountDouwnLatch实例是不能重复使用的,也就是说它是一次性的,锁一经被打开就不能再关闭使用了,如果想重复使用,请考虑使用CyclicBarrier。<br><br><wbr><wbr><wbr>下面的例子简单的说明了CountDownLatch的使用方法,模拟了100米赛跑,10名选手已经准备就绪,只等裁判一声令下。当所有人都到达终点时,比赛结束。<br><br><wbr><wbr><wbr>同样,线程池需要显式shutdown。<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. <wbr><wbr></wbr></wbr>
  3. import<wbr>java.util.concurrent.CountDownLatch;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>java.util.concurrent.ExecutorService;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  6. <wbr><wbr></wbr></wbr>
  7. public<wbr></wbr>class<wbr>TestCountDownLatch<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  8. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String[]<wbr>args)<wbr></wbr></wbr></wbr>throws<wbr>InterruptedException<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  9. //<wbr>开始的倒数锁</wbr><wbr><wbr></wbr></wbr>
  10. final<wbr>CountDownLatch<wbr>begin<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>CountDownLatch(</wbr>1);<wbr><wbr></wbr></wbr>
  11. //<wbr>结束的倒数锁</wbr><wbr><wbr></wbr></wbr>
  12. final<wbr>CountDownLatch<wbr>end<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>CountDownLatch(</wbr>10);<wbr><wbr></wbr></wbr>
  13. //<wbr>十名选手</wbr><wbr><wbr></wbr></wbr>
  14. final<wbr>ExecutorService<wbr>exec<wbr>=<wbr>Executors.newFixedThreadPool(</wbr></wbr></wbr></wbr>10);<wbr><wbr></wbr></wbr>
  15. for(int<wbr>index<wbr>=<wbr></wbr></wbr></wbr>0;<wbr>index<wbr>&lt;<wbr></wbr></wbr></wbr>10;<wbr>index++)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  16. final<wbr></wbr>int<wbr>NO<wbr>=<wbr>index<wbr>+<wbr></wbr></wbr></wbr></wbr></wbr>1;<wbr><wbr></wbr></wbr>
  17. Runnable<wbr>run<wbr>=<wbr></wbr></wbr></wbr>new<wbr>Runnable(){<wbr><wbr></wbr></wbr></wbr>
  18. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  19. try<wbr>{<wbr><wbr></wbr></wbr></wbr>
  20. begin.await();<wbr><wbr></wbr></wbr>
  21. Thread.sleep((long)<wbr>(Math.random()<wbr>*<wbr></wbr></wbr></wbr>10000));<wbr><wbr></wbr></wbr>
  22. System.out.println(“No.”<wbr>+<wbr>NO<wbr>+<wbr>”<wbr>arrived”);<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  23. }<wbr></wbr>catch<wbr>(InterruptedException<wbr>e)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  24. }<wbr></wbr>finally<wbr>{<wbr><wbr></wbr></wbr></wbr>
  25. end.countDown();<wbr><wbr></wbr></wbr>
  26. } <wbr><wbr></wbr></wbr>
  27. } <wbr><wbr></wbr></wbr>
  28. }; <wbr><wbr></wbr></wbr>
  29. exec.submit(run);<wbr><wbr></wbr></wbr>
  30. } <wbr><wbr></wbr></wbr>
  31. System.out.println(“Game<wbr>Start”);<wbr><wbr></wbr></wbr></wbr>
  32. begin.countDown();<wbr><wbr></wbr></wbr>
  33. end.await();<wbr><wbr></wbr></wbr>
  34. System.out.println(“Game<wbr>Over”);<wbr><wbr></wbr></wbr></wbr>
  35. exec.shutdown();<wbr><wbr></wbr></wbr>
  36. } <wbr><wbr></wbr></wbr>
  37. }<wbr></wbr>

运行结果:
Game Start
No.4 arrived
No.1 arrived
No.7 arrived
No.9 arrived
No.3 arrived
No.2 arrived
No.8 arrived
No.10 arrived
No.6 arrived
No.5 arrived
Game Over

<wbr><wbr><wbr>有时候在实际应用中,某些操作很耗时,但又不是不可或缺的步骤。比如用网页浏览器浏览新闻时,最重要的是要显示文字内容,至于与新闻相匹配的图片就没有那么重要的,所以此时首先保证文字信息先显示,而图片信息会后显示,但又不能不显示,由于下载图片是一个耗时的操作,所以必须一开始就得下载。<wbr><br><br><wbr><wbr><wbr>Java的并发库的Future类就可以满足这个要求。Future的重要方法包括get()和cancel(),get()获取数据对象,如果数据没有加载,就会阻塞直到取到数据,而cancel()是取消数据加载。另外一个get(timeout)操作,表示如果在timeout时间内没有取到就失败返回,而不再阻塞。<br><br><wbr><wbr><wbr>下面的Demo简单的说明了Future的使用方法:一个非常耗时的操作必须一开始启动,但又不能一直等待;其他重要的事情又必须做,等完成后,就可以做不重要的事情。<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. <wbr><wbr></wbr></wbr>
  3. import<wbr>java.util.concurrent.Callable;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>java.util.concurrent.ExecutionException;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>java.util.concurrent.ExecutorService;<wbr><wbr></wbr></wbr></wbr>
  6. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  7. import<wbr>java.util.concurrent.Future;<wbr><wbr></wbr></wbr></wbr>
  8. <wbr><wbr></wbr></wbr>
  9. public<wbr></wbr>class<wbr>TestFutureTask<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  10. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String[]<wbr>args)</wbr></wbr>throws<wbr>InterruptedException,<wbr><wbr></wbr></wbr></wbr>
  11. ExecutionException<wbr>{<wbr><wbr></wbr></wbr></wbr>
  12. final<wbr>ExecutorService<wbr>exec<wbr>=<wbr>Executors.newFixedThreadPool(</wbr></wbr></wbr></wbr>5);<wbr><wbr></wbr></wbr>
  13. Callable<wbr>call<wbr>=<wbr></wbr></wbr></wbr>new<wbr>Callable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  14. public<wbr>String<wbr>call()<wbr></wbr></wbr></wbr>throws<wbr>Exception<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  15. Thread.sleep(1000<wbr>*<wbr></wbr></wbr>5);<wbr><wbr></wbr></wbr>
  16. return<wbr>“Other<wbr>less<wbr>important<wbr>but<wbr>longtime<wbr>things.”;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  17. } <wbr><wbr></wbr></wbr>
  18. }; <wbr><wbr></wbr></wbr>
  19. Future<wbr>task<wbr>=<wbr>exec.submit(call);<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  20. //<wbr>重要的事情</wbr><wbr><wbr></wbr></wbr>
  21. Thread.sleep(1000<wbr>*<wbr></wbr></wbr>3);<wbr><wbr></wbr></wbr>
  22. System.out.println(“Let’s<wbr></wbr>do<wbr>important<wbr>things.”);<wbr><wbr></wbr></wbr></wbr></wbr>
  23. //<wbr>其他不重要的事情</wbr><wbr><wbr></wbr></wbr>
  24. String<wbr>obj<wbr>=<wbr>task.get();<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  25. System.out.println(obj);<wbr><wbr></wbr></wbr>
  26. //<wbr>关闭线程池</wbr><wbr><wbr></wbr></wbr>
  27. exec.shutdown();<wbr><wbr></wbr></wbr>
  28. } <wbr><wbr></wbr></wbr>
  29. }<wbr><wbr></wbr></wbr>


运行结果:
Let’s do important things.
Other less important but longtime things.

<wbr><wbr><wbr>考虑以下场景:浏览网页时,浏览器了5个线程下载网页中的图片文件,由于图片大小、网站访问速度等诸多因素的影响,完成图片下载的时间就会有很大的不同。如果先下载完成的图片就会被先显示到界面上,反之,后下载的图片就后显示。<wbr><br><br><wbr><wbr><wbr>Java的并发库的CompletionService可以满足这种场景要求。该接口有两个重要方法:submit()和take()。submit用于提交一个runnable或者callable,一般会提交给一个线程池处理;而take就是取出已经执行完毕runnable或者callable实例的Future对象,如果没有满足要求的,就等待了。CompletionService还有一个对应的方法poll,该方法与take类似,只是不会等待,如果没有满足要求,就返回null对象。<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. <wbr><wbr></wbr></wbr>
  3. import<wbr>java.util.concurrent.Callable;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>java.util.concurrent.CompletionService;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>java.util.concurrent.ExecutionException;<wbr><wbr></wbr></wbr></wbr>
  6. import<wbr>java.util.concurrent.ExecutorCompletionServic<wbr>e;<wbr><wbr></wbr></wbr></wbr></wbr>
  7. import<wbr>java.util.concurrent.ExecutorService;<wbr><wbr></wbr></wbr></wbr>
  8. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  9. import<wbr>java.util.concurrent.Future;<wbr><wbr></wbr></wbr></wbr>
  10. <wbr><wbr></wbr></wbr>
  11. public<wbr></wbr>class<wbr>TestCompletionService<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  12. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String[]<wbr>args)<wbr></wbr></wbr></wbr>throws<wbr>InterruptedException,<wbr><wbr></wbr></wbr></wbr>
  13. ExecutionException<wbr>{<wbr><wbr></wbr></wbr></wbr>
  14. ExecutorService<wbr>exec<wbr>=<wbr>Executors.newFixedThreadPool(</wbr></wbr></wbr>10);<wbr><wbr></wbr></wbr>
  15. CompletionService<wbr>serv<wbr>=<wbr><wbr></wbr></wbr></wbr></wbr>
  16. new<wbr>ExecutorCompletionServic<wbr>e(exec);<wbr><wbr></wbr></wbr></wbr></wbr>
  17. <wbr><wbr></wbr></wbr>
  18. for<wbr>(</wbr>int<wbr>index<wbr>=<wbr></wbr></wbr></wbr>0;<wbr>index<wbr>&lt;<wbr></wbr></wbr></wbr>5;<wbr>index++)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  19. final<wbr></wbr>int<wbr>NO<wbr>=<wbr>index;<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  20. Callable<wbr>downImg<wbr>=<wbr></wbr></wbr></wbr>new<wbr>Callable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  21. public<wbr>String<wbr>call()<wbr></wbr></wbr></wbr>throws<wbr>Exception<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  22. Thread.sleep((long)<wbr>(Math.random()<wbr>*<wbr></wbr></wbr></wbr>10000));<wbr><wbr></wbr></wbr>
  23. return<wbr>“Downloaded<wbr>Image<wbr>”<wbr>+<wbr>NO;<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  24. } <wbr><wbr></wbr></wbr>
  25. }; <wbr><wbr></wbr></wbr>
  26. serv.submit(downImg);<wbr><wbr></wbr></wbr>
  27. } <wbr><wbr></wbr></wbr>
  28. <wbr><wbr></wbr></wbr>
  29. Thread.sleep(1000<wbr>*<wbr></wbr></wbr>2);<wbr><wbr></wbr></wbr>
  30. System.out.println(“Show<wbr>web<wbr>content”);<wbr><wbr></wbr></wbr></wbr></wbr>
  31. for<wbr>(</wbr>int<wbr>index<wbr>=<wbr></wbr></wbr></wbr>0;<wbr>index<wbr>&lt;<wbr></wbr></wbr></wbr>5;<wbr>index++)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  32. Future<wbr>task<wbr>=<wbr>serv.take();<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  33. String<wbr>img<wbr>=<wbr>task.get();<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  34. System.out.println(img);<wbr><wbr></wbr></wbr>
  35. } <wbr><wbr></wbr></wbr>
  36. System.out.println(“End”);<wbr><wbr></wbr></wbr>
  37. //<wbr>关闭线程池</wbr><wbr><wbr></wbr></wbr>
  38. exec.shutdown();<wbr><wbr></wbr></wbr>
  39. } <wbr><wbr></wbr></wbr>
  40. }<wbr><wbr></wbr></wbr>


运行结果:
Show web content
Downloaded Image 1
Downloaded Image 2
Downloaded Image 4
Downloaded Image 0
Downloaded Image 3
End

<wbr><wbr><wbr>操作系统的信号量是个很重要的概念,在进程控制方面都有应用。Java并发库的Semaphore可以很轻松完成信号量控制,Semaphore可以控制某个资源可被同时访问的个数,acquire()获取一个许可,如果没有就等待,而release()释放一个许可。比如在Windows下可以设置共享文件的最大客户端访问个数。<br><br><wbr><wbr><wbr>Semaphore维护了当前访问的个数,提供同步机制,控制同时访问的个数。在数据结构中链表可以保存“无限”的节点,用Semaphore可以实现有限大小的链表。另外重入锁ReentrantLock也可以实现该功能,但实现上要负责些,代码也要复杂些。<br><br><wbr><wbr><wbr>下面的Demo中申明了一个只有5个许可的Semaphore,而有20个线程要访问这个资源,通过acquire()和release()获取和释放访问许可。<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

Java代码
  1. package<wbr>concurrent;<wbr><wbr></wbr></wbr></wbr>
  2. <wbr><wbr></wbr></wbr>
  3. import<wbr>java.util.concurrent.ExecutorService;<wbr><wbr></wbr></wbr></wbr>
  4. import<wbr>java.util.concurrent.Executors;<wbr><wbr></wbr></wbr></wbr>
  5. import<wbr>java.util.concurrent.Semaphore;<wbr><wbr></wbr></wbr></wbr>
  6. <wbr><wbr></wbr></wbr>
  7. public<wbr></wbr>class<wbr>TestSemaphore<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  8. public<wbr></wbr>static<wbr></wbr>void<wbr>main(String[]<wbr>args)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  9. //<wbr>线程池</wbr><wbr><wbr></wbr></wbr>
  10. ExecutorService<wbr>exec<wbr>=<wbr>Executors.newCachedThreadPool();<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  11. //<wbr>只能5个线程同时访问</wbr><wbr><wbr></wbr></wbr>
  12. final<wbr>Semaphore<wbr>semp<wbr>=<wbr></wbr></wbr></wbr></wbr>new<wbr>Semaphore(</wbr>5);<wbr><wbr></wbr></wbr>
  13. //<wbr>模拟20个客户端访问</wbr><wbr><wbr></wbr></wbr>
  14. for<wbr>(</wbr>int<wbr>index<wbr>=<wbr></wbr></wbr></wbr>0;<wbr>index<wbr>&lt;<wbr></wbr></wbr></wbr>20;<wbr>index++)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  15. final<wbr></wbr>int<wbr>NO<wbr>=<wbr>index;<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  16. Runnable<wbr>run<wbr>=<wbr></wbr></wbr></wbr>new<wbr>Runnable()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  17. public<wbr></wbr>void<wbr>run()<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr>
  18. try<wbr>{<wbr><wbr></wbr></wbr></wbr>
  19. //<wbr>获取许可</wbr><wbr><wbr></wbr></wbr>
  20. semp.acquire();<wbr><wbr></wbr></wbr>
  21. System.out.println(“Accessing:<wbr>”<wbr>+<wbr>NO);<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  22. Thread.sleep((long)<wbr>(Math.random()<wbr>*<wbr></wbr></wbr></wbr>10000));<wbr><wbr></wbr></wbr>
  23. //<wbr>访问完后,释放</wbr><wbr><wbr></wbr></wbr>
  24. semp.release();<wbr><wbr></wbr></wbr>
  25. }<wbr></wbr>catch<wbr>(InterruptedException<wbr>e)<wbr>{<wbr><wbr></wbr></wbr></wbr></wbr></wbr>
  26. } <wbr><wbr></wbr></wbr>
  27. } <wbr><wbr></wbr></wbr>
  28. }; <wbr><wbr></wbr></wbr>
  29. exec.execute(run);<wbr><wbr></wbr></wbr>
  30. } <wbr><wbr></wbr></wbr>
  31. //<wbr>退出线程池</wbr><wbr><wbr></wbr></wbr>
  32. exec.shutdown();<wbr><wbr></wbr></wbr>
  33. } <wbr><wbr></wbr></wbr>
  34. }<wbr><wbr></wbr></wbr>


运行结果:
Accessing: 0
Accessing: 1
Accessing: 2
Accessing: 3
Accessing: 4
Accessing: 5
Accessing: 6
Accessing: 7
Accessing: 8
Accessing: 9
Accessing: 10
Accessing: 11
Accessing: 12
Accessing: 13
Accessing: 14
Accessing: 15
Accessing: 16
Accessing: 17
Accessing: 18
Accessing: 19



你可能感兴趣的:(Concurrent)