1.題目
Java實現兩個線程依次交替運行
2.代碼
1 import java.util.concurrent.TimeUnit; 2 import java.util.concurrent.locks.Condition; 3 import java.util.concurrent.locks.ReentrantLock; 4 5 /** 6 * @title 兩個線程依次交替執行 7 * @description 假定兩個線程分別為紅燈、綠燈,執行的期待結果為:紅-綠-紅-綠-紅-綠... 8 * @author ocaicai@yeah.net 9 * @since 2013-8-17 下午12:51:11 10 */ 11 public class AlternateRun2 implements Runnable { 12 13 private int tnum = 1;// 線程編號,Thread Number 14 15 private ReentrantLock lock = new ReentrantLock(); 16 17 private Condition redCon = lock.newCondition(); 18 private Condition greenCon = lock.newCondition(); 19 20 public static void main(String[] args) { 21 new AlternateRun2().run(); 22 } 23 24 @Override 25 public void run() { 26 new Thread(new RedThread(), "red light").start(); 27 new Thread(new GreenThread(), "green light").start(); 28 } 29 30 class RedThread implements Runnable { 31 32 @Override 33 public void run() { 34 while (true) { 35 try { 36 lock.lock(); 37 while (tnum != 1) {// 判斷是否該自己執行了[采用while不是if是為了防止死鎖] 38 redCon.await(); 39 } 40 System.out.println(Thread.currentThread().getName()+ " is flashing..."); 41 42 TimeUnit.SECONDS.sleep(1);// 停留時間,便於從控制台觀看 43 44 tnum = 2; 45 greenCon.signal(); 46 47 } catch (InterruptedException e) { 48 e.printStackTrace(); 49 } finally { 50 lock.unlock(); 51 } 52 } 53 } 54 } 55 56 class GreenThread implements Runnable { 57 58 @Override 59 public void run() { 60 61 while (true) { 62 try { 63 lock.lock(); 64 while (tnum != 2) { 65 greenCon.await(); 66 } 67 System.out.println(Thread.currentThread().getName()+ " is flashing..."); 68 69 TimeUnit.SECONDS.sleep(1);// 停留時間,便於從控制台觀看 70 71 tnum = 1; 72 redCon.signal(); 73 74 } catch (InterruptedException e) { 75 e.printStackTrace(); 76 } finally { 77 lock.unlock(); 78 } 79 } 80 } 81 82 } 83 84 }
3.執行結果
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
red light is flashing...
green light is flashing...
4.參考
1 /** 2 * ref http://coolxing.iteye.com/blog/1236696<BR> 3 * desc: java並發編程--一道經典多線程題的<BR> 4 * 啟動3個線程打印遞增的數字, 線程1先打印1,2,3,4,5, <BR> 5 * 然后是線程2打印6,7,8,9,10, <BR> 6 * 然后是線程3打印11,12,13,14,15. <BR> 7 * 接着再由線程1打印16,17,18,19,20....以此類推, 直到打印到75. 程序的輸出結果應該為: 8 * 9 * 線程1: 1<BR> 10 * 線程1: 2<BR> 11 * 線程1: 3<BR> 12 * 線程1: 4<BR> 13 * 線程1: 5<BR> 14 * <BR> 15 * 線程2: 6<BR> 16 * 線程2: 7<BR> 17 * 線程2: 8<BR> 18 * 線程2: 9<BR> 19 * 線程2: 10<BR> 20 * ... <BR> 21 * 線程3: 71<BR> 22 * 線程3: 72<BR> 23 * 線程3: 73<BR> 24 * 線程3: 74<BR> 25 * 線程3: 75<BR> 26 * 27 */
5.PS
如果你查看JDK1.5+的java.util.concurrent.*就會發現每個類都被打上了這樣的注釋
/** * TODO * * @since 1.5 * @author Doug Lea */ public interface Condition { //TODO }
沒錯,這位大神就是Doug Lea 道格.李,由於其開源的並發包太過優秀所以就被納進了JDK1.5,等等,作為一個大神怎能不讓他做出更多的貢獻呢?是的,java.util.*的集合框架也部分來自於Doug的貢獻。參考:Doug Lea並發編程文章全部譯文 , Doug Lea Discusses the Fork/Join Framework