首页
注册
登录
yehen 最近的时间轴更新
yehen
V2EX 第 394372 号会员,加入于 2019-03-22 15:30:46 +08:00
yehen
提问
技术话题
好玩
工作信息
交易信息
城市相关
yehen 最近回复了
2019-07-16 14:43:59 +08:00
回复了
lisisi
创建的主题
›
程序员
›
Notion、evernote 和 onenote 程序员适合用哪个做笔记?
有道云笔记
不过有些成型的东西直接写在博客上了
2019-03-29 13:10:24 +08:00
回复了
samray
创建的主题
›
程序员
›
一条面试题引发的思考--浅谈 Java 公平锁与内存模型
package com.ljw.HelloJava;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
public class ABCThreads {
private static Integer index = 0;
private static Integer max = 6;
private static Object lock = new Object();
public static void main(String[] args) {
Thread a = getThread(i -> i % 3 == 0, "A");
Thread b = getThread(i -> i % 3 == 1, "B");
Thread c = getThread(i -> i % 3 == 2, "C");
a.start();
b.start();
c.start();
}
private static Thread getThread(Predicate<Integer> condition, String value) {
return new Thread(() -> {
while (true) {
synchronized (lock) {
while (!condition.test(index)) {
try {
lock.wait();
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
if (index >= max) {
return;
}
System.out.printf("index:%s,value:%s\n", index, value);
index++;
lock.notifyAll();
}
}
});
}
}
»
yehen 创建的更多回复
关于
·
帮助文档
·
博客
·
API
·
FAQ
·
实用小工具
·
2710 人在线
最高记录 6679
·
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms ·
UTC 15:10
·
PVG 23:10
·
LAX 08:10
·
JFK 11:10
Developed with
CodeLauncher
♥ Do have faith in what you're doing.