可以利用阻塞队列实现同步通知的功能。假如有A、B两个线程,共同执行一个任务,彼此通知、交替执行该任务。如图:

队列(二)_java队列

通过代码看一下执行的效果:

  • 1-1 一个阻塞队列实现同步通知的例子。
    1. import java.util.Collections; 
    2. import java.util.concurrent.ArrayBlockingQueue; 
    3. import java.util.concurrent.BlockingQueue; 
    4. import java.util.concurrent.atomic.AtomicInteger; 
    5.  
    6. /*
    7.  * 子线程循环10次,接着主线程循环100次,接着有回到子线程循环10次, 
    8.  * 接着再回到主线程循环100次,如此循环50次,请写出程序。 
    9.  * 用两个具有1个空间的队列来实现同步通知单的功能 
    10.  */ 
    11. public class BlockQueueConmunication1 { 
    12.     public static void main(String[] args) { 
    13.         final Business business=new Business(); 
    14.         new Thread( 
    15.                 new Runnable() { 
    16.                     @Override 
    17.                     public void run() { 
    18.                         for(int i=1;i<=50;i++){ 
    19.                             business.sub(i); 
    20.                         } 
    21.                     } 
    22.                 } 
    23.         ).start(); 
    24.          
    25.         for(int i=1;i<=50;i++){ 
    26.             business.main(i); 
    27.         } 
    28.     } 
    29.      
    30.     static class Business{ 
    31.         BlockingQueue<Integer> queue1=new ArrayBlockingQueue<Integer>(1);//2个队列 
    32.         BlockingQueue<Integer> queue2=new ArrayBlockingQueue<Integer>(1); 
    33.         //匿名构造方法,创建几个对象调用几次,而static只调用一次 
    34.         {        //构造方法先给他=赋值,让queue2先不要执行 
    35.             //Collections.synchronizedMap(null);//并发的集合,在这只是在练习的熟悉,与本程序没有关系 
    36.             try { 
    37.                 queue2.put(1); 
    38.                 //System.out.println("sahdghsagdhasdgh");//测试语句,检查知否执行到此处 
    39.             } catch (InterruptedException e) { 
    40.                 e.printStackTrace(); 
    41.             } 
    42.         } 
    43.         public  void sub(int i){ 
    44.                 try { 
    45.                     queue1.put(1); 
    46.                     //System.out.println("sahdghsagdhasdgh"); 
    47.                 } catch (InterruptedException e) { 
    48.                     // TODO Auto-generated catch block 
    49.                     e.printStackTrace(); 
    50.                 } 
    51.                 for (int j = 1; j <= 10; j++) { 
    52.                     System.out.println("sub thread sequece of "+j+",loop of"+i); 
    53.                 } 
    54.                 try { 
    55.                     queue2.take(); 
    56.                 } catch (InterruptedException e) { 
    57.                     e.printStackTrace(); 
    58.                 } 
    59.                  
    60.         } 
    61.          
    62.         public  void main(int i){ 
    63.             try { 
    64.                 queue2.put(1); 
    65.             } catch (InterruptedException e) { 
    66.                 e.printStackTrace(); 
    67.             }//刚开始的时候是满的,上面已经给他赋值了 
    68.             for (int j = 1; j <= 100; j++) { 
    69.                 System.out.println("main thread sequece of "+j+",loop of"+i); 
    70.             } 
    71.             try { 
    72.                 queue1.take(); 
    73.             } catch (InterruptedException e) { 
    74.                 e.printStackTrace(); 
    75.             } 
    76.         } 
    77.     }
    78. }

上述代码实是在 一个线程通信的例子 的基础上修改的,感兴趣的朋友可以看一下。 两段代码实现了相同的功能。