1. 模拟Queue
package com.gf.conn009; import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger; /**
* 模拟Queue
* @author huanchu
*
*/
public class MyQueue { private final LinkedList<Object> list = new LinkedList<Object>(); private final AtomicInteger count = new AtomicInteger(0); private final int maxSize; private final int minSize = 0; private final Object lock = new Object(); public MyQueue(int maxSize){
this.maxSize = maxSize;
} public void put(Object obj){
synchronized (lock) {
while (count.get() == maxSize) { try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} list.add(obj);
count.incrementAndGet();
System.out.println(" 元素 " + obj + " 被添加"); lock.notify();
}
} public Object take(){
Object temp = null;
synchronized (lock) {
while (count.get() == minSize) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} count.decrementAndGet();
temp = list.removeFirst();
System.out.println(" 元素" + temp + "被消费");
lock.notify();
}
return temp;
} public int size(){
return count.get();
} public static void main(String[] args) throws InterruptedException { final MyQueue m = new MyQueue(5);
m.put("a");
m.put("b");
m.put("c");
m.put("d");
m.put("e");
System.out.println("当前元素的个数:" + m.size()); Thread t1 = new Thread(new Runnable() { @Override
public void run() {
m.put("h");
m.put("i");
}
},"t1"); Thread t2 = new Thread(new Runnable() { @Override
public void run() {
try {
Thread.sleep(1000);
Object t1 = m.take(); Thread.sleep(1000);
Object t2 = m.take();
} catch (InterruptedException e) {
e.printStackTrace();
} }
},"t2"); t1.start();
Thread.sleep(1000);
t2.start(); } }

关注我的公众号,精彩内容不能错过
1. 模拟Queue的更多相关文章
- wait , notify 模拟 Queue
package com.itdoc.multi.sync009; import java.util.LinkedList; import java.util.concurrent.TimeUnit; ...
- Codeforces Round #366 (Div. 2) C 模拟queue
C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- CF #366 DIV2 C. Thor 模拟 queue/stack降低复杂度
C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- scheme 模拟queue
[code 1] shows a implementation of queue. The function enqueue! returns a queue in that the obj is a ...
- 模拟Queue(wait/notify)
BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面的两个方法put和take. put(anObje ...
- 用数组模拟STL中的srack(栈)和queue(队列)
我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...
- Team Queue(STL练习题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1387 Team Queue Time Limit: 2000/1000 MS (Java/Others ...
- new、delete、以及queue类
本来以为很容易的,结果还是写了我两个小时. 用指针模拟queue类,再加上类,各种错误,总算是解决掉了-- #include<iostream> #include<cstdlib&g ...
- 团体队列 UVA540 Team Queue
题目描述 有t个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后.如果没有任何一个队友排队,则他会被排到长队的队尾. 输入每个团队中所有队员的编号,要求 ...
随机推荐
- 【腾讯Bugly干货分享】Android 新一代多渠道打包神器
关于作者: 李涛,腾讯Android工程师,14年加入腾讯SNG增值产品部,期间主要负责手Q动漫.企鹅电竞等项目的功能开发和技术优化.业务时间喜欢折腾新技术,写一些技术文章,个人技术博客:www.lt ...
- [python] 3 、基于串口通信的嵌入式设备上位机自动测试程序框架(简陋框架)
星期一, 20. 八月 2018 01:53上午 - beautifulzzzz 1.前言 做类似zigbee.ble mesh...无线网络节点性能测试的时候,手动操作然后看表象往往很难找出真正的原 ...
- python 分片、截断序列
200 ? "200px" : this.width)!important;} --> 介绍 这篇文章主要介绍python对序列的分片方法.通过分片规则可以很简单的处理一些复 ...
- Chapter 8 The Simplest Plug-in Solution
This chapter introduces the simplest plug-in solution that are applicable to the four major componen ...
- JSON Web Token(JWT)使用步骤说明
在JSON Web Token(JWT)原理和用法介绍中,我们了解了JSON Web Token的原理和用法的基本介绍.本文我们着重讲一下其使用的步骤: 一.JWT基本使用 Gradle下依赖 : c ...
- FFmpeg 学习(七):FFmpeg 学习整理总结
一.FFmpeg 播放视频的基本流程整理 播放流程: video.avi(Container) -> 打开得到 Video_Stream -> 读取Packet -> 解析到 Fra ...
- Not posting notification with icon==0问题解决
问题:E/NotificationService: Not posting notification with icon==0: Notification(pri=0 contentView=null ...
- Numpy学习三:数组运算
1.转置 #reshape(shape)函数改变数组形状,shape是一个元组,表示数组的形状 创建一个包含15个元素的一维数组,通过reshape函数调整数组形状为3行5列的二维数组arr = np ...
- Day10:html和css
Day10:html和css <html> <body> <h1>标题</h1> <p>段落</p> </body> ...
- MySQL 非空约束位置不同对自增列造成的影响
MySQL版本 select version(); +------------+ | version() | +------------+ | 5.7.21-log | +------------+ ...