yehen 最近的时间轴更新
yehen

yehen

V2EX 第 394372 号会员,加入于 2019-03-22 15:30:46 +08:00
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();
}
}
});
}
}
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3853 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 05:08 · PVG 13:08 · LAX 22:08 · JFK 01:08
Developed with CodeLauncher
♥ Do have faith in what you're doing.