使用Lock,wait/notify,Semaphore三种方式实现多线程通信
2013-10-08 14:56
477 查看
java的多线程通信有Lock,wait/notify,Semaphore三种方式,以一道常见面试题来简单演示这三种多线程通信方式。
两个线程循环间隔打印指定内容,一个打印从1到52的数字,一个打印从A到Z的字母,打印输出如下:
1
2
A
3
4
B
......
51
52
Z
使用Lock实现代码如下:
使用wait/notify实现代码如下:
使用Semaphore信号量的代码如下:
两个线程循环间隔打印指定内容,一个打印从1到52的数字,一个打印从A到Z的字母,打印输出如下:
1
2
A
3
4
B
......
51
52
Z
使用Lock实现代码如下:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ThreadCommunicationTest { private final Lock lock = new ReentrantLock(); private final Condition conditionA = lock.newCondition(); private final Condition conditionB = lock.newCondition(); private static char currentThread = 'A'; public static void main(String[] args) { ThreadCommunicationTest test = new ThreadCommunicationTest(); ExecutorService service = Executors.newCachedThreadPool(); service.execute(test.new RunnableA()); service.execute(test.new RunnableB()); service.shutdown(); } private class RunnableA implements Runnable { public void run() { for (int i = 1; i <= 52; i++) { lock.lock(); try { while (currentThread != 'A') { try { conditionA.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(i); if (i % 2 == 0) { currentThread = 'B'; conditionB.signal(); } } finally { lock.unlock(); } } } } private class RunnableB implements Runnable { public void run() { for (char c = 'A'; c <= 'Z'; c++) { lock.lock(); try { while (currentThread != 'B') { try { conditionB.await(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(c); currentThread = 'A'; conditionA.signal(); } finally { lock.unlock(); } } } } }
使用wait/notify实现代码如下:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadCommunicationTest2 { private static char currentThread = 'A'; private final Object t = new Object(); //使用一个同步对象保证两个线程之间每一时刻只有一个线程工作 public static void main(String[] args) { ThreadCommunicationTest2 test = new ThreadCommunicationTest2(); ExecutorService service = Executors.newCachedThreadPool(); service.execute(test.new RunnableA()); service.execute(test.new RunnableB()); service.shutdown(); } private class RunnableA implements Runnable { public void run() { for (int i = 1; i <= 52; i++) { synchronized (t) { if(currentThread != 'A'){ try { t.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(i); if (i % 2 == 0) { currentThread = 'B'; t.notifyAll(); } } } } } private class RunnableB implements Runnable { public void run() { for (char c = 'A'; c <= 'Z'; c++) { synchronized (t) { if(currentThread != 'B'){ try { t.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(c); currentThread = 'A'; t.notifyAll(); } } } } }
使用Semaphore信号量的代码如下:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; public class ThreadCommunicationTest3 { private final Semaphore semap = new Semaphore(1);//创建一个只有1个许可的信号量,保证两个线程间每一时刻只有一个在工作 private static char currentThread = 'A'; public static void main(String[] args) { ThreadCommunicationTest3 test = new ThreadCommunicationTest3(); ExecutorService service = Executors.newCachedThreadPool(); service.execute(test.new RunnableA()); service.execute(test.new RunnableB()); service.shutdown(); } private class RunnableA implements Runnable { public void run() { for (int i = 1; i <= 52; i++) { try { semap.acquire(); while (currentThread != 'A') { semap.release(); } System.out.println(i); if (i % 2 == 0) { currentThread = 'B'; } } catch (Exception e) { e.printStackTrace(); } finally { semap.release(); } } } } private class RunnableB implements Runnable { public void run() { for (char c = 'A'; c <= 'Z'; c++) { try { semap.acquire(); while (currentThread != 'B') { semap.release(); } System.out.println(c); currentThread = 'A'; } catch (Exception e) { e.printStackTrace(); } finally { semap.release(); } } } } }
相关文章推荐
- 使用Lock,wait/notify,Semaphore三种方式实现多线程通信
- 使用Lock,wait/notify,Semaphore三种方式实现多线程通信
- 线程系列08,实现线程锁的各种方式,使用lock,Montor,Mutex,Semaphore以及线程死锁
- Java可阻塞队列的两种实现方式 (传统wait/notify和jdk1.5以后的lock)
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- 详解python使用递归、尾递归、循环三种方式实现斐波那契数列
- 使用wait/notify实现生产者消费者模式
- Java:使用wait()与notify()实现线程间协作
- 使用wait和notify实现经典生产者消费者问题
- 【Java并发编程】:使用wait/notify/notifyAll实现线程间通信
- 【Java并发编程】之十:使用wait/notify/notifyAll实现线程间通信的几点重要说明(r)
- JS定义和直接使用匿名函数的三种实现方式
- struts2笔记Action实现的三种使用方式和路径通配符
- Java:使用wait()与notify()实现线程间协作
- JAVA多线程实现的三种方式(继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程)
- mybatis一对一的三种实现方式 数据准备: 使用mysql数据库作为测试。建表语句及测试数据: CREATE TABLE `classes` ( `class_id` int(11) NOT
- 使用Notify 和 wait ,使用Linklist实现生产者消费者问题
- 使用javascript实现在页面打印的效果的三种方式
- Java:使用wait()与notify()实现线程间协作
- java使用轮询和wait()/notify()实现多线程之间的通信