이 경우 책 java Concurrency in Practice (이 경우에는 5.3 (생산자 - 소비자 패턴))을 권하고 싶습니다.
BlockingQueue<Data> queue =
new LinkedBlockingQueue<Data>(MAX_BLOCKS_IN_QUEUE_UNTIL_BLOCK);
for (int i=0; i < MAX_DATA_PRODUCERS; i++) {
new Thread(new DataProducer(queue)).start();
}
new Thread(DataWriter(queue)).start
은 분명히 DataProducer 및 DataWriter가보다 Runnable 있습니다
솔루션은 같은 것을 볼 수 있었다.
class DataProducer implements Runnable {
...
queue.put(data); // blocks if MAX_BLOCKS_IN_QUEUE_UNTIL_BLOCK
// are waiting to be written
// This prevents an OutOfMemoryException
...
}
class DataConsumer implements Runnable {
...
try {
while(true) {
writeData(queue.take()); // blocks until there is a block of data
}
} catch (InteruptedException e) {
Thread.currentThread().interrupt();
}
...
}
어떤 경우 든 비 연속적으로 작성하면 성능이 저하됩니다. 나는 하나의 작업자 (쓰는) 쓰레드로 블로킹 큐 솔루션이 더 나은 성능과 덜 두통을 줄 것이라고 생각한다. ... – extraneon
@extraneon 대답과 주석이 아니어야한다.) – WarmWaffles
명확성을 위해서 - 동시 작성은 요구 사항을 충족시키지 않거나 완료되면 데이터를 덤프 할 수있는 스레드를 생성하는 데이터로만 전환하여 다른 블록을 처리 할 수 있습니까? – extraneon