模拟Queue(wait/notify)
public class MyQueue { // 1 需要一个承装元素的集合
private LinkedList<Object> list = new LinkedList<Object>(); // 2 需要一个计数器
private AtomicInteger count = new AtomicInteger(0); // 3 需要制定上限和下限
private final int minSize = 0; private final int maxSize; // 4 构造方法
public MyQueue(int size) {
this.maxSize = size;
} // 5 初始化一个对象 用于加锁
private final Object lock = new Object(); // put(anObject):
// 把anObject加到BlockingQueue里,如果BlockQueue没有空间,则调用此方法的线程被阻断,直到BlockingQueue里面有空间再继续.
public void put(Object obj) {
synchronized (lock) {
while (count.get() == this.maxSize) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 1 加入元素
list.add(obj);
// 2 计数器累加
count.incrementAndGet();
// 3 通知另外一个线程(唤醒)
lock.notify();
System.out.println("新加入的元素为:" + obj);
}
} // take:
// 取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到BlockingQueue有新的数据被加入.
public Object take() {
Object ret = null;
synchronized (lock) {
while (count.get() == this.minSize) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 1 做移除元素操作
ret = list.removeFirst();
// 2 计数器递减
count.decrementAndGet();
// 3 唤醒另外一个线程
lock.notify();
}
return ret;
} public int getSize() {
return this.count.get();
} public static void main(String[] args) { final MyQueue mq = new MyQueue(5);
mq.put("a");
mq.put("b");
mq.put("c");
mq.put("d");
mq.put("e"); System.out.println("当前容器的长度:" + mq.getSize()); Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
mq.put("f");
mq.put("g");
}
}, "t1"); t1.start(); Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
Object o1 = mq.take();
System.out.println("移除的元素为:" + o1);
Object o2 = mq.take();
System.out.println("移除的元素为:" + o2);
}
}, "t2"); try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
} t2.start();
}
}
模拟Queue(wait/notify)的更多相关文章
- wait , notify 模拟 Queue
package com.itdoc.multi.sync009; import java.util.LinkedList; import java.util.concurrent.TimeUnit; ...
- 1. 模拟Queue
package com.gf.conn009; import java.util.LinkedList; import java.util.concurrent.atomic.AtomicIntege ...
- 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 ...
- 用数组模拟STL中的srack(栈)和queue(队列)
我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...
- Java Thread wait, notify and notifyAll Example
Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...
- 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 ...
随机推荐
- sql日期提取
--插入数据修改不行:必须提供学号 insert into Student(生日类型) values('阳历') --把月份提取出来 显示两位数 select DATENAME(month,getda ...
- Java中“==”、“compareTo()”和“equals()”的区别
在比较两个对象或者数据大小的时候,经常会用到==.compareTo()和equals(),尤其是在接入了Comparable接口后重写compareTo方法等场景,所以我们来理一下这三个的区别. 1 ...
- 取三级分销上下级用户id
//取上三级的用户idpublic function _get_up_third_id($member_id){ $up_id=array(); $invite_id=dbselect('invite ...
- How To:配置Linux iSCSI客户端
1.安装客户端 [root@node01 Packages]# rpm -Uvh iscsi-initiator-utils-6.2.0.873-2.el6.x86_64.rpm warning: i ...
- (C/C++学习)12.获取系统时间制作时钟(system()略解)
说明:通过调用函数来获取系统当前时间,并制作一个数字式的时钟,时钟的显示包括年.月.日.小时.分以及秒,通过系统屏幕的刷新来对不断更新的时间进行屏幕的显示. 一.对相关函数的学习 1.time_t t ...
- C#学习笔记_12_枚举&结构体
12_枚举&结构体 枚举 是一种数据类型 适用于某些取值范围有限的数据 语法: [访问权限修饰符] enum 枚举名 { 枚举值 } 枚举名遵循大驼峰命名法 枚举一般情况下是和switch c ...
- Maven学习总结(十一)——Maven项目对象模型pom.xml文件详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 洛谷——P2722 总分 Score Inflation
https://www.luogu.org/problem/show?pid=2722 题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这 ...
- wait-notify模型面试题
一道面试题: 启动两个线程, 一个输出 1,3,5,7-99, 另一个输出 2,4,6,8-100 最后 STDOUT 中按序输出 1,2,3,4,5-100 错误实现1: public class ...
- [Cypress] Create True end-to-end Tests with Cypress (Smoke test)
Integration tests let us keep our tests fast and reliable. They also allow us to test scenarios that ...