ReentrantLock
发布日期:2021-10-18 18:51:26
浏览次数:2
分类:技术文章
本文共 8250 字,大约阅读时间需要 27 分钟。
import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 一道经典的Java多线程编程题 * Created by yuanyong on 17/2/7. * 问题描述 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到打印到75. 程序的输出结果应该为: */public class ThreadDemo2 { final Lock lock = new ReentrantLock(); final Condition condition_1 = lock.newCondition(); final Condition condition_2 = lock.newCondition(); final Condition condition_3 = lock.newCondition(); public static volatile int number = 0; class RunClass implements Runnable { ConcurrentLinkedQueuestack; RunClass(Integer id, ConcurrentLinkedQueue stack) { this.id = id; this.stack = stack; } private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } private void wirte() { int i = 5; while (i > 0) { number++; System.out.println(Thread.currentThread().getName() + ":" + number); i--; } } @Override public void run() { switch (id) { case 1: while (stack.size() > 0) { //第一次 if (number == 0) { lock.lock(); try { wirte(); stack.poll(); condition_2.signal(); } finally { lock.unlock(); } } else { lock.lock(); try { condition_1.await(); if (stack.poll() == number) { wirte(); condition_2.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } } break; case 2: while (stack.size() > 0) { lock.lock(); try { condition_2.await(); if (stack.poll() == number) { wirte(); condition_3.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; case 3: while (stack.size() > 0) { lock.lock(); try { condition_3.await(); if (stack.poll() == number) { wirte(); condition_1.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; } } } public static void main(String args[]) { ConcurrentLinkedQueue stack1 = new ConcurrentLinkedQueue (); ConcurrentLinkedQueue stack2 = new ConcurrentLinkedQueue (); ConcurrentLinkedQueue stack3 = new ConcurrentLinkedQueue (); ThreadDemo2 threadDemo2 = new ThreadDemo2(); RunClass runClass = threadDemo2.new RunClass(1, stack1); RunClass runClass2 = threadDemo2.new RunClass(2, stack2); RunClass runClass3 = threadDemo2.new RunClass(3, stack3); Thread thread1 = new Thread(runClass, "线程1"); Thread thread2 = new Thread(runClass2, "线程2"); Thread thread3 = new Thread(runClass3, "线程3"); int n_1 = 0; int n_2 = 5; int n_3 = 10; for (int n = 0; n < 5; n++) { stack1.add(n_1); stack2.add(n_2); stack3.add(n_3); n_1 += 15; n_2 += 15; n_3 += 15; } thread3.start(); thread2.start(); thread1.start(); }}
实例2
线程屏障CountDownLatch单词使用
import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 一道经典的Java多线程编程题 * Created by yuanyong on 17/2/7. * 问题描述 启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20....以此类推, 直到打印到75. 程序的输出结果应该为: */public class ThreadDemo2 { ConcurrentLinkedQueuestack1 = new ConcurrentLinkedQueue (); ConcurrentLinkedQueue stack2 = new ConcurrentLinkedQueue (); ConcurrentLinkedQueue stack3 = new ConcurrentLinkedQueue (); final CountDownLatch startGate=new CountDownLatch(1);// final CountDownLatch endGate=new CountDownLatch(3); final Lock lock = new ReentrantLock(); final Condition condition_1 = lock.newCondition(); final Condition condition_2 = lock.newCondition(); final Condition condition_3 = lock.newCondition(); public static volatile int number = 0; class RunClass2 implements Runnable{ public void run() { //第一次 if (number == 0) { lock.lock(); try { wirte(); stack1.poll(); condition_2.signal(); } finally { lock.unlock(); } } } } private void wirte() { int i = 5; while (i > 0) { number++; System.out.println(Thread.currentThread().getName() + ":" + number); i--; } } class RunClass implements Runnable { ConcurrentLinkedQueue stack; RunClass(Integer id, ConcurrentLinkedQueue stack) { this.id = id; this.stack = stack; } private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public void run() {// endGate.countDown();// System.out.println(endGate.getCount()); try { startGate.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("switch--------"); switch (id) { case 1: while (stack.size() > 0) { lock.lock(); try { condition_1.await(); if (stack.poll() == number) { wirte(); condition_2.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; case 2: while (stack.size() > 0) { lock.lock(); try { condition_2.await(); if (stack.poll() == number) { wirte(); condition_3.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; case 3: while (stack.size() > 0) { lock.lock(); try { condition_3.await(); if (stack.poll() == number) { wirte(); condition_1.signal(); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } break; } } public ConcurrentLinkedQueue getStack() { return stack; } public void setStack(ConcurrentLinkedQueue stack) { this.stack = stack; } } public static void main(String args[]) throws BrokenBarrierException, InterruptedException { ThreadDemo2 threadDemo2 = new ThreadDemo2(); RunClass runClass = threadDemo2.new RunClass(1, threadDemo2.stack1); RunClass runClass2 = threadDemo2.new RunClass(2, threadDemo2.stack2); RunClass runClass3 = threadDemo2.new RunClass(3, threadDemo2.stack3); Thread thread1 = new Thread(runClass, "线程1"); Thread thread2 = new Thread(runClass2, "线程2"); Thread thread3 = new Thread(runClass3, "线程3"); int n_1 = 0; int n_2 = 5; int n_3 = 10; for (int n = 0; n < 5; n++) { threadDemo2.stack1.add(n_1); threadDemo2.stack2.add(n_2); threadDemo2.stack3.add(n_3); n_1 += 15; n_2 += 15; n_3 += 15; }// threadDemo2.endGate.wait(); thread1.start(); thread2.start(); thread3.start(); Thread.sleep(3000); threadDemo2.startGate.countDown(); new Thread(threadDemo2.new RunClass2()).start(); }}
CyclicBarrier可以重复使用对应博文地址:
转载地址:https://blog.csdn.net/u011153869/article/details/54911737 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月11日 20时11分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
还在搞三层架构?DDD 分层架构了解下!
2019-04-27
字节跳动面试流程和考点都在这了!!
2019-04-27
最牛逼的数据库性能测试工具,没有之一!
2019-04-27
Spring 天天用,bean 懒加载原理你懂吗?
2019-04-27
如何设计一个完美的权限管理模块?
2019-04-27
离字节跳动最近的机会,来了!
2019-04-27
Java 正则表达式处理复杂文本,效率就是高!
2019-04-27
最牛逼的 Java 日志框架,性能无敌,横扫所有对手。。
2019-04-27
浅谈人才网门户运营初期六大方案
2019-04-27
谷粒商城46 - 前端-商品服务-API-三级分类-基本修改效果完成
2019-04-27
Java启动线程的正确方式 & 为什么 run()方法不是启动线程的方式
2019-04-27
Java多线程 while内try/catch 导致自动清除中断信号
2019-04-27
Java多线程 守护线程
2019-04-27
Java多线程 线程的优先级&四个线程属性的总结
2019-04-27
Java多线程 线程属性面试问题
2019-04-27
Java多线程 需要UncaughtException的3个理由
2019-04-27
Java多线程 自己实现异常处理器
2019-04-27
Java多线程 线程未捕获的异常-常见的面试问题
2019-04-27