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


Java LinkedBlockingDeque.take方法代码示例

本文整理汇总了Java中java.util.concurrent.LinkedBlockingDeque.take方法的典型用法代码示例。如果您正苦于以下问题:Java LinkedBlockingDeque.take方法的具体用法?Java LinkedBlockingDeque.take怎么用?Java LinkedBlockingDeque.take使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.LinkedBlockingDeque的用法示例。


在下文中一共展示了LinkedBlockingDeque.take方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import java.util.concurrent.LinkedBlockingDeque; //导入方法依赖的package包/类
/**
 * Main method of the class
 * 
 * @param args
 */
public static void main(String[] args) throws Exception {

	// Create a ConcurrentLinkedDeque to work with it in the example
	LinkedBlockingDeque<String> list = new LinkedBlockingDeque<>(3);

	Client client = new Client(list);
	Thread thread = new Thread(client);
	thread.start();

	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 3; j++) {
			String request = list.take();
			System.out.printf("Main: Removed: %s at %s. Size: %d\n", request, new Date(), list.size());
		}
		TimeUnit.MILLISECONDS.sleep(300);
	}

	System.out.printf("Main: End of the program.\n");

}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:26,代码来源:Main.java

示例2: testMultiThreadedDecompressorPool

import java.util.concurrent.LinkedBlockingDeque; //导入方法依赖的package包/类
@Test(timeout = 1000)
public void testMultiThreadedDecompressorPool() throws InterruptedException {
  final int iterations = 4;
  ExecutorService threadpool = Executors.newFixedThreadPool(3);
  final LinkedBlockingDeque<Decompressor> queue = new LinkedBlockingDeque<Decompressor>(
      2 * iterations);

  Callable<Boolean> consumer = new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
      Decompressor dc = queue.take();
      CodecPool.returnDecompressor(dc);
      return dc != null;
    }
  };

  Callable<Boolean> producer = new Callable<Boolean>() {
    @Override
    public Boolean call() throws Exception {
      Decompressor c = CodecPool.getDecompressor(codec);
      queue.put(c);
      return c != null;
    }
  };

  for (int i = 0; i < iterations; i++) {
    threadpool.submit(consumer);
    threadpool.submit(producer);
  }

  // wait for completion
  threadpool.shutdown();
  threadpool.awaitTermination(1000, TimeUnit.SECONDS);

  assertEquals(LEASE_COUNT_ERR, 0,
      CodecPool.getLeasedDecompressorsCount(codec));
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:38,代码来源:TestCodecPool.java


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