问题描述
一般信息:三个读取器线程以块的形式从文件中随机读取,其中每个块都有一个 id,并且它们写入普通的 arraylist.只要将具有所需 id 的块添加到列表中,写入器线程就会写入输出文件.
general information: three reader-threads read randomly from a file in chunks where each chunk has an id and they write to a normal arraylist. a writer-thread writes to an outputfile as soon as a chunk with the needed id is added to the list.
出于这个原因,我编写了一个 blockingchunklist,它应该同步 add() 和 getnextchunk() 方法.
for that reason i have written a blockingchunklist which should synchronize the add() and getnextchunk() methods.
在一种情况下它适用于同步 通知 notifyall 和另一种情况下的同步列表.
it works for me with synchronized notify notifyall in one case and with a synchronized list in another.
当我使用 reentrantlock 和 condition 时,我无法做到这一点.writer-thread 只写了四个块,然后就卡住了.
i don't manage to do it when i use reentrantlock and condition. the writer-thread only writes four chunks and then he gets stuck.
为什么它可能不起作用:我怀疑一旦读者完成,作者就无法取回锁定.但是我希望每次有东西要写(available = true)时,都应该调用写线程.当 available 为 true 时,他们似乎会忽略 hasaccess.await().
why it might not work: i have the suspicion that once the readers are done the writer doesn't get the lock back. however i would expect that everytime when there is something to write (available=true) then the writer thread should be called. they seem to ignore hasaccess.await() when available is true.
应该如何工作:读取线程仅调用 add 方法,并且仅在有要写入的内容(可用)时才释放写入线程.当 available=true 时,它??们也会阻止自己.当作者通过调用 hasaccess.signalall() 写了一些东西时,这个锁就会被释放写入线程只调用 getnextchunk() 方法,并在写入块时释放其他线程.当 available=false 时他会屏蔽自己,并被读者释放.
how it should work: the reading threads only call the add method and they release the writing thread only when there is something to write (available). they also block themselves when available=true. this lock is released when the writer has written something by calling hasaccess.signalall() the writing thread only calls the getnextchunk() method and he releases the other threads when he wrote the chunk. he blocks himself when available=false and he is released by the readers.
问题:读取线程完成它们的工作,写入线程只写入前 4 个块.我希望编写器总是在 available=true 时被调用.
question: the reading threads finish their work and the writing thread only writes the first 4 chunks. i expect that the writer is always called when available=true.
我不需要一个确切的百家乐凯发k8的解决方案,因为我认为我遗漏了一些东西,所以也很感谢你的提示.那么:我错过了什么?
i don't need an exact solution, a hint is appreciated as well since i think i am missing something. so: what am i missing ?
谢谢
并发仅在发布的类中处理.主要方法仅启动踏板.编辑 2:这是我第一次尝试并发.我知道 arraylist 不是线程安全的.我想通过使用 reentrantlock 和 condition 来理解这些概念.basic 的想法是阻止读取器或写入器,无论可用是真还是假.
concurrency is handeled only in the posted class. the main-method only starts the treads. edit 2: this is one of my first shots at concurrency. i know that arraylist is not thread safe. i would like to make it so by using reentrantlock and condition in order to understand the concepts. the basic idea is to block either reader or writer whether available is true or false.
import java.util.arraylist; import java.util.concurrent.locks.condition; import java.util.concurrent.locks.lock; import java.util.concurrent.locks.reentrantlock; public class blockingchunkqueue { private final arraylistchunks; private volatile int currentid = 0; // first id for which the writer needs to wait private volatile boolean available; // true if the id is in the list private final lock listlock; private final condition hasaccess; public blockingchunkqueue(){ chunks = new arraylist<>(); listlock = new reentrantlock(); hasaccess = listlock.newcondition(); available = false; } /* * while there is a chunk which might be written to the output (=correct id) then wait for access. * then add the chunk and if the added chunk has the id for which the other thread is waiting, then, * then available = true and signal the waiting thread. * * */ public void add(chunk chunk) throws interruptedexception{ listlock.lock(); try{ while(available){ hasaccess.await(); // reader block yourself until you get the lock back } chunks.add(chunk); if(chunk.getid() == currentid){ available = true; hasaccess.signalall(); } }finally{ listlock.unlock(); } } /* * if the chunk is not available then wait. * if it becomes available then increment the id, remove it from the list, signal the reader-threads and set available false. * return the chunk which will be written to the output. */ public chunk getnextchunk() throws interruptedexception { listlock.lock(); try{ while(!available){ hasaccess.await(); // block yourself until u can write something } for(chunk c : chunks){ if(c.getid() == currentid){ currentid ; chunks.remove(c); hasaccess.signalall(); //signal the readers available = false; return c; } } }finally{ listlock.unlock(); } return null; } public int getcurrentid(){ return currentid;} public boolean isempty(){ return chunks.isempty(); } public int size(){return chunks.size();} }
百家乐凯发k8的解决方案:处理线程没有任何问题.事实证明这是我这边的一个逻辑错误.写入线程卡住了,因为他没有机会检查必要的 id,因为写入者随机读取块.感谢您提供有用的答案.
solution: there was nothing wrong with handling the threads. it turned out to be a logical error from my side. the writing thread gets stuck because he doesn't get the chance to check for the necessary ids because the writers read the chunks randomly. thanks for the helpfull answer.
推荐答案
这里有几个问题.
volatile 变量 available 仅在 lock 被持有时被读取或改变的目的是什么?
what is the purpose of the volatile variable available which is only read or mutated while the lock is held?
isemtpy 和 size 方法调用 chunks 上的方法,而不持有 lock.arraylist 不是线程安全的.这些调用的行为无法预测.
the isemtpy and size methods call methods on chunks without holding the lock. arraylist is not thread-safe. the behavior of these calls cannot be predicted.
您可能会卡住的一个原因是如果在调用 getnextchunk 之前添加了多个块.
a reason you might get stuck is if multiple chunks get added before getnextchunk is called.
在您的循环中查找您设置为 false 的当前",但它实际上可能已经在列表中:
in your loop to find the "current" you set available to false, but it may actually already be in the list:
for(chunk c : chunks){ if(c.getid() == currentid){ currentid ; chunks.remove(c); hasaccess.signalall(); //signal the readers available = false; return c; } }
考虑将块存储在 map
consider storing your chunks in a map