我试图创建一个系统,其中一个线程a将项目添加到缓冲区,然后另一个线程B负责按照它们进入的确切顺序读取项目,然后对它们进行一些可能的长时间操作。
我最好的猜测:
Class B extends Thread {
Buffer fifo = BufferUtils.synchronizedBuffer(new BoundedFifoBuffer());
add(Object o) { // Thread A calls me, and doesn't deal well with delays :)
fifo.add(o); // will the sync below prevent this from happening?
// or can .add be independent of the sync ?
}
run() {
synchronized (fifo) { // why am i sync'd here? I am the only thread accessing...
while ( item in buffer ) { // also how do i check this, and block otherwise?
process(fifo.remove());
}
}
|
}
正如您所看到的,我甚至不能完全确定同步是否必要。我的线程安全问题与get()访问无关,因为只有一个线程访问它,但最重要的是线程A在线程B处理缓冲区内容期间调用.add()而没有任何并发访问异常。
也许我想太多了?和他在一起安全吗?非常感谢您对这个问题的评价。 真诚周杰伦如果我没有错,你也可能对这个ArrayBlockingQueue类感兴趣。
如果要记录一个字符流,最快的方法可能是使用管道。
PipedOutputStream pos = new PipedOutputStream();
final PipedInputStream pis = new PipedInputStream(pos, 256*1024);
ExecutorService es = Executors.newSingleThreadExecutor();
es.execute(new Runnable() {
@Override
public void run() {
byte[] bytes = new byte[256*1024];
int length;
try {
while ((length = pis.read(bytes)) > 0) {
// something slow.
Thread.sleep(1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
// time latency
PrintWriter pw = new PrintWriter(pos);
long start = System.nanoTime();
int runs = 10*1000*1000;
for(int i=0;i<runs;i++) {
pw.println("Hello "+i);
}
long time = System.nanoTime() - start;
System.out.printf("Took an average of %,d nano-seconds per line%n", time/runs);
es.shutdown();
打印
Took an average of 269 nano-seconds per line
注意:管道本身不会产生任何垃圾。(与队列不同)
你可以使用ExecutorService来封装一个队列和线程
ExecutorService es =
es.submit(new Runnable() {
public void run() {
process(o);
}
});