- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我问的是经典的读者-作者问题:wiki link
我最近在研究这个,我写了一个只使用一个锁的解决方案:
class ReadWrite {
private Object lock = new Object();
private boolean isWriting = false;
private int readerCount = 0;
public void read() throws InterruptedException {
synchronized(lock) {
while (isWriting) {
lock.wait();
}
readerCount++;
}
doRead(); // takes some time to read it...
synchronized(lock) {
readerCount--;
lock.notifyAll();
}
}
public void write(int value) throws InterruptedException {
synchronized(lock) {
while (readerCount > 0 || isWriting) {
lock.wait();
}
isWriting = true;
}
doWrite(value); // takes some time to write it
synchronized(lock) {
isWriting = false;
lock.notifyAll();
}
}
private void doRead() { // omitted }
private void doWrite(int value) { // omitted }
}
.. 然后我会创建一堆线程,每个线程重复调用 read() 或 write()。
这解决了有利于读者的第一个读者/作者问题。
...然后我在网上看到所有的引用资料/书籍总是使用两个/多个锁;所以我不确定我是否遗漏了什么
我用多线程配置测试了它,它实际上工作正常,如下所示:1)同时多次读取2) 在没有阅读的情况下,任何时候只有一个作家在写作
我解决了它直到 3rd 读者/作者问题(读者/作者之间的公平性),只是稍微增加了上面的解决方案,你可以在下面看到/* edited */)
除了可能的效率差异之外,您是否看到任何错误的算法 w/为读者/作者问题使用单个锁(至少对于有利于读者的第一个读者/作者问题)?
编辑:
有人讨论我是否可以通过修改原始文件来解决第三个读者/作者问题...并将其限制在 2000 毫秒(100 毫秒读/写 * 20 个线程)以下,所以在这里;您可以取消注释所有系统输出以查看独占写入是否仍然发生以及同时读取。我还在下面复制粘贴了时间运行输出。
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.IntStream;
class ReadWrite {
private Object lock = new Object();
private boolean isWriting = false;
private int readerCount = 0;
Queue<Thread> order = new LinkedList<Thread>();
public void read(Reader r) throws InterruptedException {
long start = System.currentTimeMillis();
synchronized (lock) {
while (isWriting || (!order.isEmpty() && order.peek() != r)) {
if (!order.contains(r)) {
order.add(r);
}
lock.wait();
}
order.remove(r);
readerCount++;
lock.notifyAll();
// System.out.println("Reader " + r.id + " started reading; readerCount: " + readerCount);
}
doRead(r.id); // takes however many seconds to read it...
synchronized (lock) {
readerCount--;
// System.out.println("Reader " + r.id + " finished reading; readerCount: " + readerCount);
lock.notifyAll();
}
long delta = System.currentTimeMillis() - start;
System.out.println("Reader " + r.id + " delta: " + delta + "");
}
public void write(Writer w, int value) throws InterruptedException {
// System.out.println("Writer " + w.id + " entered write");
long start = System.currentTimeMillis();
synchronized (lock) {
while (readerCount > 0 || isWriting || (!order.isEmpty() && order.peek() != w)) {
if (!order.contains(w)) {
order.add(w);
}
lock.wait();
}
order.remove(w);
isWriting = true;
// System.out.println("Writer " + w.id + " started writing-----------");
}
doWrite(w.id, value);
synchronized (lock) {
isWriting = false;
// System.out.println("Writer " + w.id + " finished writing----------");
lock.notifyAll();
}
long delta = System.currentTimeMillis() - start;
System.out.println("Writer " + w.id + " delta: " + delta);
if (delta - 2000 > 10) {
System.out.println("!!!attention above!!!");
}
}
private void doRead(int readerId) {
LockSupport.parkNanos(100_000_000L);
}
private void doWrite(int writerId, int value) {
LockSupport.parkNanos(100_000_000L);
}
}
class Reader extends Thread{
public int id = 0;
private ReadWrite rw = null;
public Reader(int id, ReadWrite rw) {
this.id = id;
this.rw = rw;
}
@Override
public void run() {
while (true) {
try {
rw.read(this);
} catch (InterruptedException e) {
}
}
}
}
class Writer extends Thread {
public int id = 0;
private ReadWrite rw = null;
public Writer(int id, ReadWrite rw) {
this.id = id;
this.rw = rw;
}
@Override
public void run() {
while (true) {
try {
rw.write(this, 0);
} catch (InterruptedException e) {
}
}
}
}
public class RW {
public static void main(String[] args) throws InterruptedException {
ReadWrite rw = new ReadWrite();
for (int i : IntStream.range(0, 10).toArray()) {
new Reader(i, rw).start();
}
for (int i : IntStream.range(0, 10).toArray()) {
new Writer(i, rw).start();
}
}
}
扩展运行的输出:
Reader 7 delta: 100
Reader 8 delta: 100
Reader 9 delta: 100
Reader 3 delta: 101
Reader 2 delta: 100
Reader 5 delta: 100
Reader 1 delta: 101
Reader 0 delta: 101
Reader 6 delta: 100
Reader 4 delta: 100
Writer 0 delta: 200
Writer 1 delta: 302
Writer 3 delta: 402
Writer 5 delta: 502
Writer 2 delta: 603
Writer 4 delta: 704
Writer 6 delta: 805
Writer 7 delta: 906
Writer 8 delta: 1006
Writer 9 delta: 1106
Reader 8 delta: 1106
Reader 9 delta: 1106
Reader 7 delta: 1106
Reader 5 delta: 1105
Reader 6 delta: 1105
Reader 3 delta: 1106
Reader 2 delta: 1105
Reader 1 delta: 1105
Reader 0 delta: 1105
Reader 4 delta: 1105
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1106
Writer 5 delta: 1106
Writer 2 delta: 1105
Writer 4 delta: 1105
Writer 6 delta: 1104
Writer 7 delta: 1104
Writer 8 delta: 1104
Writer 9 delta: 1105
Reader 8 delta: 1105
Reader 7 delta: 1106
Reader 9 delta: 1106
Reader 3 delta: 1107
Reader 5 delta: 1107
Reader 6 delta: 1107
Reader 2 delta: 1107
Reader 4 delta: 1107
Reader 1 delta: 1108
Reader 0 delta: 1108
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1108
Writer 4 delta: 1109
Writer 6 delta: 1109
Writer 7 delta: 1109
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 8 delta: 1110
Reader 9 delta: 1109
Reader 7 delta: 1110
Reader 3 delta: 1110
Reader 5 delta: 1111
Reader 4 delta: 1110
Reader 2 delta: 1110
Reader 6 delta: 1110
Reader 0 delta: 1110
Reader 1 delta: 1111
Writer 0 delta: 1111
Writer 1 delta: 1112
Writer 3 delta: 1111
Writer 5 delta: 1112
Writer 2 delta: 1113
Writer 4 delta: 1111
Writer 6 delta: 1112
Writer 7 delta: 1111
Writer 8 delta: 1112
Writer 9 delta: 1112
Reader 8 delta: 1111
Reader 9 delta: 1111
Reader 7 delta: 1111
Reader 5 delta: 1110
Reader 3 delta: 1111
Reader 6 delta: 1110
Reader 4 delta: 1110
Reader 2 delta: 1110
Reader 1 delta: 1110
Reader 0 delta: 1110
Writer 0 delta: 1109
Writer 1 delta: 1109
Writer 3 delta: 1109
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1109
Writer 7 delta: 1109
Writer 8 delta: 1108
Writer 9 delta: 1107
Reader 3 delta: 1105
Reader 7 delta: 1106
Reader 4 delta: 1105
Reader 6 delta: 1105
Reader 8 delta: 1107
Reader 9 delta: 1106
Reader 2 delta: 1105
Reader 1 delta: 1105
Reader 0 delta: 1105
Reader 5 delta: 1107
Writer 0 delta: 1106
Writer 1 delta: 1105
Writer 3 delta: 1106
Writer 5 delta: 1105
Writer 2 delta: 1104
Writer 4 delta: 1103
Writer 6 delta: 1103
Writer 7 delta: 1103
Writer 8 delta: 1103
Writer 9 delta: 1104
Reader 3 delta: 1104
Reader 7 delta: 1105
Reader 8 delta: 1106
Reader 1 delta: 1105
Reader 4 delta: 1106
Reader 6 delta: 1106
Reader 0 delta: 1105
Reader 2 delta: 1105
Reader 9 delta: 1106
Reader 5 delta: 1105
Writer 0 delta: 1105
Writer 1 delta: 1105
Writer 3 delta: 1104
Writer 5 delta: 1105
Writer 2 delta: 1106
Writer 4 delta: 1107
Writer 6 delta: 1109
Writer 7 delta: 1109
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 7 delta: 1110
Reader 3 delta: 1111
Reader 8 delta: 1110
Reader 1 delta: 1111
Reader 4 delta: 1111
Reader 6 delta: 1112
Reader 0 delta: 1112
Reader 5 delta: 1112
Reader 2 delta: 1112
Reader 9 delta: 1112
Writer 0 delta: 1113
Writer 1 delta: 1113
Writer 3 delta: 1113
Writer 5 delta: 1113
Writer 2 delta: 1113
Writer 4 delta: 1112
Writer 6 delta: 1110
Writer 7 delta: 1111
Writer 8 delta: 1110
Writer 9 delta: 1111
Reader 7 delta: 1110
Reader 3 delta: 1111
Reader 8 delta: 1110
Reader 1 delta: 1110
Reader 4 delta: 1110
Reader 6 delta: 1109
Reader 0 delta: 1110
Reader 5 delta: 1109
Reader 9 delta: 1109
Reader 2 delta: 1109
Writer 0 delta: 1109
Writer 1 delta: 1111
Writer 3 delta: 1112
Writer 5 delta: 1111
Writer 2 delta: 1111
Writer 4 delta: 1111
Writer 6 delta: 1112
Writer 7 delta: 1112
Writer 8 delta: 1112
Writer 9 delta: 1111
Reader 7 delta: 1111
Reader 3 delta: 1110
Reader 8 delta: 1111
Reader 4 delta: 1112
Reader 1 delta: 1112
Reader 5 delta: 1112
Reader 6 delta: 1113
Reader 0 delta: 1112
Reader 2 delta: 1113
Reader 9 delta: 1113
Writer 0 delta: 1112
Writer 1 delta: 1111
Writer 3 delta: 1110
Writer 5 delta: 1110
Writer 2 delta: 1109
Writer 4 delta: 1109
Writer 6 delta: 1108
Writer 7 delta: 1108
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 7 delta: 1109
Reader 3 delta: 1109
Reader 1 delta: 1107
Reader 4 delta: 1107
Reader 8 delta: 1109
Reader 9 delta: 1106
Reader 0 delta: 1107
Reader 2 delta: 1106
Reader 6 delta: 1107
Reader 5 delta: 1107
Writer 0 delta: 1107
Writer 1 delta: 1106
Writer 3 delta: 1107
Writer 5 delta: 1109
Writer 2 delta: 1110
Writer 4 delta: 1110
Writer 6 delta: 1111
Writer 7 delta: 1110
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 7 delta: 1109
Reader 1 delta: 1109
Reader 3 delta: 1110
Reader 4 delta: 1109
Reader 8 delta: 1110
Reader 0 delta: 1109
Reader 9 delta: 1109
Reader 6 delta: 1109
Reader 2 delta: 1109
Reader 5 delta: 1109
Writer 0 delta: 1109
Writer 1 delta: 1110
Writer 3 delta: 1110
Writer 5 delta: 1109
Writer 2 delta: 1108
Writer 4 delta: 1109
Writer 6 delta: 1108
Writer 7 delta: 1109
Writer 8 delta: 1111
Writer 9 delta: 1111
Reader 1 delta: 1111
Reader 7 delta: 1112
Reader 3 delta: 1111
Reader 4 delta: 1111
Reader 8 delta: 1111
Reader 9 delta: 1111
Reader 6 delta: 1111
Reader 0 delta: 1111
Reader 2 delta: 1112
Reader 5 delta: 1112
Writer 0 delta: 1111
Writer 1 delta: 1111
Writer 3 delta: 1110
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1110
Writer 8 delta: 1108
Writer 9 delta: 1107
Reader 1 delta: 1107
Reader 7 delta: 1108
Reader 3 delta: 1108
Reader 4 delta: 1109
Reader 8 delta: 1108
Reader 0 delta: 1109
Reader 6 delta: 1109
Reader 9 delta: 1109
Reader 2 delta: 1109
Reader 5 delta: 1109
Writer 0 delta: 1108
Writer 1 delta: 1108
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1109
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1107
Writer 8 delta: 1107
Writer 9 delta: 1108
Reader 1 delta: 1108
Reader 4 delta: 1107
Reader 7 delta: 1108
Reader 3 delta: 1108
Reader 8 delta: 1108
Reader 6 delta: 1107
Reader 0 delta: 1107
Reader 5 delta: 1107
Reader 9 delta: 1108
Reader 2 delta: 1107
Writer 0 delta: 1107
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1109
Writer 6 delta: 1110
Writer 7 delta: 1109
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 1 delta: 1109
Reader 3 delta: 1109
Reader 4 delta: 1109
Reader 7 delta: 1109
Reader 0 delta: 1109
Reader 5 delta: 1108
Reader 8 delta: 1109
Reader 6 delta: 1109
Reader 9 delta: 1109
Reader 2 delta: 1109
Writer 0 delta: 1110
Writer 1 delta: 1111
Writer 3 delta: 1111
Writer 5 delta: 1110
Writer 2 delta: 1110
Writer 4 delta: 1111
Writer 6 delta: 1111
Writer 7 delta: 1112
Writer 8 delta: 1111
Writer 9 delta: 1110
Reader 1 delta: 1111
Reader 3 delta: 1110
Reader 4 delta: 1111
Reader 0 delta: 1111
Reader 5 delta: 1111
Reader 7 delta: 1112
Reader 8 delta: 1112
Reader 6 delta: 1112
Reader 2 delta: 1112
Reader 9 delta: 1112
Writer 0 delta: 1111
Writer 1 delta: 1110
Writer 3 delta: 1109
Writer 5 delta: 1110
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1109
Writer 8 delta: 1110
Writer 9 delta: 1111
Reader 1 delta: 1111
Reader 3 delta: 1112
Reader 4 delta: 1111
Reader 5 delta: 1110
Reader 0 delta: 1110
Reader 7 delta: 1112
Reader 8 delta: 1111
Reader 6 delta: 1111
Reader 9 delta: 1110
Reader 2 delta: 1110
Writer 0 delta: 1111
Writer 1 delta: 1111
Writer 3 delta: 1112
Writer 5 delta: 1112
Writer 2 delta: 1112
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1111
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 1 delta: 1109
Reader 3 delta: 1109
Reader 5 delta: 1109
Reader 4 delta: 1109
Reader 7 delta: 1108
Reader 8 delta: 1108
Reader 0 delta: 1110
Reader 6 delta: 1108
Reader 9 delta: 1108
Reader 2 delta: 1108
Writer 0 delta: 1107
Writer 1 delta: 1108
Writer 3 delta: 1107
Writer 5 delta: 1107
Writer 2 delta: 1107
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1106
Writer 8 delta: 1106
Writer 9 delta: 1105
Reader 1 delta: 1105
Reader 3 delta: 1105
Reader 5 delta: 1106
Reader 4 delta: 1106
Reader 7 delta: 1105
Reader 0 delta: 1106
Reader 8 delta: 1106
Reader 6 delta: 1106
Reader 9 delta: 1106
Reader 2 delta: 1106
Writer 0 delta: 1107
Writer 1 delta: 1106
Writer 3 delta: 1106
Writer 5 delta: 1106
Writer 2 delta: 1107
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1107
Writer 8 delta: 1108
Writer 9 delta: 1108
Reader 1 delta: 1109
Reader 3 delta: 1108
Reader 5 delta: 1108
Reader 7 delta: 1109
Reader 4 delta: 1109
Reader 8 delta: 1108
Reader 0 delta: 1108
Reader 9 delta: 1109
Reader 2 delta: 1109
Reader 6 delta: 1109
Writer 0 delta: 1108
Writer 1 delta: 1108
Writer 3 delta: 1109
Writer 5 delta: 1108
Writer 2 delta: 1108
Writer 4 delta: 1108
Writer 6 delta: 1107
Writer 7 delta: 1108
Writer 8 delta: 1107
Writer 9 delta: 1107
Reader 1 delta: 1106
Reader 3 delta: 1107
Reader 5 delta: 1107
Reader 7 delta: 1106
Reader 0 delta: 1108
Reader 8 delta: 1108
Reader 4 delta: 1108
Reader 9 delta: 1108
Reader 2 delta: 1108
Reader 6 delta: 1108
Writer 0 delta: 1109
Writer 1 delta: 1111
Writer 3 delta: 1111
Writer 5 delta: 1111
Writer 2 delta: 1110
Writer 4 delta: 1110
Writer 6 delta: 1111
Writer 7 delta: 1111
Writer 8 delta: 1112
Writer 9 delta: 1112
Reader 1 delta: 1113
Reader 3 delta: 1112
Reader 5 delta: 1112
Reader 0 delta: 1110
Reader 7 delta: 1112
Reader 8 delta: 1111
Reader 9 delta: 1110
Reader 4 delta: 1111
Reader 6 delta: 1110
Reader 2 delta: 1110
Writer 0 delta: 1110
Writer 1 delta: 1108
Writer 3 delta: 1107
Writer 5 delta: 1108
Writer 2 delta: 1108
Writer 4 delta: 1108
Writer 6 delta: 1108
Writer 7 delta: 1107
Writer 8 delta: 1106
Writer 9 delta: 1107
Reader 1 delta: 1106
Reader 3 delta: 1106
Reader 5 delta: 1105
Reader 0 delta: 1106
Reader 7 delta: 1106
Reader 8 delta: 1106
Reader 4 delta: 1106
Reader 9 delta: 1106
Reader 6 delta: 1106
Reader 2 delta: 1107
Writer 0 delta: 1106
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1109
Writer 7 delta: 1110
Writer 8 delta: 1111
Writer 9 delta: 1110
Reader 3 delta: 1110
Reader 5 delta: 1110
Reader 1 delta: 1110
Reader 7 delta: 1110
Reader 0 delta: 1110
Reader 4 delta: 1110
Reader 8 delta: 1110
Reader 9 delta: 1110
Reader 2 delta: 1110
Reader 6 delta: 1111
Writer 0 delta: 1110
Writer 1 delta: 1110
Writer 3 delta: 1109
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1110
Writer 7 delta: 1110
Writer 8 delta: 1110
Writer 9 delta: 1110
Reader 3 delta: 1111
Reader 0 delta: 1111
Reader 5 delta: 1112
Reader 7 delta: 1111
Reader 1 delta: 1112
Reader 9 delta: 1110
Reader 8 delta: 1110
Reader 4 delta: 1110
Reader 2 delta: 1110
Reader 6 delta: 1110
Writer 0 delta: 1110
Writer 1 delta: 1110
Writer 3 delta: 1110
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1107
Writer 6 delta: 1108
Writer 7 delta: 1107
Writer 8 delta: 1107
Writer 9 delta: 1108
Reader 3 delta: 1107
Reader 1 delta: 1107
Reader 9 delta: 1107
Reader 0 delta: 1107
Reader 7 delta: 1107
Reader 5 delta: 1107
Reader 6 delta: 1107
Reader 2 delta: 1107
Reader 4 delta: 1108
Reader 8 delta: 1108
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1108
Writer 2 delta: 1107
Writer 4 delta: 1108
Writer 6 delta: 1107
Writer 7 delta: 1109
Writer 8 delta: 1109
Writer 9 delta: 1108
Reader 3 delta: 1108
Reader 1 delta: 1108
Reader 9 delta: 1108
Reader 7 delta: 1109
Reader 0 delta: 1109
Reader 5 delta: 1109
Reader 8 delta: 1109
Reader 2 delta: 1109
Reader 6 delta: 1109
Reader 4 delta: 1109
Writer 0 delta: 1108
Writer 1 delta: 1110
Writer 3 delta: 1109
Writer 5 delta: 1109
Writer 2 delta: 1109
Writer 4 delta: 1108
Writer 6 delta: 1108
Writer 7 delta: 1106
Writer 8 delta: 1107
Writer 9 delta: 1107
Reader 1 delta: 1107
Reader 3 delta: 1108
Reader 9 delta: 1107
Reader 0 delta: 1107
Reader 5 delta: 1107
Reader 7 delta: 1107
Reader 8 delta: 1107
Reader 6 delta: 1107
Reader 2 delta: 1107
Reader 4 delta: 1107
Writer 0 delta: 1108
Writer 1 delta: 1107
Writer 3 delta: 1107
Writer 5 delta: 1108
Writer 2 delta: 1109
Writer 4 delta: 1110
Writer 6 delta: 1111
Writer 7 delta: 1112
Writer 8 delta: 1110
Writer 9 delta: 1111
Reader 3 delta: 1111
Reader 1 delta: 1111
Reader 5 delta: 1111
Reader 9 delta: 1112
Reader 0 delta: 1111
Reader 7 delta: 1112
Reader 6 delta: 1112
Reader 2 delta: 1112
Reader 4 delta: 1112
Reader 8 delta: 1112
Writer 0 delta: 1111
Writer 1 delta: 1111
Writer 3 delta: 1113
Writer 5 delta: 1112
Writer 2 delta: 1112
Writer 4 delta: 1111
Writer 6 delta: 1110
Writer 7 delta: 1109
Writer 8 delta: 1109
Writer 9 delta: 1109
Reader 3 delta: 1108
Reader 1 delta: 1108
Reader 9 delta: 1108
Reader 0 delta: 1108
Reader 5 delta: 1108
Reader 2 delta: 1108
Reader 7 delta: 1109
Reader 4 delta: 1108
Reader 6 delta: 1108
Reader 8 delta: 1108
Writer 0 delta: 1110
Writer 1 delta: 1109
Writer 3 delta: 1107
Writer 5 delta: 1107
Writer 2 delta: 1107
Writer 4 delta: 1107
Writer 6 delta: 1107
Writer 7 delta: 1108
Writer 8 delta: 1108
Writer 9 delta: 1108
Reader 3 delta: 1109
Reader 9 delta: 1109
Reader 1 delta: 1110
Reader 7 delta: 1108
Reader 4 delta: 1108
Reader 2 delta: 1108
Reader 0 delta: 1110
Reader 8 delta: 1108
Reader 5 delta: 1110
Reader 6 delta: 1108
Writer 0 delta: 1106
Writer 1 delta: 1106
Writer 3 delta: 1106
Writer 5 delta: 1107
Writer 2 delta: 1106
Writer 4 delta: 1106
Writer 6 delta: 1106
Writer 7 delta: 1106
Writer 8 delta: 1106
Writer 9 delta: 1106
Reader 3 delta: 1105
Reader 9 delta: 1105
Reader 1 delta: 1105
Reader 4 delta: 1105
Reader 7 delta: 1105
Reader 2 delta: 1105
Reader 8 delta: 1105
Reader 0 delta: 1105
Reader 5 delta: 1105
Reader 6 delta: 1105
Writer 0 delta: 1106
Writer 1 delta: 1107
Writer 3 delta: 1108
Writer 5 delta: 1107
Writer 2 delta: 1108
因此它会限制在最大预期时间范围内;
最佳答案
似乎您的代码仍然没有解决第三个问题,并且优先考虑读者而不是作者。正如维基所说:
Therefore, the third readers-writers problem is sometimes proposed, which adds the constraint that no thread shall be allowed to starve; that is, the operation of obtaining a lock on the shared data will always terminate in a bounded amount of time.
让我们创建 10 个读者和 10 个作者线程:
public static void main(String[] args) throws InterruptedException {
ReadWrite rw = new ReadWrite();
for(int i : IntStream.range(0, 10).toArray()) {
new Thread(() -> {
while(true) {
try {
rw.read(i);
} catch (InterruptedException e) {}
}
}, "Reader #"+i).start();
}
for(int i : IntStream.range(0, 10).toArray()) {
new Thread(() -> {
while(true) {
try {
rw.write(i, i);
} catch (InterruptedException e) {}
}
}, "Writer #"+i).start();
}
}
让 doRead
和 doWrite
只 hibernate 100 毫秒:
private void doRead(int readerId) {
LockSupport.parkNanos(100_000_000L);
}
private void doWrite(int writerId, int value) {
LockSupport.parkNanos(100_000_000L);
}
让我们打印执行写入所需的时间:
public void write(int writerId, int value) throws InterruptedException {
long start = System.currentTimeMillis();
... all your code ...
long delta = System.currentTimeMillis()-start;
System.out.println("Write delta: "+delta);
}
我删除了其他打印件。现在我看到以下输出:
Write delta: 200
Write delta: 200
Write delta: 600
Write delta: 800
Write delta: 1000
Write delta: 1099
Write delta: 1300
Write delta: 1500
Write delta: 600
Write delta: 1899
...
Write delta: 2700
Write delta: 4399
Write delta: 2100
Write delta: 1900
...
Write delta: 2800
Write delta: 3400
Write delta: 6999
Write delta: 900
Write delta: 1300
Write delta: 2500
...
Write delta: 1800
Write delta: 2800
Write delta: 7802
Write delta: 2600
...
我预计最大增量大约为 2000 毫秒:我们有 20 个并发线程,每个线程运行大约需要 100 毫秒,因此我们最多应该等待所有其他线程完成当前操作。但是我们不应该多次等待其他线程。实际上,当我用标准 JDK ReentrantReadWriteLock
替换您的实现时,我观察到最大增量 2000-2100。在您的情况下,延迟时间可能会长几倍,而且似乎没有保证最大值(从长远来看,我也观察到更高的延迟时间)。因此我不会说您的代码解决了第三个问题。
关于java - 经典的读者-作者算法——使用单个锁有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32901672/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!