package cc.mrbird.febs.pay.util;
|
|
import cc.mrbird.febs.common.entity.FebsResponse;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Slf4j
|
public class PayThreadPool {
|
|
private static Map<Integer, Object> PAY_WAIT_POOL = new HashMap<>();
|
private static Map<Integer, FebsResponse> PAY_THREAD_MSG = new HashMap<>();
|
|
public static void waitThread(Integer orderId, Object lock) {
|
PAY_WAIT_POOL.put(orderId, lock);
|
synchronized (lock) {
|
try {
|
// 设置线程最多等待15s
|
lock.wait(1000 * 15);
|
} catch (InterruptedException e) {
|
log.debug("线程等待失败", e);
|
}
|
}
|
|
}
|
|
public static FebsResponse getThreadResult(Integer orderId) {
|
//获取一次后删除结果对象
|
// TODO 需要优化可能存在对象积压过多内存溢出风险
|
return PAY_THREAD_MSG.remove(orderId);
|
}
|
|
public static void notifyThread(Integer orderId, FebsResponse result) {
|
Object lock = PAY_WAIT_POOL.get(orderId);
|
if (lock != null) {
|
synchronized (lock) {
|
log.debug("尝试释放锁{}", orderId);
|
PAY_THREAD_MSG.put(orderId, result);
|
PAY_WAIT_POOL.remove(orderId);
|
lock.notifyAll();
|
}
|
}
|
}
|
|
}
|