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个团队的人正在排一个长队.每次新来一个人时,如果他有队友在排队,那么新人会插队到最后一个队友的身后.如果没有任何一个队友排队,则他会被排到长队的队尾. 输入每个团队中所有队员的编号,要求 ...
随机推荐
- 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框)
[源码下载] 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框) 作者:webabcd 介绍背水一战 Wind ...
- 导出到word
导出到excel功能会常做,但是导出到word功能很少做,项目遇到,在这里做一下标记. 导出到excel比较容易,excel都有固定格式也模板,但是word需要自己写模板,这里用了freemarker ...
- java IO流之详细总结
什么是io流? 分为两种: 输入流:可以从文件中读取到程序,从源数据源读取到程序,叫做输入流. 输出流:可以从程序中读取到文件,从程序写,使用输出流,写入到文件中.叫做输出流. 使用File操作文件或 ...
- 本地语音识别开源软件pocketsphinx调试总结
1问题一: fatal error: pocketsphinx.h: No such file or directory 解决方法: $ cd /usr/include $ sudo ln -s /m ...
- vue计算属性(通过计算得来的属性)
1.computed:是一个计算属性,用来监听属性的变化 eg: <p>computed:{{count}}</p> computed:{ count(){ c ...
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
0.背景 在开发项目的过程当中,生产环境与调试环境的配置肯定是不一样的.拿个最简单的例子来说,比如连接字符串这种东西,调试环境肯定是不能连接生产数据库的.在之前的话,这种情况只能说是你 COPY 两个 ...
- Android--UI之ImageView
前言 这篇博客聊一聊在Android下ImageView的使用,在此篇博客中,会讲解到ImageView的一些属性的使用,以及ImageView展示图片的放大.缩小.旋转等操作.最后再讲解一下Andr ...
- Chapter 4 Invitations——1
In my dream it was very dark, and what dim light there was seemed to be radiating from Edward's skin ...
- Go的50度灰:开发者要注意的陷阱和常见错误
Go是一门简单有趣的语言,但与其他语言类似,它会有一些技巧...这些技巧的绝大部分并不是Go的缺陷造成的.如果你以前使用的是其他语言,那么这其中的有些错误就是很自然的陷阱.其它的是由错误的假设和缺少细 ...
- java中Char到底是什么格式的编码
文本处理中经常有这样的逻辑: String s = new String(bts, "UTF-8"); 看String源代码,里面是一个char[],将bts按照某种编码方式,变成 ...