天下脸皮共十分
我占八分

保护性暂停-解耦等待和生产

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();
}
}
赞(1) 打赏
未经允许不得转载:Stephen Young » 保护性暂停-解耦等待和生产
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏