Java線程同步 2

CyclicBarrier用於設置一個Barrier,達到Barrier的線程將等待,當所有線程都達到Barrier時,所有線程繼續執行。

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

1.       創建CyclicBarrier時通過構造函數指定線程的數量n

2.       每個線程調用await方法等待其他線程。

3.       當有nawait方法被調用時,所有處於await的線程都將繼續執行。

  1. import java.util.concurrent.BrokenBarrierException;
  2. import java.util.concurrent.CyclicBarrier;
  3. public class CyclicBarrierTest
  4. {
  5.     private static CyclicBarrier barrier = null;
  6.     
  7.     public static void main(String[] args)
  8.     {
  9.         // 初始化
  10.         barrier = new CyclicBarrier(10);
  11.         
  12.         // 啓動10個線程
  13.         System.out.println("main: starting 10 threads.");
  14.         for (int i = 0; i < 10; i++)
  15.         {
  16.             Thread th = new Thread(new TestRunnable(), "thread " + i);
  17.             th.start();
  18.         }
  19.     }
  20.     
  21.     private static class TestRunnable implements Runnable
  22.     {
  23.         @Override
  24.         public void run()
  25.         {
  26.             String thName = Thread.currentThread().getName();
  27.             try
  28.             {
  29.                 // 隨機等待5-14秒
  30.                 int n = (int)(Math.random() * 10 + 5);
  31.                 System.out.printf("%s: sleep for %d seconds. /n", thName, n);
  32.                 Thread.sleep(n * 1000);
  33.                 
  34.                 // 等待其他線程
  35.                 System.out.printf("%s: waiting for other threads. /n", thName);
  36.                 barrier.await();
  37.                 
  38.                 // 結束
  39.                 System.out.printf("%s: end. /n", thName);
  40.             }
  41.             catch (InterruptedException e)
  42.             {
  43.                 System.out.printf("%s: interrupted. /n", thName);
  44.                 return;
  45.             }
  46.             catch (BrokenBarrierException e)
  47.             {
  48.                 System.out.printf("%s: broken barrier. /n", thName);
  49.                 return;
  50.             }
  51.         }
  52.     }
  53. }

 

1.       在調用await方法時設置超時時間。

2.       當有一個await方法超時,所有處於await等待的線程將收到BrokenBarrierException,超時的線程自身將收到TimeoutException

3.       BrokenBarrier之後的所有await調用將直接拋出BrokenBarrierException

 

 
  1. import java.util.concurrent.BrokenBarrierException;
  2. import java.util.concurrent.CyclicBarrier;
  3. import java.util.concurrent.TimeUnit;
  4. import java.util.concurrent.TimeoutException;
  5. public class CyclicBarrierBrokenTest
  6. {
  7.     private static CyclicBarrier barrier = null;
  8.     
  9.     public static void main(String[] args)
  10.     {
  11.         // 初始化
  12.         barrier = new CyclicBarrier(10);
  13.         
  14.         // 啓動10個線程
  15.         System.out.println("main: starting 10 threads.");
  16.         for (int i = 0; i < 10; i++)
  17.         {
  18.             Thread th = new Thread(new TestRunnable(), "thread " + i);
  19.             th.start();
  20.         }
  21.     }
  22.     
  23.     private static class TestRunnable implements Runnable
  24.     {
  25.         @Override
  26.         public void run()
  27.         {
  28.             String thName = Thread.currentThread().getName();
  29.             try
  30.             {
  31.                 // 隨機等待5-14秒
  32.                 int n = (int)(Math.random() * 10 + 5);
  33.                 System.out.printf("%s: sleep for %d seconds. /n", thName, n);
  34.                 Thread.sleep(n * 1000);
  35.                 
  36.                 // 等待其他線程
  37.                 System.out.printf("%s: waiting for other threads. /n", thName);
  38.                 barrier.await(5, TimeUnit.SECONDS);
  39.                 
  40.                 // 結束
  41.                 System.out.printf("%s: end. /n", thName);
  42.             }
  43.             catch (InterruptedException e)
  44.             {
  45.                 System.out.printf("%s: interrupted. /n", thName);
  46.                 return;
  47.             }
  48.             catch (BrokenBarrierException e)
  49.             {
  50.                 System.out.printf("%s: broken barrier. /n", thName);
  51.                 return;
  52.             }
  53.             catch (TimeoutException e)
  54.             {
  55.                 System.out.printf("%s: timeout. /n", thName);
  56.                 return;
  57.             }
  58.         }
  59.     }
  60. }

當調用await線程達到CyclicBarrier的設置之後,CyclicBarrier將被重置,重新等待nawait調用。

 

  1. import java.util.concurrent.BrokenBarrierException;
  2. import java.util.concurrent.CyclicBarrier;
  3. public class CyclicBarrierResetTest
  4. {
  5.     private static CyclicBarrier barrier = null;
  6.     
  7.     public static void main(String[] args) throws Exception
  8.     {
  9.         barrier = new CyclicBarrier(2);
  10.         
  11.         Thread th1 = new Thread(new TestRunnable(), "thread 1");
  12.         Thread th2 = new Thread(new TestRunnable(), "thread 2");
  13.         
  14.         th1.start();
  15.         th2.start();
  16.         
  17.         Thread.sleep(60 * 1000);
  18.         
  19.         th1.interrupt();
  20.         th2.interrupt();
  21.         
  22.         th1.join();
  23.         th2.join();
  24.     }
  25.     
  26.     private static class TestRunnable implements Runnable
  27.     {
  28.         @Override
  29.         public void run()
  30.         {
  31.             String thName = Thread.currentThread().getName();
  32.             System.out.printf("%s: start. /n", thName);
  33.             
  34.             try
  35.             {
  36.                 while (true)
  37.                 {
  38.                     // 隨機產生1-100的整數。
  39.                     int c = (int)(Math.random() * 100) + 1;
  40.                     
  41.                     // 隨機等待0-4秒。
  42.                     int t = (int)(Math.random() * 5);
  43.                     System.out.printf("%s: sleep for %d seconds./n", thName, t);
  44.                     Thread.sleep(t * 1000);
  45.                     
  46.                     // 等待另1線程。
  47.                     barrier.await();
  48.                     
  49.                     // 輸出產生的整數。
  50.                     System.out.printf("%s: %d /n", thName, c);
  51.                     
  52.                     // 等待其他線程輸出。
  53.                     Thread.sleep(100);
  54.                 }
  55.             }
  56.             catch (InterruptedException e)
  57.             {
  58.                 System.out.printf("%s: interrupted. /n", thName);
  59.             }
  60.             catch (BrokenBarrierException e)
  61.             {
  62.                 e.printStackTrace();
  63.             }
  64.             
  65.             System.out.printf("%s: end. /n", thName);
  66.         }
  67.     }
  68. }

 

1.       在創建CyclicBarrier的時候指定通過Barrier時需要執行的語句。

2.       通過構造方法的Runnable參數指定。

3.       Runnable中的語句將由最後達到Barrier的線程執行。

 
  1. import java.util.concurrent.BrokenBarrierException;
  2. import java.util.concurrent.CyclicBarrier;
  3. public class CyclicBarrierActionTest
  4. {
  5.     private static CyclicBarrier barrier = null;
  6.     
  7.     public static void main(String[] args) throws Exception
  8.     {
  9.         barrier = new CyclicBarrier(10new BarrierAction());
  10.         
  11.         for (int i = 0; i < 10; i++)
  12.         {
  13.             Thread th = new Thread(new TestRunnable(), "thread " + i);
  14.             th.start();
  15.         }
  16.     }
  17.     
  18.     private static class TestRunnable implements Runnable
  19.     {
  20.         @Override
  21.         public void run()
  22.         {
  23.             try
  24.             {
  25.                 String thName = Thread.currentThread().getName();
  26.                 // System.out.printf("%s: start. /n", thName);
  27.                 
  28.                 // 隨機等待0-9秒
  29.                 int t = (int)(Math.random() * 10);
  30.                 System.out.printf("%s: sleep for %d seconds. /n", thName, t);
  31.                 Thread.sleep(t * 1000);
  32.                 
  33.                 // 等待barrier
  34.                 barrier.await();
  35.                 System.out.printf("%s: end. /n", thName);
  36.             }
  37.             catch (InterruptedException e)
  38.             {
  39.                 e.printStackTrace();
  40.                 return;
  41.             }
  42.             catch (BrokenBarrierException e)
  43.             {
  44.                 e.printStackTrace();
  45.                 return;
  46.             }
  47.         }
  48.     }
  49.     
  50.     private static class BarrierAction implements Runnable
  51.     {
  52.         @Override
  53.         public void run()
  54.         {
  55.             String thName = Thread.currentThread().getName();
  56.             
  57.             System.out.printf("%s: barrier action. /n", thName);
  58.         }
  59.     }
  60. }

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章