synchronized 版本
| package com.luoKing.PC; |
| |
| |
| |
| public class A { |
| public static void main(String[] args) { |
| Data data = new Data(); |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| try { |
| data.increment(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| },"A").start(); |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| try { |
| data.decrement(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| },"B").start(); |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| try { |
| data.increment(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| },"C").start(); |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| try { |
| data.decrement(); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| },"D").start(); |
| |
| |
| } |
| |
| } |
| |
| |
| class Data{ |
| private int num = 0; |
| |
| public synchronized void increment() throws InterruptedException { |
| while(num!=0){ |
| this.wait(); |
| } |
| num++; |
| System.out.println(Thread.currentThread().getName()+"->"+num); |
| this.notifyAll(); |
| } |
| |
| public synchronized void decrement() throws InterruptedException { |
| while(num==0){ |
| this.wait(); |
| } |
| num--; |
| System.out.println(Thread.currentThread().getName()+"->"+num); |
| this.notifyAll(); |
| } |
| } |
Lock 版本(指定线程运行)
线程运行顺序A->B->c->D->A
| package com.luoKing.PC; |
| |
| import java.util.concurrent.locks.Condition; |
| import java.util.concurrent.locks.Lock; |
| import java.util.concurrent.locks.ReentrantLock; |
| |
| public class B { |
| |
| public static void main(String[] args) { |
| Data2 data2 = new Data2(); |
| |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| data2.decrementA(); |
| } |
| },"A").start(); |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| data2.incrementB(); |
| } |
| },"B").start(); |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| data2.decrementC(); |
| } |
| },"C").start(); |
| new Thread(()->{ |
| for (int i = 0; i < 10; i++) { |
| data2.incrementD(); |
| } |
| },"D").start(); |
| |
| } |
| |
| |
| } |
| |
| class Data2{ |
| private int num = 0; |
| Lock lock = new ReentrantLock(); |
| |
| Condition conditionA = lock.newCondition(); |
| Condition conditionB = lock.newCondition(); |
| Condition conditionC = lock.newCondition(); |
| Condition conditionD = lock.newCondition(); |
| |
| public void decrementA(){ |
| lock.lock(); |
| try { |
| while(num!=0){ |
| conditionA.await(); |
| } |
| System.out.println(Thread.currentThread().getName()+"->"+num++); |
| conditionB.signal(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| public void decrementC(){ |
| lock.lock(); |
| try { |
| while(num!=0){ |
| conditionC.await(); |
| } |
| System.out.println(Thread.currentThread().getName()+"->"+num++); |
| conditionD.signal(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| public void incrementB(){ |
| lock.lock(); |
| |
| try { |
| while(num!=1){ |
| conditionB.await(); |
| } |
| System.out.println(Thread.currentThread().getName()+"->"+num--); |
| conditionC.signal(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| public void incrementD(){ |
| lock.lock(); |
| |
| try { |
| while(num!=1){ |
| conditionD.await(); |
| } |
| System.out.println(Thread.currentThread().getName()+"->"+num--); |
| conditionA.signal(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } finally { |
| lock.unlock(); |
| } |
| } |
| |
| |
| } |
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决