2014年1月30日星期四

Java多线程系列--“JUC集合”07之 ArrayBlockingQueue - 如果天空不死

本邮件内容由第三方提供,如果您不想继续收到该邮件,可 点此退订
Java多线程系列--"JUC集合"07之 ArrayBlockingQueue - 如果天空不死  阅读原文»

概要

本章对Java.util.concurrent包中的ArrayBlockingQueue类进行详细的介绍。内容包括:
ArrayBlockingQueue介绍
ArrayBlockingQueue原理和数据结构
ArrayBlockingQueue函数列表
ArrayBlockingQueue源码分析(JDK1.7.0_40版本)
ArrayBlockingQueue示例

转载请注明出处:http://www.cnblogs.com/skywang12345/p/3498652.html

ArrayBlockingQueue介绍

ArrayBlockingQueue是数组实现的线程安全的有界的阻塞队列。
线程安全是指,ArrayBlockingQueue内部通过“互斥锁”保护竞争资源,实现了多线程对竞争资源的互斥访问。而有界,则是指ArrayBlockingQueue对应的数组是有界限的。 阻塞队列,是指多线程访问竞争资源时,当竞争资源已被某线程获取时,其它要获取该资源的线程需要阻塞等待;而且,ArrayBlockingQueue是按 FIFO(先进先出)原则对元素进行排序,元素都是从尾部插入到队列,从头部开始返回。

注意:ArrayBlockingQueue不同于ConcurrentLinkedQueue,ArrayBlockingQueue是数组实现的,并且是有界限的;而ConcurrentLinkedQueue是链表实现的,是无界限的。

ArrayBlockingQueue原理和数据结构

ArrayBlockingQueue的数据结构,如下图所示:

说明
1. ArrayBlockingQueue继承于AbstractQueue,并且它实现了BlockingQueue接口。
2. ArrayBlockingQueue内部是通过Object[]数组保存数据的,也就是说ArrayBlockingQueue本质上是通过数组实现的。ArrayBlockingQueue的大小,即数组的容量是创建ArrayBlockingQueue时指定的。
3. ArrayBlockingQueue与ReentrantLock是组合关系,ArrayBlockingQueue中包含一个ReentrantLock对象(lock)。ReentrantLock是可重入的互斥锁,ArrayBlockingQueue就是根据该互斥锁实现“多线程对竞争资源的互斥访问”。而且,ReentrantLock分为公平锁和非公平锁,关于具体使用公平锁还是非公平锁,在创建ArrayBlockingQueue时可以指定;而且,ArrayBlockingQueue默认会使用非公平锁。
4. ArrayBlockingQueue与Condition是组合关系,ArrayBlockingQueue中包含两个Condition对象(notEmpty和notFull)。而且,Condition又依赖于ArrayBlockingQueue而存在,通过Condition可以实现对ArrayBlockingQueue的更精确的访问 -- (01)若某线程(线程A)要取数据时,数组正好为空,则该线程会执行notEmpty.await()进行等待;当其它某个线程(线程B)向数组中插入了数据之后,会调用notEmpty.signal()唤醒“notEmpty上的等待线程”。此时,线程A会被唤醒从而得以继续运行。(02)若某线程(线程H)要插入数据时,数组已满,则该线程会它执行notFull.await()进行等待;当其它某个线程(线程I)取出数据之后,会调用notFull.signal()唤醒“notFull上的等待线程”。此时,线程H就会被唤醒从而得以继续运行。
关于ReentrantLock,公平锁,非公平锁,以及Condition等更多的内容,可以参考:

(01) Java多线程系列--“JUC锁”02之 互斥锁ReentrantLock
(02) Java多线程系列--“JUC锁”03之 公平锁(一)
(03) Java多线程系列--“JUC锁”04之 公平锁(二)
(04) Java多线程系列--“JUC锁”05之 非公平锁
(05) Java多线程系列--“JUC锁”06之 Condition条件

ArrayBlockingQueue函数列表

// 创建一个带有给定的(固定)容量和默认访问策略的 ArrayBlockingQueue。
ArrayBlockingQueue(int capacity)
// 创建一个具有给定的(固定)容量和指定访问策略的 ArrayBlockingQueue。
ArrayBlockingQueue(int capacity, boolean fair)
// 创建一个具有给定的(固定)容量和指定访问策略的 ArrayBlockingQueue,它最初包含给定 collection 的元素,并以 collection 迭代器的遍历顺序添加元素。
ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c)

// 将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量),在成功时返回 true,如果此队列已满,则抛出 IllegalStateException。
boolean add(E e)
// 自动移除此队列中的所有元素。
void clear()
// 如果此队列包含指定的元素,则返回 true。
boolean contains(Object o)
// 移除此队列中所有可用的元素,并将它们添加到给定 collection 中。
int drainTo(Collection<? super E> c)
// 最多从此队列中移除给定数量的可用元素,并将这些元素添加到给定 collection 中。
int drainTo(Collection<? super E> c, int maxElements)
// 返回在此队列中的元素上按适当顺序进行迭代的迭代器。
Iterator<E> iterator()
// 将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量),在成功时返回 true,如果此队列已满,则返回 false。
boolean offer(E e)
// 将指定的元素插入此队列的尾部,如果该队列已满,则在到达指定的等待时间之前等待可用的空间。
boolean offer(E e, long timeout, TimeUnit unit)
// 获取但不移除此队列的头;如果此队列为空,则返回 null。
E peek()
// 获取并移除此队列的头,如果此队列为空,则返回 null。
E poll()
// 获取并移除此队列的头部,在指定的等待时间前等待可用的元素(如果有必要)。
E poll(long timeout, TimeUnit unit)
// 将指定的元素插入此队列的尾部,如果该队列已满,则等待可用的空间。
void put(E e)
// 返回在无阻塞的理想情况下(不存在内存或资源约束)此队列能接受的其他元素数量。
int remainingCapacity()
// 从此队列中移除指定元素的单个实例(如果存在)。
boolean remove(Object
面试准备 - HashTable 的C#实现 开发地址法 - 听说读写  阅读原文»

Hashtable是很经常在面试中遇到的数据结构,因为他的O(1)操作时间和O(n)空间

之所以自己写一份是因为:

  • 加深对于hashtable的理解
  • 某些公司面试的时候需要coding.......

开发地址法 Xn=(Xn-1 +b ) % size

理论上b要和size是要精心选择的,不过我这边没有做特别的处理,101的默认size是从c#源代码中抄袭的。。。。

代码尽量简单一点是为了理解方便

hashtable快满的时候扩展一倍空间,数据和标志位还有key 这三个数组都要扩展

删除的时候不能直接删除元素,只能打一个标志(因为用了开放地方方法)

目前只支持string和int类型的key(按位131进制)


public class Hashtable<T>

{
public Hashtable()
{
this.dataArray = new T[this.m];
this.avaiableCapacity = this.m;
this.keyArray = new int[this.m];
for (int i = 0; i < this.keyArray.Length; i++)
{
this.keyArray = -1;
}
this.flagArray = new bool[this.m];
}

private int m = 101;

private int l = 1;

private int avaiableCapacity;

private double factor = 0.35;

private T[] dataArray;

private int[] keyArray;

private bool[] flagArray;

public void Add(string s, T item)
{
if (string.IsNullOrEmpty(s))
{
throw new ArgumentNullException("s");
}

if ((double)this.avaiableCapacity / this.m < this.factor)
{
this.ExtendCapacity();
}

var code = HashtableHelper.GetStringHash(s);
this.AddItem(code, item, this.dataArray, code, this.keyArray, this.flagArray);
}

public T Get(string s)
{
if (string.IsNullOrEmpty(s))
{
throw new ArgumentNullException("s");
}

var code = HashtableHelper.GetStringHash(s);
return this.GetItem(code, this.dataArray, code, this.keyArray, this.flagArray);
}

private void ExtendCapacity()
{
this.m *= 2;
this.avaiableCapacity += this.m;
T[] newItems
= new T[this.m];
int[] newKeys = new int[this.m];
bool[] newFlags = new bool[this.m];

for (int i = 0; i < newKeys.Length; i++)
{
newKeys
= -1;
}

for (int i = 0; i < this.dataArray.Length; i++)
{
if (this.keyArray >= 0 && !this.flagArray)
{
//var code = HashtableHelper.GetStringHash(s);
this.AddItem(
this.keyArray,
this.dataArray,
newItems,
this.keyArray,
newKeys,
this.flagArray);
}
}
this.dataArray = newItems;
this.keyArray = newKeys;
this.flagArray = newFlags;
// throw new NotImplementedException();
}

private int AddItem(int code, T item, T[] data, int hashCode, int[] keys, bool[] flags)
{
int address =

阅读更多内容

没有评论:

发表评论