使用队列实现信号量
Implementing a Semaphore with a Queue
我正在尝试使用队列创建基本的信号量实现。这个想法是,有一个数据库,有 10 个作者。写入者只能在互斥的情况下写入数据库。我正在使用队列,因为我想实现先进先出和后进先出。
使用信号量,我无法通知特定线程唤醒。所以我的想法是我正在为每个 Writer 创建一个对象并告诉 Writer 等待该对象。将该对象放入队列中。然后从队列中移除该对象并通知正在等待该对象的线程。这样,我想我可以做一个先进先出或后进先出的实现。
我需要有关实际代码实现的帮助:
1. 我 运行 下面的代码,它给了我很多 IllegalMonitorStateException。
2. FIFO 和 LIFO 代码(我的 FIFO 代码似乎不正确,而对于 LIFO 代码,我正在考虑使用 Stack 而不是 Queue)。
public class Test {
public static void main(String [] args) {
Database db = new Database();
for (int i = 0; i < 10; i++)
(new Thread(new Writer(db))).start();
}
}
public class Writer implements Runnable {
private Database database;
public Writer(Database database) {
this.database = database;
}
public void run() {
this.database.acquireWriteLock();
this.database.write();
this.database.releaseWriteLock();
}
}
public class Database {
private Semaphore lockQueue;
public Database() {
this.lockQueue = new Semaphore();
}
public void write() {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {}
}
public void acquireWriteLock() {
lockQueue.acquire();
}
public void releaseWriteLock() {
lockQueue.release();
}
}
import java.util.Queue;
import java.util.LinkedList;
public class Semaphore {
private Queue<Object> queue;
public Semaphore() {
this.queue = new LinkedList<Object>();
}
public synchronized void acquire() {
Object object = new Object();
try {
if (this.queue.size() > 0) {
object.wait();
this.queue.add(object);
}
} catch (InterruptedException ie) {}
this.queue.add(object);
}
public synchronized void release() {
Object object = this.queue.remove();
object.notify();
}
}
在使用wait()和notify()之前,您需要获取对象的锁。
尝试检查以下代码是否有效:
public class Semaphore {
private Queue<Object> queue;
private int state;
public Semaphore() {
this.queue = new LinkedList<Object>();
}
public void acquire() {
Object object = new Object();
synchronized (object) {
try {
if (this.state > 0) {
this.queue.add(object);
object.wait();
} else {
state++;
}
} catch (InterruptedException ie) {
}
}
}
public void release() {
Object object = this.queue.poll();
state--;
if(null == object) {
return;
}
synchronized (object) {
object.notify();
}
}
}
我正在尝试使用队列创建基本的信号量实现。这个想法是,有一个数据库,有 10 个作者。写入者只能在互斥的情况下写入数据库。我正在使用队列,因为我想实现先进先出和后进先出。
使用信号量,我无法通知特定线程唤醒。所以我的想法是我正在为每个 Writer 创建一个对象并告诉 Writer 等待该对象。将该对象放入队列中。然后从队列中移除该对象并通知正在等待该对象的线程。这样,我想我可以做一个先进先出或后进先出的实现。
我需要有关实际代码实现的帮助: 1. 我 运行 下面的代码,它给了我很多 IllegalMonitorStateException。 2. FIFO 和 LIFO 代码(我的 FIFO 代码似乎不正确,而对于 LIFO 代码,我正在考虑使用 Stack 而不是 Queue)。
public class Test {
public static void main(String [] args) {
Database db = new Database();
for (int i = 0; i < 10; i++)
(new Thread(new Writer(db))).start();
}
}
public class Writer implements Runnable {
private Database database;
public Writer(Database database) {
this.database = database;
}
public void run() {
this.database.acquireWriteLock();
this.database.write();
this.database.releaseWriteLock();
}
}
public class Database {
private Semaphore lockQueue;
public Database() {
this.lockQueue = new Semaphore();
}
public void write() {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {}
}
public void acquireWriteLock() {
lockQueue.acquire();
}
public void releaseWriteLock() {
lockQueue.release();
}
}
import java.util.Queue;
import java.util.LinkedList;
public class Semaphore {
private Queue<Object> queue;
public Semaphore() {
this.queue = new LinkedList<Object>();
}
public synchronized void acquire() {
Object object = new Object();
try {
if (this.queue.size() > 0) {
object.wait();
this.queue.add(object);
}
} catch (InterruptedException ie) {}
this.queue.add(object);
}
public synchronized void release() {
Object object = this.queue.remove();
object.notify();
}
}
在使用wait()和notify()之前,您需要获取对象的锁。 尝试检查以下代码是否有效:
public class Semaphore {
private Queue<Object> queue;
private int state;
public Semaphore() {
this.queue = new LinkedList<Object>();
}
public void acquire() {
Object object = new Object();
synchronized (object) {
try {
if (this.state > 0) {
this.queue.add(object);
object.wait();
} else {
state++;
}
} catch (InterruptedException ie) {
}
}
}
public void release() {
Object object = this.queue.poll();
state--;
if(null == object) {
return;
}
synchronized (object) {
object.notify();
}
}
}