![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
并发编程
扬帆丶起航
乘风破浪会有时,直挂云帆济沧海
展开
-
AQS - ReentrantLock - 非公平锁
加锁过程原创 2021-03-09 20:14:24 · 98 阅读 · 0 评论 -
如何停止一个正在运行的线程?
设置flag 主线程修改flag之后确保子线程能感知到改变,然后子线程跳出循环 public class Main { static volatile boolean flag = true; public static void main(String[] args) throws ExecutionException, InterruptedException { System.out.println("start"); Thread thread原创 2021-03-07 10:03:39 · 107 阅读 · 1 评论 -
volatile关键字
volatile可见性测试 不加volatile 线程1start之后一直空转,陷入死循环,因为线程1读取的一直是自己内存的副本数据,线程1并不知道要去主内存拉取新数据 public class Main extends Thread { static boolean flag = true; public static void main(String[] args) throws InterruptedException { new Thread(() -&g原创 2021-03-04 17:18:33 · 123 阅读 · 2 评论 -
生产者-消费者模型
有一个苹果箱,有10个人向这个箱子中每次随机放入一个苹果,有10个人每次随机从这个箱子中随机拿走一个苹果,同时需要满足箱子中的苹果总数不能超过50个。请用代码实现上面的场景 wait-notify public class AppleBox { private int appleCount; public synchronized void putApple() { while (appleCount >= 50) { try { .原创 2021-03-04 10:30:36 · 124 阅读 · 1 评论