悲剧了(愈发的感觉 数据结构的重要性,一定要把数据结构的思想整明白)
2011-03-23 10:14
337 查看
这两天看了看池,循环队列没好好学习,算是重新来过了
protected int _next;
protected int _oldest;
关于队列满的情况判断,所以队列中只能存储size-1个值,如果使用
return (_next) % _size == _oldest;的话,初始就full了,而用next+1的话,
最后一个位置就不能够填上,等到oldest改变(即池中的数据取出之后才开始
继续填充)
package tests;
public class SyncQueue {
protected Object[] _array;
protected int _next;
protected int _oldest;
protected int _size;
public SyncQueue(int size) {
_array = new Object[size];
_size = size;
_oldest = 0;
_next = 0;
}
public synchronized void put(Object o) throws
ExceptionAdapter {
while (full()) {
try {
System.out.print("full;&&&&&&&wait\n");
wait();
} catch (InterruptedException ex) {
System.out.print("Exception");
throw new ExceptionAdapter(ex);
}
}
_array[_next] = o;
System.out.print("next="+_next+"\n");
_next = (_next + 1) % _size;
notify();
}
public synchronized Object get() {
while (empty()) {
try {
System.out.print("empty;&&&&&&&wait\n");
wait();
} catch (InterruptedException ex) {
try {
throw new ExceptionAdapter
(ex);
} catch (ExceptionAdapter e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
}
Object ret = _array[_oldest];
_oldest = (_oldest + 1) % _size;
notify();
return ret;
}
protected boolean empty() {
return _next == _oldest;
}
protected boolean full() {
return (_next+1) % _size == _oldest;
}
}
protected int _next;
protected int _oldest;
关于队列满的情况判断,所以队列中只能存储size-1个值,如果使用
return (_next) % _size == _oldest;的话,初始就full了,而用next+1的话,
最后一个位置就不能够填上,等到oldest改变(即池中的数据取出之后才开始
继续填充)
package tests;
public class SyncQueue {
protected Object[] _array;
protected int _next;
protected int _oldest;
protected int _size;
public SyncQueue(int size) {
_array = new Object[size];
_size = size;
_oldest = 0;
_next = 0;
}
public synchronized void put(Object o) throws
ExceptionAdapter {
while (full()) {
try {
System.out.print("full;&&&&&&&wait\n");
wait();
} catch (InterruptedException ex) {
System.out.print("Exception");
throw new ExceptionAdapter(ex);
}
}
_array[_next] = o;
System.out.print("next="+_next+"\n");
_next = (_next + 1) % _size;
notify();
}
public synchronized Object get() {
while (empty()) {
try {
System.out.print("empty;&&&&&&&wait\n");
wait();
} catch (InterruptedException ex) {
try {
throw new ExceptionAdapter
(ex);
} catch (ExceptionAdapter e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
}
}
Object ret = _array[_oldest];
_oldest = (_oldest + 1) % _size;
notify();
return ret;
}
protected boolean empty() {
return _next == _oldest;
}
protected boolean full() {
return (_next+1) % _size == _oldest;
}
}
相关文章推荐
- 【数据结构】数据结构之位图
- (6)Java数据结构-- 转:JAVA常用数据结构及原理分析
- 数据结构复习(重写某些重要数据结构API) ------------二叉树
- (8) 什么是数据结构--每天五分钟学习数据结构
- 基于半边数据结构(翼边数据结构)的Eular操作来实现扫成 通过OpenGL进行CAD实体建模 经测试可直接运行
- 【数据结构】数据、数据结构(分类)、抽象数据结构
- 完成端口中的单句柄数据结构与单IO数据结构的理解与设计
- 三.数据结构与基本绘图(2.常用图形数据结构)
- 数据结构第二章数据结构图
- java数据结构----数据结构之学习的点滴
- Laravel4.1数据库 数据库迁移案例2——创建数据结构,数据表,修改数据结构(五)
- 【数据结构与算法005】基本数据结构——图
- 数据结构:顺序结构和链式结构的数据类型定义
- 数据结构基础 之 二叉搜索树的思想与实现
- 数据结构之线索二叉树(整理严蔚敏数据结构)
- 数据结构之交换排序之快速排序(参考整理严蔚敏数据结构)
- C#数据结构学习之一:数据结构基础知识
- 数据结构笔记-----数据结构与算法
- (摘)面向对象c++数据结构之基本数据结构-链表-使用链表管理器(连载中)
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)