使用Lock,wait/notify,Semaphore三种方式实现多线程通信

Java的多线程通信有Lock,wait/notify,Semaphore三种方式,以一道常见面试题来简单演示这三种多线程通信方式。

两个线程循环间隔打印指定内容,一个打印从1到52的数字,一个打印从A到Z的字母,打印输出如下:

1
2
A
3
4

B

......

51
52
Z

使用Lock实现代码如下:

[java]  view plain  copy
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.locks.Condition;  
  4. import java.util.concurrent.locks.Lock;  
  5. import java.util.concurrent.locks.ReentrantLock;  
  6.   
  7. public class ThreadCommunicationTest {    
  8.         
  9.     private final Lock lock = new ReentrantLock();    
  10.     
  11.     private final Condition conditionA = lock.newCondition();    
  12.     private final Condition conditionB = lock.newCondition();    
  13.     
  14.     private static char currentThread = 'A';    
  15.     
  16.     public static void main(String[] args) {    
  17.     
  18.         ThreadCommunicationTest test = new ThreadCommunicationTest();    
  19.     
  20.         ExecutorService service = Executors.newCachedThreadPool();    
  21.     
  22.         service.execute(test.new RunnableA());    
  23.         service.execute(test.new RunnableB());    
  24.     
  25.         service.shutdown();    
  26.     
  27.     }    
  28.     
  29.     private class RunnableA implements Runnable {    
  30.         public void run() {    
  31.             for (int i = 1; i <= 52; i++) {    
  32.                 lock.lock();    
  33.     
  34.                 try {    
  35.                     while (currentThread != 'A') {    
  36.                         try {    
  37.                             conditionA.await();    
  38.                         } catch (InterruptedException e) {    
  39.                             e.printStackTrace();    
  40.                         }    
  41.                     }    
  42.     
  43.                     System.out.println(i);    
  44.                     if (i % 2 == 0) {    
  45.                         currentThread = 'B';    
  46.                         conditionB.signal();    
  47.                     }    
  48.                 } finally {    
  49.                     lock.unlock();    
  50.                 }    
  51.             }    
  52.     
  53.         }    
  54.     
  55.     }    
  56.     
  57.     private class RunnableB implements Runnable {    
  58.         public void run() {    
  59.             for (char c = 'A'; c <= 'Z'; c++) {    
  60.                 lock.lock();    
  61.                 try {    
  62.                     while (currentThread != 'B') {    
  63.                         try {    
  64.                             conditionB.await();    
  65.                         } catch (InterruptedException e) {    
  66.                             e.printStackTrace();    
  67.                         }    
  68.                     }    
  69.     
  70.                     System.out.println(c);    
  71.                     currentThread = 'A';    
  72.                     conditionA.signal();    
  73.                 } finally {    
  74.                     lock.unlock();    
  75.                 }    
  76.             }    
  77.     
  78.         }    
  79.     
  80.     }    
  81. }    
使用wait/notify实现代码如下:

[java]  view plain  copy
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3.   
  4.   
  5. public class ThreadCommunicationTest2 {    
  6.       
  7.     private static char currentThread = 'A';   
  8.     private final Object t = new Object(); //使用一个同步对象保证两个线程之间每一时刻只有一个线程工作  
  9.         
  10.     public static void main(String[] args) {    
  11.         ThreadCommunicationTest2 test = new ThreadCommunicationTest2();    
  12.         ExecutorService service = Executors.newCachedThreadPool();    
  13.           
  14.         service.execute(test.new RunnableA());    
  15.         service.execute(test.new RunnableB());    
  16.     
  17.         service.shutdown();    
  18.     }    
  19.       
  20.     private class RunnableA implements Runnable {  
  21.         public void run() {    
  22.             for (int i = 1; i <= 52; i++) {    
  23.                 synchronized (t) {    
  24.                     if(currentThread != 'A'){    
  25.                         try {    
  26.                             t.wait();    
  27.                         } catch (InterruptedException e) {    
  28.                             e.printStackTrace();    
  29.                         }    
  30.                     }    
  31.                     System.out.println(i);    
  32.                     if (i % 2 == 0) {    
  33.                         currentThread = 'B';    
  34.                         t.notifyAll();    
  35.                     }    
  36.                 }    
  37.             }    
  38.         }  
  39.     }  
  40.       
  41.     private class RunnableB implements Runnable {  
  42.         public void run() {    
  43.             for (char c = 'A'; c <= 'Z'; c++) {   
  44.                 synchronized (t) {    
  45.                     if(currentThread != 'B'){    
  46.                         try {    
  47.                             t.wait();    
  48.                         } catch (InterruptedException e) {    
  49.                             e.printStackTrace();    
  50.                         }    
  51.                     }    
  52.                     System.out.println(c);    
  53.                     currentThread = 'A';    
  54.                     t.notifyAll();    
  55.                 }    
  56.             }    
  57.         }  
  58.     }  
  59.     
  60. }  
使用Semaphore信号量的代码如下:

[java]  view plain  copy
  1. import java.util.concurrent.ExecutorService;  
  2. import java.util.concurrent.Executors;  
  3. import java.util.concurrent.Semaphore;  
  4.   
  5. public class ThreadCommunicationTest3 {  
  6.   
  7.     private final Semaphore semap = new Semaphore(1);//创建一个只有1个许可的信号量,保证两个线程间每一时刻只有一个在工作  
  8.   
  9.     private static char currentThread = 'A';  
  10.   
  11.     public static void main(String[] args) {  
  12.   
  13.         ThreadCommunicationTest3 test = new ThreadCommunicationTest3();  
  14.   
  15.         ExecutorService service = Executors.newCachedThreadPool();  
  16.   
  17.         service.execute(test.new RunnableA());  
  18.         service.execute(test.new RunnableB());  
  19.   
  20.         service.shutdown();  
  21.   
  22.     }  
  23.   
  24.     private class RunnableA implements Runnable {  
  25.         public void run() {  
  26.             for (int i = 1; i <= 52; i++) {  
  27.                 try {  
  28.                     semap.acquire();  
  29.                     while (currentThread != 'A') {  
  30.                         semap.release();  
  31.                     }  
  32.                     System.out.println(i);  
  33.                     if (i % 2 == 0) {  
  34.                         currentThread = 'B';  
  35.                     }  
  36.                 } catch (Exception e) {  
  37.                     e.printStackTrace();  
  38.                 } finally {  
  39.                     semap.release();  
  40.                 }  
  41.             }  
  42.         }  
  43.     }  
  44.   
  45.     private class RunnableB implements Runnable {  
  46.         public void run() {  
  47.             for (char c = 'A'; c <= 'Z'; c++) {  
  48.                 try {  
  49.                     semap.acquire();  
  50.                     while (currentThread != 'B') {  
  51.                         semap.release();  
  52.                     }  
  53.                     System.out.println(c);  
  54.                     currentThread = 'A';  
  55.                 } catch (Exception e) {  
  56.                     e.printStackTrace();  
  57.                 } finally {  
  58.                     semap.release();  
  59.                 }  
  60.             }  
  61.         }  
  62.   
  63.     }  
  64. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值