并发基础知识2

4终结任务  
1、装饰性花园 


2、在阻塞时终结 
四种状态: 
新建(new):当线程被创建时,他只会短暂的出于这种状态。 
就绪(Runnable):在这种状态下,只要调度器把时间片分配给线程,线程就可以运行。 
阻塞(Blocked):线程能够运行,但有某个条件阻止他的运行。 
死亡(Dead):处于死亡活终止状态的线程将不再是可调度的,并且再也不会得到CPU时间,他的任务已经结束,或不再是可运行的。 


进入阻塞状态: 
一个任务进入阻塞状态可能有如下原因: 
通过调用sleep(milliseconds)使任务进入休眠状态,在这种情况下,任务在指定的时间内不会运行。 
你通过调用wait()使线程挂起。知道线程得到notify()或notifyAll()消息(或者在Java SE5中java.util.concurrent类库中等价的signal()或signalAll()消息),线程才会进入就绪状态。 
任务在等待某个输入/输出完成。 
任务试图在某个对象上调用其同步控制方法,但是对象锁不可用,因为另一个任务已经获得了这个锁。 



3、中断 
Thread.interrupt()或者新类库里面通过Executor的submit()来获得Future<?>返回值,这个Future提供cancel()以停止这个线程。 

cancel: 
boolean cancel(boolean mayInterruptIfRunning)试图取消对此任务的执行。如果任务已完成、或已取消,或者由于某些其他原因而无法取消,则此尝试将失败。当调用 cancel 时,如果调用成功,而此任务尚未启动,则此任务将永不运行。如果任务已经启动,则 mayInterruptIfRunning 参数确定是否应该以试图停止任务的方式来中断执行此任务的线程。 

参数: 
mayInterruptIfRunning - 如果应该中断执行此任务的线程,则为 true;否则允许正在运行的任务运行完成。 
返回: 
如果无法取消任务,则返回 false,这通常是由于它已经正常完成;否则返回 true。 


被互斥所阻塞 
如果你尝试着在一个对象上调用其synchronized方法,而这个对象的锁已经被其他任务获得,那么调用任务讲被挂起(阻塞),直至这个锁可获得。 




4、检查中断 
注意,当你在线程上调用interrupt()时,中断发生的唯一时刻是在任务要进入到阻塞操作中,或者已经在阻塞操作内部时。 
如果你调用interrupt()以停止某个任务,那么在run()循环碰巧没有产生任何阻塞调用的情况下,你的任务将需要第二种方式退出。 
还可以通过interrupted()来检测中断状态。 









5线程之间的协作  
1、wait()与notifyAll() 
wait()使你可以等待某个条件发生变化,而改变这个条件超出了当前方法的控制能力。只有在notify()或notifyAll()发生时,这个任务才会被唤醒并且去检查所发生的变化。因此,wait()提供了一种在任务之间对活动同步的方式。 
调用sleep()的时候锁并没有释放,调用yield()也属于这种情况。wait()将释放锁。 
wait还可以指定时间参数。 

Demo:
  1. package com.partner4java.tij.cooperate;  
  2.   
  3. public class Car {  
  4.     private boolean waxOn = false;  
  5.     public synchronized void waxed(){  
  6.         waxOn = true//Ready to buff  
  7.         notifyAll();  
  8.     }  
  9.     public synchronized void buffed(){  
  10.         waxOn = false//Ready for another coat of wax  
  11.         notifyAll();  
  12.     }  
  13.     public synchronized void waitForWaxing() throws InterruptedException{  
  14.         while(waxOn == false)  
  15.             wait();  
  16.     }  
  17.     public synchronized void waitForBuffing() throws InterruptedException{  
  18.         while(waxOn == true)  
  19.             wait();  
  20.     }  
  21. }  
  22.   
  23.   
  24. package com.partner4java.tij.cooperate;  
  25.   
  26. import java.util.concurrent.TimeUnit;  
  27.   
  28. public class WaxOff implements Runnable {  
  29.     private Car car;  
  30.     public WaxOff(Car c) {  
  31.         car = c;  
  32.     }  
  33.     @Override  
  34.     public void run() {  
  35.         try {  
  36.             while(!Thread.interrupted()){  
  37.                 car.waitForWaxing();  
  38.                 System.out.println("Wax Off!");  
  39.                 TimeUnit.MILLISECONDS.sleep(200);  
  40.                 car.buffed();  
  41.             }  
  42.         } catch (InterruptedException e) {  
  43.             System.out.println("Exiting via interrupt");  
  44.         }  
  45.         System.out.println("Ending Wax Off task");  
  46.     }  
  47.   
  48. }  
  49.   
  50.   
  51.   
  52. package com.partner4java.tij.cooperate;  
  53.   
  54. import java.util.concurrent.TimeUnit;  
  55.   
  56. public class WaxOn implements Runnable {  
  57.     private Car car;  
  58.     public WaxOn(Car c) {  
  59.         car = c;  
  60.     }  
  61.     @Override  
  62.     public void run() {  
  63.         try {  
  64.             while(!Thread.interrupted()){  
  65.                 System.out.println("Wax On!");  
  66.                 TimeUnit.MILLISECONDS.sleep(200);  
  67.                 car.waxed();  
  68.                 car.waitForBuffing();  
  69.             }  
  70.         } catch (InterruptedException e) {  
  71.             System.out.println("Exiting via interrupt");  
  72.         }  
  73.         System.out.println("Ending Wax on Task");  
  74.     }  
  75.   
  76. }  
  77.   
  78.   
  79.   
  80. package com.partner4java.tij.cooperate;  
  81.   
  82. import java.util.concurrent.ExecutorService;  
  83. import java.util.concurrent.Executors;  
  84. import java.util.concurrent.ThreadFactory;  
  85. import java.util.concurrent.TimeUnit;  
  86.   
  87. public class WaxOMatic {  
  88.   
  89.     public static void main(String[] args) throws InterruptedException {  
  90.         ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory() {  
  91.               
  92.             @Override  
  93.             public Thread newThread(Runnable r) {  
  94.                 Thread thread = new Thread(r);  
  95.                 thread.setDaemon(true);  
  96.                 return thread;  
  97.             }  
  98.         });  
  99.         Car car = new Car();  
  100.         executorService.execute(new WaxOff(car ));  
  101.         executorService.execute(new WaxOn(car));  
  102.         TimeUnit.SECONDS.sleep(5);  
  103.         executorService.shutdown();  
  104.     }  
  105.   
  106. }  


错失信号: 
解决方案是防止在变量上产生竞争条件。 
  1. T1:  
  2. synchronized(sharedMonitor){  
  3.     <setup condition for T2>  
  4.     sharedMonitor.notify();  
  5. }  
  6. T2:  
  7. synchronized(sharedMonitor){  
  8.     while(someCondition)  
  9.         sharedMonitor.wait();  
  10. }  





2、notify()与notifyAll() 
使用notify不是notifyAll的一种优化。 
当notifyAll因某个特定的锁而被调用时,只要等待这个锁的任务才会被唤醒。 

Demo:
  1. package com.partner4java.tij.cooperate;  
  2.   
  3. public class Blocker {  
  4.     synchronized void waitCall(){  
  5.         try {  
  6.             while(!Thread.interrupted()){  
  7.                 wait();  
  8.                 System.out.println(Thread.currentThread() + " ");  
  9.             }  
  10.         } catch (Exception e) {  
  11.         }  
  12.     }  
  13.       
  14.     synchronized void prod(){  
  15.         notify();  
  16.     }  
  17.       
  18.     synchronized void prodAll(){  
  19.         notifyAll();  
  20.     }  
  21. }  
  22.   
  23.   
  24. package com.partner4java.tij.cooperate;  
  25.   
  26. public class Task implements Runnable {  
  27.     static Blocker blocker = new Blocker();  
  28.     @Override  
  29.     public void run() {  
  30.         blocker.waitCall();  
  31.     }  
  32.   
  33. }  
  34.   
  35.   
  36. package com.partner4java.tij.cooperate;  
  37.   
  38. public class Task2 implements Runnable {  
  39.     static Blocker blocker = new Blocker();  
  40.     @Override  
  41.     public void run() {  
  42.         blocker.waitCall();  
  43.     }  
  44.   
  45. }  
  46.   
  47.   
  48. package com.partner4java.tij.cooperate;  
  49.   
  50. import java.util.concurrent.ExecutorService;  
  51. import java.util.concurrent.Executors;  
  52. import java.util.concurrent.TimeUnit;  
  53.   
  54. public class NotifyVsNotifyAll {  
  55.   
  56.     public static void main(String[] args) throws InterruptedException {  
  57.         ExecutorService executorService = Executors.newCachedThreadPool();  
  58.         for(int i=0;i<5;i++){  
  59.             executorService.execute(new Task());  
  60.         }  
  61.         executorService.execute(new Task2());  
  62.           
  63.           
  64.         executorService.execute(new Runnable() {  
  65.               
  66.             @Override  
  67.             public void run() {  
  68.                 System.out.println("Task.blocker.prod");  
  69.                 Task.blocker.prod();  
  70.                 System.out.println("Task2.blocker.prodAll");  
  71.                 Task2.blocker.prodAll();  
  72.             }  
  73.         });  
  74.           
  75.         TimeUnit.MILLISECONDS.sleep(100);  
  76.           
  77.         executorService.shutdownNow();  
  78.     }  
  79.   
  80. }  





3、生产者和消费者 
Demo:
  1. package com.partner4java.tij.cooperate;  
  2.   
  3. public class Meal {  
  4.     private final int orderNum;  
  5.     public Meal(int orderNum) {  
  6.         this.orderNum = orderNum;  
  7.     }  
  8.     @Override  
  9.     public String toString() {  
  10.         return "Meal [orderNum=" + orderNum + "]";  
  11.     }  
  12.       
  13. }  
  14.   
  15.   
  16. package com.partner4java.tij.cooperate;  
  17.   
  18. public class WaitPerson implements Runnable {  
  19.     private Restaurant restaurant;  
  20.     public WaitPerson(Restaurant restaurant) {  
  21.         this.restaurant = restaurant;  
  22.     }  
  23.     @Override  
  24.     public void run() {  
  25.         try {  
  26.             while(!Thread.interrupted()){  
  27.                 synchronized (this) {  
  28.                     while(restaurant.meal == null){  
  29.                         wait();  
  30.                     }  
  31.                 }  
  32.                 System.out.println("Waitperson got " + restaurant.meal);  
  33.                 synchronized (restaurant.chef) {  
  34.                     restaurant.meal = null;  
  35.                     restaurant.chef.notifyAll();//Ready for another  
  36.                 }  
  37.             }  
  38.         } catch (InterruptedException e) {  
  39.             System.out.println("WaitPerson interrupted");  
  40.         }  
  41.     }  
  42.   
  43. }  
  44.   
  45.   
  46. package com.partner4java.tij.cooperate;  
  47.   
  48. import java.util.concurrent.TimeUnit;  
  49.   
  50. public class Chef implements Runnable {  
  51.     private Restaurant restaurant;  
  52.     private int count = 0;  
  53.     public Chef(Restaurant restaurant) {  
  54.         this.restaurant = restaurant;  
  55.     }  
  56.     @Override  
  57.     public void run() {  
  58.         try {  
  59.             while(!Thread.interrupted()){  
  60.                 synchronized (this) {  
  61.                     while(restaurant.meal != null){  
  62.                         wait();  
  63.                     }  
  64.                 }  
  65.                 if(++count == 10){  
  66.                     System.out.println("Out of food,closing");  
  67.                     restaurant.exec.shutdownNow();  
  68.                 }  
  69.                 System.out.println("Order up!");  
  70.                 synchronized (restaurant.waitPerson) {  
  71.                     restaurant.meal = new Meal(count);  
  72.                     restaurant.waitPerson.notifyAll();  
  73.                 }  
  74.                 TimeUnit.MILLISECONDS.sleep(100);  
  75.             }  
  76.         } catch (InterruptedException e) {  
  77.             System.out.println("Chef interrupted");  
  78.         }  
  79.     }  
  80.   
  81. }  
  82.   
  83.   
  84. package com.partner4java.tij.cooperate;  
  85.   
  86. import java.util.concurrent.ExecutorService;  
  87. import java.util.concurrent.Executors;  
  88.   
  89. public class Restaurant {  
  90.     Meal meal;  
  91.     ExecutorService exec = Executors.newCachedThreadPool();  
  92.     WaitPerson waitPerson = new WaitPerson(this);  
  93.     Chef chef = new Chef(this);  
  94.     public Restaurant() {  
  95.         exec.execute(chef);  
  96.         exec.execute(waitPerson);  
  97.     }  
  98.     public static void main(String[] args) {  
  99.         new Restaurant();  
  100.     }  
  101.   
  102. }  



使用显示的Lock和Condition对象: 
Demo: 
  1. class BoundedBuffer {  
  2.   final Lock lock = new ReentrantLock();  
  3.   final Condition notFull  = lock.newCondition();   
  4.   final Condition notEmpty = lock.newCondition();   
  5.   
  6.   final Object[] items = new Object[100];  
  7.   int putptr, takeptr, count;  
  8.   
  9.   public void put(Object x) throws InterruptedException {  
  10.     lock.lock();  
  11.     try {  
  12.       while (count == items.length)   
  13.         notFull.await();  
  14.       items[putptr] = x;   
  15.       if (++putptr == items.length) putptr = 0;  
  16.       ++count;  
  17.       notEmpty.signal();  
  18.     } finally {  
  19.       lock.unlock();  
  20.     }  
  21.   }  
  22.   
  23.   public Object take() throws InterruptedException {  
  24.     lock.lock();  
  25.     try {  
  26.       while (count == 0)   
  27.         notEmpty.await();  
  28.       Object x = items[takeptr];   
  29.       if (++takeptr == items.length) takeptr = 0;  
  30.       --count;  
  31.       notFull.signal();  
  32.       return x;  
  33.     } finally {  
  34.       lock.unlock();  
  35.     }  
  36.   }   
  37. }  






4、生产者-消费者与队列 
使用同步队列来解决任务协作问题。同步队列在任务时刻都只允许一个任务插入或移除元素。 
Demo: 
  1. package com.partner4java.tij.blockingqueue;  
  2.   
  3. import java.util.concurrent.BlockingQueue;  
  4.   
  5. import com.partner4java.tij.util.LiftOff;  
  6.   
  7. public class LiftOffRunner implements Runnable {  
  8.     private BlockingQueue<LiftOff> rockets;  
  9.     public LiftOffRunner(BlockingQueue<LiftOff> rockets) {  
  10.         this.rockets = rockets;  
  11.     }  
  12.       
  13.     public void add(LiftOff liftOff){  
  14.         try {  
  15.             rockets.add(liftOff);  
  16.         } catch (Exception e) {  
  17.             System.out.println("Interrupted during put()");  
  18.         }  
  19.     }  
  20.       
  21.     @Override  
  22.     public void run() {  
  23.         try {  
  24.             while(!Thread.interrupted()){  
  25.                 LiftOff liftOff = rockets.take();  
  26.                 liftOff.run();  
  27.             }  
  28.         } catch (InterruptedException e) {  
  29.             System.out.println("Waking from take()");  
  30.         }  
  31.         System.out.println("Exiting LiftOffRunner");  
  32.     }  
  33.   
  34. }  
  35.   
  36.   
  37. package com.partner4java.tij.blockingqueue;  
  38.   
  39. import java.util.concurrent.ArrayBlockingQueue;  
  40. import java.util.concurrent.BlockingQueue;  
  41. import java.util.concurrent.LinkedBlockingDeque;  
  42. import java.util.concurrent.SynchronousQueue;  
  43. import java.util.concurrent.TimeUnit;  
  44.   
  45. import com.partner4java.tij.util.LiftOff;  
  46.   
  47. public class TestBlockingQueues {  
  48.       
  49.     public static void main(String[] args) throws InterruptedException {  
  50.         test("LinkedBlockingQueue"new LinkedBlockingDeque<LiftOff>());  
  51.         test("ArrayBlockingQueue"new ArrayBlockingQueue<LiftOff>(3));  
  52.         test("SynchronousQueue"new SynchronousQueue<LiftOff>());  
  53.     }  
  54.   
  55.     private static void test(String message,BlockingQueue<LiftOff> rockets) throws InterruptedException {  
  56.         System.out.println("test:" + message);  
  57.         LiftOffRunner liftOffRunner = new LiftOffRunner(rockets);  
  58.         Thread thread = new Thread(liftOffRunner);  
  59.         thread.start();  
  60.           
  61.         for(int i=0;i<5;i++){  
  62.             liftOffRunner.add(new LiftOff(6));  
  63.         }  
  64.           
  65.         thread.interrupt();  
  66.     }  
  67.   
  68. }  
  69.   
  70.   
  71. //支持两个附加操作的 Queue,这两个操作是:检索元素时等待队列变为非空,以及存储元素时等待空间变得可用。   
  72. //  
  73. //BlockingQueue 不接受 null 元素。试图 add、put 或 offer 一个 null 元素时,某些实现会抛出 NullPointerException。null 被用作指示 poll 操作失败的警戒值。   
  74. //  
  75. //BlockingQueue 可以是限定容量的。它在任意给定时间都可以有一个 remainingCapacity,超出此容量,便无法无阻塞地 put 额外的元素。没有任何内部容量约束的 BlockingQueue 总是报告 Integer.MAX_VALUE 的剩余容量。   
  76. //  
  77. //BlockingQueue 实现主要用于生产者-使用者队列,但它另外还支持 Collection 接口。因此,举例来说,使用 remove(x) 从队列中移除任意一个元素是有可能的。然而,这种操作通常不 会有效执行,只能有计划地偶尔使用,比如在取消排队信息时。   
  78. //  
  79. //BlockingQueue 实现是线程安全的。所有排队方法都可以使用内部锁定或其他形式的并发控制来自动达到它们的目的。然而,大量的 Collection 操作(addAll、containsAll、retainAll 和 removeAll)没有 必要自动执行,除非在实现中特别说明。因此,举例来说,在只添加了 c 中的一些元素后,addAll(c) 有可能失败(抛出一个异常)。   
  80. //  
  81. //BlockingQueue 实质上不 支持使用任何一种“close”或“shutdown”操作来指示不再添加任何项。这种功能的需求和使用有依赖于实现的倾向。例如,一种常用的策略是:对于生产者,插入特殊的 end-of-stream 或 poison 对象,并根据使用者获取这些对象的时间来对它们进行解释。   
  82. //  
  83. //以下是基于典型的生产者-使用者场景的一个用例。注意,BlockingQueue 可以安全地与多个生产者和多个使用者一起使用。   
  84. //  
  85. // class Producer implements Runnable {  
  86. //   private final BlockingQueue queue;  
  87. //   Producer(BlockingQueue q) { queue = q; }  
  88. //   public void run() {  
  89. //     try {  
  90. //       while(true) { queue.put(produce()); }  
  91. //     } catch (InterruptedException ex) { ... handle ...}  
  92. //   }  
  93. //   Object produce() { ... }  
  94. // }  
  95. //  
  96. // class Consumer implements Runnable {  
  97. //   private final BlockingQueue queue;  
  98. //   Consumer(BlockingQueue q) { queue = q; }  
  99. //   public void run() {  
  100. //     try {  
  101. //       while(true) { consume(queue.take()); }  
  102. //     } catch (InterruptedException ex) { ... handle ...}  
  103. //   }  
  104. //   void consume(Object x) { ... }  
  105. // }  
  106. //  
  107. // class Setup {  
  108. //   void main() {  
  109. //     BlockingQueue q = new SomeQueueImplementation();  
  110. //     Producer p = new Producer(q);  
  111. //     Consumer c1 = new Consumer(q);  
  112. //     Consumer c2 = new Consumer(q);  
  113. //     new Thread(p).start();  
  114. //     new Thread(c1).start();  
  115. //     new Thread(c2).start();  
  116. //   }  
  117. // }  
  118.   


当你存在多个任务之间的协调时,你还可以同时创建多个BlockingQueue,用于每个步骤的协调。 





5、任务间使用管道进行输入/输出 
Demo: 
  1. package com.partner4java.tij.piped;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PipedWriter;  
  5. import java.util.Random;  
  6. import java.util.concurrent.TimeUnit;  
  7.   
  8. public class Sender implements Runnable {  
  9.     private Random random = new Random(47);  
  10.     private PipedWriter out = new PipedWriter();  
  11.       
  12.     public PipedWriter getPipedWriter(){  
  13.         return out;  
  14.     }  
  15.       
  16.     @Override  
  17.     public void run() {  
  18.         try {  
  19.             while(true){  
  20.                 for(char c = 'A';c<='z';c++){  
  21.                     out.write(c);  
  22.                     TimeUnit.MILLISECONDS.sleep(random.nextInt(500));  
  23.                 }  
  24.             }  
  25.         } catch (IOException e) {  
  26.             e.printStackTrace();  
  27.         } catch (InterruptedException e) {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.   
  32. }  
  33.   
  34.   
  35. package com.partner4java.tij.piped;  
  36.   
  37. import java.io.IOException;  
  38. import java.io.PipedReader;  
  39.   
  40. public class Receiver implements Runnable {  
  41.     private PipedReader in;  
  42.     public Receiver(Sender sender) throws IOException {  
  43.         in = new PipedReader(sender.getPipedWriter());  
  44.     }  
  45.       
  46.     @Override  
  47.     public void run() {  
  48.         try {  
  49.             while(true){  
  50.                 System.out.println("Read:" + (char)in.read()+",");  
  51.             }  
  52.         } catch (IOException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56. }  
  57.   
  58.   
  59. package com.partner4java.tij.piped;  
  60.   
  61. import java.io.IOException;  
  62. import java.util.concurrent.ExecutorService;  
  63. import java.util.concurrent.Executors;  
  64. import java.util.concurrent.TimeUnit;  
  65.   
  66. public class PipedIO {  
  67.   
  68.     public static void main(String[] args) throws IOException, InterruptedException {  
  69.         Sender sender = new Sender();  
  70.         Receiver receiver = new Receiver(sender);  
  71.         ExecutorService executorService = Executors.newCachedThreadPool();  
  72.         executorService.execute(sender);  
  73.         executorService.execute(receiver);  
  74.         TimeUnit.SECONDS.sleep(10);  
  75.         executorService.shutdownNow();  
  76.     }  
  77.   
  78. }  







6死锁  
某个任务在等待另一个任务,而后者又等待别的任务,这样一直下去,直到这个链条上的任务又在等待第一个任务释放锁,这样就造成了一个互相等待的循环。变成了死锁。

以下四个条件同时满足时,就会发生死锁: 
1、互斥条件。 
2、至少有一个任务他必须持有一个资源且正在等待获取一个当前被别的任务持有的资源。 
3、资源不能被任务抢占,任务必须把资源释放当做普通事件。 
4、必须有循环等待,这样,一个任务等待其他任务所持有的资源。 







7新类库中的构件  
1、CountDownLatch 
他被也拿过来同步一个或多个任务,强制他们等待由其他任务执行的一组操作完成。 
你可以向CountDownLatch对象设置一个初始计数值,任务在这个对象上调用wait()的方法都将阻塞,直至这个计数值到达0。 
其他任务在结束起工作时,可以在改对象上调用countDown()来减小这个计数值。 
CountDownLatch被设计为止触发一次,计数值不能被重置。如果你需要能够重置计数值的版本,则可以使用CyclicBarrier。 
Demo: 
package com.partner4java.tij.countdownlatch; 

import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.TimeUnit; 

public class TaskPortion implements Runnable { 
private static int count = 0; 
private static final int id = count++; 
private CountDownLatch countDownLatch; 
public TaskPortion(CountDownLatch countDownLatch) { 
this.countDownLatch = countDownLatch; 

@Override 
public void run() { 
try { 
dowork(); 
countDownLatch.countDown(); 
} catch (InterruptedException e) { 
e.printStackTrace(); 



private void dowork() throws InterruptedException { 
TimeUnit.MILLISECONDS.sleep(100); 
System.out.println(this + " is completed"); 


@Override 
public String toString() { 
return "TaskPortion id:" + id; 







package com.partner4java.tij.countdownlatch; 

import java.util.concurrent.CountDownLatch; 

public class WaitingTask implements Runnable { 
private static int count = 0; 
private static final int id = count++; 
private CountDownLatch countDownLatch; 

public WaitingTask(CountDownLatch countDownLatch) { 
this.countDownLatch = countDownLatch; 


@Override 
public void run() { 
try { 
countDownLatch.await(); 
System.out.println("CountDownLatch barrier passed for " + this); 
} catch (InterruptedException e) { 
System.out.println(this + " interrupted"); 



@Override 
public String toString() { 
return "WaitingTask " + id; 




package com.partner4java.tij.countdownlatch; 

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

public class CountDownLatchDemo { 
private static final int SIZE = 100; 

public static void main(String[] args) { 
ExecutorService executorService = Executors.newCachedThreadPool(); 
CountDownLatch countDownLatch = new CountDownLatch(SIZE); 
for(int i=0;i<10;i++){ 
executorService.execute(new WaitingTask(countDownLatch)); 

for(int i=0;i<SIZE;i++){ 
executorService.execute(new TaskPortion(countDownLatch)); 

System.out.println("Launched all tasks"); 
executorService.shutdown(); 







类库的线程安全: 







2、CyclicBarrier 
CountDownLatch是只触发一次的事件,而CyclicBarrier可以多次重用。 
一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 CyclicBarrier 很有用。因为该 barrier 在释放等待线程后可以重用,所以称它为循环 的 barrier。 
CyclicBarrier 支持一个可选的 Runnable 命令,在一组线程中的最后一个线程到达之后(但在释放所有线程之前),该命令只在每个屏障点运行一次。若在继续所有参与线程之前更新共享状态,此屏障操作 很有用。 

Demo:
  1. package com.partner4java.tij.se5.cyclicbarrier;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.BrokenBarrierException;  
  5. import java.util.concurrent.CyclicBarrier;  
  6.   
  7. public class Horse implements Runnable {  
  8.     private static int counter = 0;  
  9.     private static final int id = counter++;  
  10.     private int strides = 0;  
  11.     private static Random random = new Random(47);  
  12.     private static CyclicBarrier barrier;  
  13.     public Horse(CyclicBarrier barrier) {  
  14.         this.barrier = barrier;  
  15.     }  
  16.     public synchronized int getStrides() {  
  17.         return strides;  
  18.     }  
  19.       
  20.     @Override  
  21.     public void run() {  
  22.         try {  
  23.             while(!Thread.interrupted()){  
  24.                 synchronized (this) {  
  25.                     strides += random.nextInt(3);  
  26.                 }  
  27.                 barrier.await();  
  28.             }  
  29.         } catch (InterruptedException e) {  
  30.             e.printStackTrace();  
  31.         } catch (BrokenBarrierException e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35.   
  36.     @Override  
  37.     public String toString() {  
  38.         return "Horse: " + id;  
  39.     }  
  40.     public String tracks() {  
  41.         StringBuilder builder = new StringBuilder();  
  42.         for(int i=0;i<getStrides();i++){  
  43.             builder.append("*");  
  44.         }  
  45.         builder.append(id);  
  46.         return builder.toString();  
  47.     }  
  48. }  
  49.   
  50.   
  51. package com.partner4java.tij.se5.cyclicbarrier;  
  52.   
  53. import java.util.ArrayList;  
  54. import java.util.List;  
  55. import java.util.concurrent.CyclicBarrier;  
  56. import java.util.concurrent.ExecutorService;  
  57. import java.util.concurrent.Executors;  
  58. import java.util.concurrent.TimeUnit;  
  59.   
  60. public class HorseRace {  
  61.     static final int FINISH_LINE = 75;  
  62.     private List<Horse> horses = new ArrayList<Horse>();  
  63.     private ExecutorService executorService = Executors.newCachedThreadPool();  
  64.     private CyclicBarrier barrier;  
  65.     public HorseRace(int nHorses, final int pause) {  
  66.         barrier = new CyclicBarrier(nHorses, new Runnable() {  
  67.               
  68.             @Override  
  69.             public void run() {  
  70.                 StringBuilder builder = new StringBuilder();  
  71.                 for(int i=0;i<FINISH_LINE;i++){  
  72.                     builder.append("=");  
  73.                 }  
  74.                 System.out.println(builder);  
  75.                 for(Horse horse:horses){  
  76.                     System.out.println(horse.tracks());  
  77.                 }  
  78.                 for(Horse horse:horses){  
  79.                     if(horse.getStrides() >= FINISH_LINE){  
  80.                         System.out.println(horse + " won!");  
  81.                         executorService.shutdownNow();  
  82.                         return;  
  83.                     }  
  84.                 }  
  85.                 try {  
  86.                     TimeUnit.MILLISECONDS.sleep(pause);  
  87.                 } catch (InterruptedException e) {  
  88.                     e.printStackTrace();  
  89.                 }  
  90.             }  
  91.         });  
  92.         for(int i=0;i<nHorses;i++){  
  93.             Horse horse = new Horse(barrier);  
  94.             horses.add(horse);  
  95.             executorService.execute(horse);  
  96.         }  
  97.     }  
  98.       
  99.     public static void main(String[] args) {  
  100.         int nHorses = 7;  
  101.         int pause = 200;  
  102.         new HorseRace(nHorses, pause);  
  103.     }  
  104.   
  105. }  
  106.   
  107. //一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待,此时 CyclicBarrier 很有用。因为该 barrier 在释放等待线程后可以重用,所以称它为循环 的 barrier。   
  108. //  
  109. //CyclicBarrier 支持一个可选的 Runnable 命令,在一组线程中的最后一个线程到达之后(但在释放所有线程之前),该命令只在每个屏障点运行一次。若在继续所有参与线程之前更新共享状态,此屏障操作 很有用。   
  110. //  
  111. //示例用法:下面是一个在并行分解设计中使用 barrier 的例子:   
  112. //  
  113. // class Solver {  
  114. //   final int N;  
  115. //   final float[][] data;  
  116. //   final CyclicBarrier barrier;  
  117. //     
  118. //   class Worker implements Runnable {  
  119. //     int myRow;  
  120. //     Worker(int row) { myRow = row; }  
  121. //     public void run() {  
  122. //       while (!done()) {  
  123. //         processRow(myRow);  
  124. //  
  125. //         try {  
  126. //           barrier.await();   
  127. //         } catch (InterruptedException ex) {   
  128. //return;   
  129. //         } catch (BrokenBarrierException ex) {   
  130. //return;   
  131. //         }  
  132. //       }  
  133. //     }  
  134. //   }  
  135. //  
  136. //   public Solver(float[][] matrix) {  
  137. //     data = matrix;  
  138. //     N = matrix.length;  
  139. //     barrier = new CyclicBarrier(N,   
  140. //                                 new Runnable() {  
  141. //                                   public void run() {   
  142. //                                     mergeRows(...);   
  143. //                                   }  
  144. //                                 });  
  145. //     for (int i = 0; i < N; ++i)   
  146. //       new Thread(new Worker(i)).start();  
  147. //  
  148. //     waitUntilDone();  
  149. //   }  
  150. // }  
  151. // 在这个例子中,每个 worker 线程处理矩阵的一行,在处理完所有的行之前,该线程将一直在屏障处等待。处理完所有的行之后,将执行所提供的 Runnable 屏障操作,并合并这些行。如果合并者确定已经找到了一个解决方案,那么 done() 将返回 true,所有的 worker 线程都将终止。   
  152. //如果屏障操作在执行时不依赖于正挂起的线程,则线程组中的任何线程在获得释放时都能执行该操作。为方便此操作,每次调用 await() 都将返回能到达屏障处的线程的索引。然后,您可以选择哪个线程应该执行屏障操作,例如:   
  153. //  
  154. //  if (barrier.await() == 0) {  
  155. //     // log the completion of this iteration  
  156. //   }对于失败的同步尝试,CyclicBarrier 使用了一种快速失败的、要么全部要么全不 (all-or-none) 的破坏模式:如果因为中断、失败或者超时等原因,导致线程过早地离开了屏障点,那么其他所有线程(甚至是那些尚未从以前的 await() 中恢复的线程)也将通过 BrokenBarrierException(如果它们几乎同时被中断,则用 InterruptedException)以反常的方式离开。   











3、DelayQueue 
这是一个无界的BlockingQueue,有您关于防止实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。这种队列是有序的,即对头对象的延迟到期时间最长。如果没有任何延迟到期,那么就不会有任何头元素,并且poll()将返回null。 


public class DelayQueue<E extends Delayed>extends AbstractQueue<E>implements BlockingQueue<E>Delayed 元素的一个无界阻塞队列,只有在延迟期满时才能从中提取元素。该队列的头部 是延迟期满后保存时间最长的 Delayed 元素。如果延迟都还没有期满,则队列没有头部,并且 poll 将返回 null。当一个元素的 getDelay(TimeUnit.NANOSECONDS) 方法返回一个小于或等于零的值时,则出现期满。此队列不允许使用 null 元素。 

此类及其迭代器实现了 Collection 和 Iterator 接口的所有可选 方法。 

此类是 Java Collections Framework 的成员。 



4、PriorityBlockingQueue 
这是一个很基础的优先级队列,他具有可阻塞的读取操作。 

public class PriorityBlockingQueue<E>extends AbstractQueue<E>implements BlockingQueue<E>, Serializable一个无界的阻塞队列,它使用与类 PriorityQueue 相同的顺序规则,并且提供了阻塞检索的操作。虽然此队列逻辑上是无界的,但是由于资源被耗尽,所以试图执行添加操作可能会失败(导致 OutOfMemoryError)。此类不允许使用 null 元素。依赖自然顺序的优先级队列也不允许插入不可比较的对象(因为这样做会抛出 ClassCastException)。 

此类及其迭代器可以实现 Collection 和 Iterator 接口的所有可选 方法。iterator() 方法中所提供的迭代器并不 保证以特定的顺序遍历 PriorityBlockingQueue 的元素。如果需要有序地遍历,则应考虑使用 Arrays.sort(pq.toArray())。 

此类是 Java Collections Framework 的成员。 






5、使用ScheduledExecutor的温室控制器 
public interface ScheduledExecutorServiceextends ExecutorService一个 ExecutorService,可安排在给定的延迟后运行或定期执行的命令。 

schedule 方法使用各种延迟创建任务,并返回一个可用于取消或检查执行的任务对象。scheduleAtFixedRate 和 scheduleWithFixedDelay 方法创建并执行某些在取消前一直定期运行的任务。 

用 Executor.execute(java.lang.Runnable) 和 ExecutorService 的 submit 方法所提交的命令,通过所请求的 0 延迟进行安排。schedule 方法中允许出现 0 和负数延迟(但不是周期),并将这些视为一种立即执行的请求。 

所有的 schedule 方法都接受相对 延迟和周期作为参数,而不是绝对的时间或日期。将以 Date 所表示的绝对时间转换成要求的形式很容易。例如,要安排在某个以后的日期运行,可以使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。但是要注意,由于网络时间同步协议、时钟漂移或其他因素的存在,因此相对延迟的期满日期不必与启用任务的当前 Date 相符。 Executors 类为此包中所提供的 ScheduledExecutorService 实现提供了便捷的工厂方法。 

用法示例 
以下是一个带方法的类,它设置了 ScheduledExecutorService ,在 1 小时内每 10 秒钟蜂鸣一次: 
  1. import static java.util.concurrent.TimeUnit.*;  
  2. class BeeperControl {  
  3.    private final ScheduledExecutorService scheduler =   
  4.       Executors.newScheduledThreadPool(1);  
  5.   
  6.    public void beepForAnHour() {  
  7.        final Runnable beeper = new Runnable() {  
  8.                public void run() { System.out.println("beep"); }  
  9.            };  
  10.        final ScheduledFuture<?> beeperHandle =   
  11.            scheduler.scheduleAtFixedRate(beeper, 1010, SECONDS);  
  12.        scheduler.schedule(new Runnable() {  
  13.                public void run() { beeperHandle.cancel(true); }  
  14.            }, 60 * 60, SECONDS);  
  15.    }  
  16. }  






6、Semaphore 
正常的锁(来自concurrent.locks或内建的synchronized锁)在任何时刻都只允许一个任务访问一项资源,而计数信号量允许N个任务同时访问这个资源。你还可以将信号量看做是向外分发使用资源的的“许可证”,尽管实际上没有任何许可证对象。 
public class Semaphoreextends Objectimplements Serializable一个计数信号量。从概念上讲,信号量维护了一个许可集合。如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可。每个 release() 添加一个许可,从而可能释放一个正在阻塞的获取者。但是,不使用实际的许可对象,Semaphore 只对可用许可的号码进行计数,并采取相应的行动。 

Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目。例如,下面的类使用信号量控制对内容池的访问: 

  1. class Pool {  
  2.    private static final MAX_AVAILABLE = 100;  
  3.    private final Semaphore available = new Semaphore(MAX_AVAILABLE, true);  
  4.   
  5.    public Object getItem() throws InterruptedException {  
  6.      available.acquire();  
  7.      return getNextAvailableItem();  
  8.    }  
  9.   
  10.    public void putItem(Object x) {  
  11.      if (markAsUnused(x))  
  12.        available.release();  
  13.    }  
  14.   
  15.    // Not a particularly efficient data structure; just for demo  
  16.   
  17.    protected Object[] items = ... whatever kinds of items being managed  
  18.    protected boolean[] used = new boolean[MAX_AVAILABLE];  
  19.   
  20.    protected synchronized Object getNextAvailableItem() {  
  21.      for (int i = 0; i < MAX_AVAILABLE; ++i) {  
  22.        if (!used[i]) {  
  23.           used[i] = true;  
  24.           return items[i];  
  25.        }  
  26.      }  
  27.      return null// not reached  
  28.    }  
  29.   
  30.    protected synchronized boolean markAsUnused(Object item) {  
  31.      for (int i = 0; i < MAX_AVAILABLE; ++i) {  
  32.        if (item == items[i]) {  
  33.           if (used[i]) {  
  34.             used[i] = false;  
  35.             return true;  
  36.           } else  
  37.             return false;  
  38.        }  
  39.      }  
  40.      return false;  
  41.    }  
  42.   
  43. }  

获得一项前,每个线程必须从信号量获取许可,从而保证可以使用该项。该线程结束后,将项返回到池中并将许可返回到该信号量,从而允许其他线程获取该项。注意,调用 acquire() 时无法保持同步锁定,因为这会阻止将项返回到池中。信号量封装所需的同步,以限制对池的访问,这同维持该池本身一致性所需的同步是分开的。 

将信号量初始化为 1,使得它在使用时最多只有一个可用的许可,从而可用作一个相互排斥的锁。这通常也称为二进制信号量,因为它只能有两种状态:一个可用的许可,或零个可用的许可。按此方式使用时,二进制信号量具有某种属性(与很多 Lock 实现不同),即可以由线程释放“锁定”,而不是由所有者(因为信号量没有所有权的概念)。在某些专门的上下文(如死锁恢复)中这会很有用。 

此类的构造方法可选地接受一个公平 参数。当设置为 false 时,此类不对线程获取许可的顺序做任何保证。特别地,闯入 是允许的,也就是说可以在已经等待的线程前为调用 acquire() 的线程分配一个许可,从逻辑上说,就是新线程将自己置于等待线程队列的头部。当公平设置为 true 时,信号量保证对于任何调用 acquire 方法的线程而言,都按照处理它们调用这些方法的顺序(即先进先出;FIFO)来选择线程、获得许可。注意,FIFO 排序必然应用到这些方法内的指定内部执行点。所以,可能某个线程先于另一个线程调用了 acquire,但是却在该线程之后到达排序点,并且从方法返回时也类似。还要注意,非同步的 tryAcquire 方法不使用公平设置,而是使用任意可用的许可。 

通常,应该将用于控制资源访问的信号量初始化为公平的,以确保所有线程都可访问资源。为其他的种类的同步控制使用信号量时,非公平排序的吞吐量优势通常要比公平考虑更为重要。 

此类还提供便捷的方法来同时 acquire 和 release 多个许可。小心,在未将公平设置为 true 时使用这些方法会增加不确定延期的风险。 





7、Exchanger 
Exchanger是两个对象之间交换对象的栅栏。当这些任务进入栅栏时,他们各自拥有一个对象,当他们离开时,他们都拥有之前由对象持有的对象。 
public class Exchanger<V>extends Object两个线程可以交换对象的同步点。每个线程都在进入 exchange 方法时给出某个对象,并接受其他线程返回时给出的对象。 

用法示例:以下是重点介绍的一个类,该类使用 Exchanger 在线程间交换缓冲区,因此,在需要时,填充缓冲区的线程获得一个新腾空的缓冲区,并将填满的缓冲区传递给腾空缓冲区的线程。 
  1. class FillAndEmpty {  
  2.   Exchanger<DataBuffer> exchanger = new Exchanger();  
  3.   DataBuffer initialEmptyBuffer = ... a made-up type  
  4.   DataBuffer initialFullBuffer = ...  
  5.   
  6.   class FillingLoop implements Runnable {  
  7.     public void run() {  
  8.       DataBuffer currentBuffer = initialEmptyBuffer;  
  9.       try {  
  10.         while (currentBuffer != null) {  
  11.           addToBuffer(currentBuffer);  
  12.           if (currentBuffer.full())  
  13.             currentBuffer = exchanger.exchange(currentBuffer);  
  14.         }  
  15.       } catch (InterruptedException ex) { ... handle ... }  
  16.     }  
  17.   }  
  18.   
  19.   class EmptyingLoop implements Runnable {  
  20.     public void run() {  
  21.       DataBuffer currentBuffer = initialFullBuffer;  
  22.       try {  
  23.         while (currentBuffer != null) {  
  24.           takeFromBuffer(currentBuffer);  
  25.           if (currentBuffer.empty())  
  26.             currentBuffer = exchanger.exchange(currentBuffer);  
  27.         }  
  28.       } catch (InterruptedException ex) { ... handle ...}  
  29.     }  
  30.   }  
  31.   
  32.   void start() {  
  33.     new Thread(new FillingLoop()).start();  
  34.     new Thread(new EmptyingLoop()).start();  
  35.   }  
  36. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值