Skip to content

Instantly share code, notes, and snippets.

@melin
Created October 4, 2014 02:32
Show Gist options
  • Save melin/0aac3b62c7d857b5f5be to your computer and use it in GitHub Desktop.
Save melin/0aac3b62c7d857b5f5be to your computer and use it in GitHub Desktop.
基于信号量、condition、object wait notify三种方式的实现的简易队列(demo)
public class ArrayQueueSemaphore implements Queue {
private final Semaphore notFull = new Semaphore(10);
private final Semaphore notEmpty = new Semaphore(0);
private final ReentrantLock lock = new ReentrantLock();
private final String[] connections = new String[10];
int putIndex, takeIndex, count;
public void put(String e) throws InterruptedException {
notFull.acquire();
lock.lockInterruptibly();
try {
connections[putIndex] = e;
if(++putIndex == connections.length)
putIndex = 0;
count++;
} finally {
notEmpty.release();
lock.unlock();
}
}
public String take() throws InterruptedException {
notEmpty.acquire();
lock.lockInterruptibly();
try {
String re = connections[takeIndex];
if(++takeIndex == connections.length)
takeIndex = 0;
count--;
return re;
} finally {
notFull.release();
lock.unlock();
}
}
public int size() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
}
}
public class ArrayQueueCondition implements Queue {
private final ReentrantLock lock = new ReentrantLock();
private final Condition notFull = lock.newCondition();
private final Condition notEmpty = lock.newCondition();
private final String[] connections = new String[10];
int putIndex, takeIndex, count;
public void put(String e) throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
if(count == connections.length)
notFull.await();
connections[putIndex] = e;
if(++putIndex == connections.length)
putIndex = 0;
count++;
notEmpty.signal();
} finally {
lock.unlock();
}
}
public String take() throws InterruptedException {
lock.lock();
try {
if(count == 0)
notEmpty.await();
String re = connections[takeIndex];
if(++takeIndex == connections.length)
takeIndex = 0;
count--;
notFull.signal();
return re;
} finally {
lock.unlock();
}
}
public int size() {
final ReentrantLock lock = this.lock;
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
}
}
public class ArrayQueueObject implements Queue {
private final Object notFull = new Object();
private final Object notEmpty = new Object();
private int length = 10;
private final String[] connections = new String[length];
int putIndex, takeIndex, count;
public void put(String e) throws InterruptedException {
synchronized (notEmpty) {
if (count == 0) {
notEmpty.notify();
}
synchronized (notFull) {
if(count == length)
notFull.wait();
connections[putIndex] = e;
if (++putIndex == length)
putIndex = 0;
count++;
}
}
}
public String take() throws InterruptedException {
synchronized (notEmpty) {
if (count == 0) {
notEmpty.wait();
}
synchronized (notFull) {
if(count == length)
notFull.notify();
String re = connections[takeIndex];
if (++takeIndex == connections.length)
takeIndex = 0;
count--;
return re;
}
}
}
public int size() {
synchronized (notEmpty) {
synchronized (notFull) {
return count;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment