利用多condition动态管理池化的异步资源背景,java面试场景设计题

Stream.iterate(0, n -> n + 1).limit(workerConditions.size())
.forEach( i -> poolExecutor.submit(new KeyPairRunable(cacheDAO, lock, workerConditions.get(i))));
}

class KeyPairRunable implements Runnable {

private final RedisCacheDAO cacheDAO;
private final ReentrantLock lock;
private final Condition condition;

public KeyPairRunable(RedisCacheDAO cacheDAO, ReentrantLock lock, Condition condition) {
this.cacheDAO = cacheDAO;
this.lock = lock;
this.condition = condition;
}

@Override
public void run() {

while(true) {

String keyBytes = genKeyPair();

try {
int currentSize = cacheDAO.listLpush(keyBytes);
// 写入记录后实时返回当前List元素数
if(currentSize >= RedisCacheDAO.MAX_CACHE_SIZE) {
System.out.println(“cache is full. " + Thread.currentThread().getName() + " ready to park.”);

lock.lock();
condition.await();

System.out.println(“cache is consuming. " + Thread.currentThread().getName() + " unparked.”);
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " is interuupted.");
} finally {

if(lock.isLocked()) {
lock.unlock();
}
}
}
}

private String genKeyPair() {

// TODO 秘钥对桩
return “”;
}
}

class KeyPairGeneratorThreadFactory implements ThreadFactory {

private final String threadGroupName;
private final AtomicInteger idSeq;

public KeyPairGeneratorThreadFactory(String threadGroupName, int maxSeq) {
this.threadGroupName = threadGroupName;
this.idSeq = new AtomicInteger(maxSeq);
}

@Override
public Thread newThread(Runnable r) {

int threadId = idSeq.getAndDecrement();
if(threadId < 0) {
throw new UnsupportedOperationException(“thread number cannot be out of range”);
}

return new Thread(r, threadGroupName + “_” + threadId);
}
}
}

秘钥对生成monitor

/**

  • 秘钥对生成定时调度
    */
    public enum KeyPairsMonitor {

INSTANCE;

private final ReentrantLock reentrantLock;
private final List conditionList;
private final RedisCacheDAO redisCacheDAO;
private final int coreSize;

KeyPairsMonitor() {

this.redisCacheDAO = new RedisCacheDAO();
this.reentrantLock = new ReentrantLock();

coreSize = Runtime.getRuntime().availableProcessors();
this.conditionList = new ArrayList<>(coreSize);
for( int i=0; i< coreSize; i++ ) {
conditionList.add(reentrantLock.newCondition());
}
}

/**

  • 启动密钥生成任务,开启调度
    */
    public void monitor() {

KeyPairExecutors executors = new KeyPairExecutors(reentrantLock, conditionList);
executors.start();

buildMonitorSchedule();
}

/**

  • 构造定时任务
    */
    private void buildMonitorSchedule() {

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(new Runnable() {

@Override
public void run() {
int currentSize = redisCacheDAO.listLlen();
System.out.println("current cache size is: " + currentSize);

int executNum = 0;
if(currentSize <= RedisCacheDAO.HALF_MAX_CACHE_SIZE) {
System.out.println(“current cache level is under 50% to .” + currentSize);
executNum = coreSize;
} else if(currentSize <= RedisCacheDAO.PERCENT_75_MAX_CACHE_SIZE) {
System.out.println(“current cache level is under 75% to .” + currentSize);
executNum = coreSize >> 1;
}

for(int i=0; i < executNum; i++) {
try {
reentrantLock.lock();
conditionList.get(i).signal();
} catch (IllegalMonitorStateException e) {
// do nothing, condition no await
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if(reentrantLock.isLocked()) {
reentrantLock.unlock();
}
}

}
}

}, 0, 5, TimeUnit.SECONDS);
}

打桩redis操作List操作:

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
img

pTJat2Ns-1710918127693)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-XDBquHxK-1710918127694)]

  • 15
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值