package tech.ityoung.study.demo.jvm.juc;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.events.Event;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class MultiGuardedDemo {
public static void main(String[] args) throws InterruptedException {
int loopNum = 3;
for (int i = 0; i < loopNum; i++) {
new People().start();
}
Thread.sleep(1000);
Set<Integer> boxIds = Mailbox.getBoxIds();
for (Integer boxId : boxIds) {
new Postman(boxId, "mail " + boxId).start();
}
}
}
@Slf4j
class People extends Thread {
@Override
public void run() {
GuardedObject go = Mailbox.createGuardedObject();
log.info("starting waiting for receiving mail No.{}", go.getId());
Object o = go.get(5000);
log.info("getting email completed:{}", o);
}
}
@Slf4j
class Postman extends Thread {
private int id;
private String mail;
public Postman(int id, String mail) {
this.id = id;
this.mail = mail;
}
@Override
public synchronized void run() {
GuardedObject go = Mailbox.getGuardedObject(id);
go.complete(mail);
log.info("starting sending mail, the number is {}, and the content is {}", id, mail);
}
}
class Mailbox {
private static Map<Integer, GuardedObject> boxes = new ConcurrentHashMap<>();
private static int id = 1;
public static synchronized int generateId() {
return ++id;
}
public static GuardedObject getGuardedObject(int id) {
return boxes.remove(id);
}
public static GuardedObject createGuardedObject() {
GuardedObject guardedObject = new GuardedObject(generateId());
boxes.put(guardedObject.getId(), guardedObject);
return guardedObject;
}
public static Set<Integer> getBoxIds() {
return boxes.keySet();
}
}
保护性暂停-解耦等待和生产
未经允许不得转载:Stephen Young » 保护性暂停-解耦等待和生产
相关推荐
-      ThreadLocal常规使用方法
-      手写线程池
-      CAS原理及应用
-      Jmeter特定位置增加定时器
-      Stream的一些高级用法
-      JVM结构及GC
-      @Autowired与@Resource
-      动态代理
评论前必须登录!
注册