本文整理汇总了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");
}
示例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));
}