blocked Queue

有几种blocked Queue
系统开发中,经常会用到 阻塞型的容器,适合于实现“消费者生产者”模式

转发

类图介绍

这里写图片描述

核心接口

放入数据

  • offer(anObject): 非阻塞, 表示如果可能的话,如可容纳,return true;
  • offer(E o, long timeout, TimeUnit unit), 阻塞+超时;
  • put(anObject): 一直阻塞;

获取数据

  • poll(time): 阻塞+超时, 取走首位的对象
  • take(): 一直阻塞, 取走排在首位的对象;
  • drainTo(): 批量获取所有可用的数据对象(还可以指定获取数据的个数), 提升获取数据效率;不需要多次分批加锁或释放锁。

ArrayBlockingQueue

LinkedBlockingQueue

它对头和尾(取和添加操作)采用两把不同的锁,相对于ArrayBlockingQueue提高了吞吐量

ConcurrentLinkedQueue

是一个无锁的queue实现,它采用了一种无锁算法(在API中有说明),相比于传统的同步的queue来说吞吐量可以大大提高,同时它也不同于BlockingQueue,并不单单提供阻塞操作。它主要的目的是通过采用无锁的算法,使得read/write操作均不需要对容器加锁,提高容器吞吐量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多级反馈队列调度算法是一种常见的进程调度算法,它基于多个队列,每个队列具有不同的优先级,每个进程被分配到其中一个队列中,然后在各个队列中按照不同的优先级进行调度。 该算法的代码实现可以分为以下几个步骤: 1. 定义进程控制块(PCB)结构体,包含进程ID、状态、优先级等信息。 2. 定义多个队列,每个队列对应一个优先级,可以使用链表或数组实现。 3. 将所有进程按照优先级放入对应的队列中。 4. 定义一个调度器函数,每次从最高优先级的队列开始遍历,找到一个可运行的进程并运行它。如果该进程运行时间超过了一个时间片,则将其放入下一个优先级的队列中。 5. 如果当前队列为空,则转到下一个优先级的队列。如果所有队列都为空,则调度器函数结束。 6. 如果某个进程结束或者被阻塞,则将其从对应队列中移除。 下面是伪代码实现: ``` struct PCB { int pid; int state; int priority; // other information }; // define multiple queues queue<PCB> queue1; queue<PCB> queue2; queue<PCB> queue3; // initialize queues with processes void initQueues() { // put processes with priority 1 into queue1 // put processes with priority 2 into queue2 // put processes with priority 3 into queue3 } // scheduler function void scheduler() { while (true) { // check if any queue is not empty if (!queue1.empty()) { // find the first runnable process in queue1 PCB p = queue1.front(); queue1.pop(); // run the process for a time slice run(p); // check if the process has finished or blocked if (p.state == FINISHED) { // remove the process from the queue removeProcess(p); } else if (p.state == BLOCKED) { // put the process into the corresponding blocked queue blockProcess(p); } else { // put the process into the next lower priority queue queue2.push(p); } } else if (!queue2.empty()) { // do the same for queue2 and queue3 // ... } else if (!queue3.empty()) { // do the same for queue3 // ... } else { // all queues are empty, exit the scheduler function break; } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值