本文整理汇总了Java中java.util.concurrent.ArrayBlockingQueue.peek方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayBlockingQueue.peek方法的具体用法?Java ArrayBlockingQueue.peek怎么用?Java ArrayBlockingQueue.peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ArrayBlockingQueue
的用法示例。
在下文中一共展示了ArrayBlockingQueue.peek方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: throttleChunkReading
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Test
public void throttleChunkReading() throws IOException, InterruptedException {
final Utils.LogFile logFile = new Utils.LogFile(100 * 1024, 400, 100); // 100kb simulated log file, lines max 400 chars long, 100 chars deviation
final Path tempFile = logFile.getPath();
logFile.close();
final ArrayBlockingQueue<FileChunk> chunkQueue = Queues.newArrayBlockingQueue(1);
final AsynchronousFileChannel channel = AsynchronousFileChannel.open(tempFile, StandardOpenOption.READ);
final CountingAsyncFileChannel spy = new CountingAsyncFileChannel(channel);
final ChunkReader chunkReader = new ChunkReader(mock(FileInput.class), tempFile, spy, chunkQueue, 10 * 1024,
FileInput.InitialReadPosition.START, null);
final ScheduledExecutorService chunkReaderExecutor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setDaemon(false)
.setNameFormat("file-chunk-reader-%d")
.setUncaughtExceptionHandler(this)
.build()
);
final Thread consumer = new Thread() {
@Override
public void run() {
try {
while (null == chunkQueue.peek()) {
// spin until the first chunk appears, then block for longer than the executor schedules tasks
}
log.debug("Found first chunk");
// do nothing for a while, just make sure the chunkreader isn't trying to read from the channel in the meantime!
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
}
};
consumer.start();
chunkReaderExecutor.scheduleAtFixedRate(chunkReader, 0, 250, TimeUnit.MILLISECONDS);
consumer.join();
// we can process one chunk at a time, so one read is queued, the second is buffered
assertEquals("ChunkReader should perform two reads only", 2, spy.getReadCount());
assertEquals("The queue should be full", 0, chunkQueue.remainingCapacity());
}