当前位置: 首页>>代码示例>>Java>>正文


Java ArrayBlockingQueue.peek方法代码示例

本文整理汇总了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());
}
 
开发者ID:DevOpsStudio,项目名称:Re-Collector,代码行数:43,代码来源:ChunkReaderTest.java


注:本文中的java.util.concurrent.ArrayBlockingQueue.peek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。