本文整理汇总了Java中java.util.concurrent.ConcurrentLinkedDeque.offer方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentLinkedDeque.offer方法的具体用法?Java ConcurrentLinkedDeque.offer怎么用?Java ConcurrentLinkedDeque.offer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ConcurrentLinkedDeque
的用法示例。
在下文中一共展示了ConcurrentLinkedDeque.offer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testOfferNull
import java.util.concurrent.ConcurrentLinkedDeque; //导入方法依赖的package包/类
/**
* offer(null) throws NPE
*/
public void testOfferNull() {
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
try {
q.offer(null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例2: testBufferSizeGrowWithMultiThread
import java.util.concurrent.ConcurrentLinkedDeque; //导入方法依赖的package包/类
@Test
public void testBufferSizeGrowWithMultiThread() throws Exception {
final ConcurrentLinkedDeque<ByteBuffer> bufferQueue = new ConcurrentLinkedDeque<ByteBuffer>();
int takeBufferThreadsCount = 30;
int putBufferThreadsCount = 1;
Thread takeBufferThreads[] = new Thread[takeBufferThreadsCount];
for (int i = 0; i < takeBufferThreadsCount; i++) {
takeBufferThreads[i] = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
ByteBuffer buffer = reservoir.getBuffer();
try {
Thread.sleep(5);
} catch (InterruptedException e) {
break;
}
bufferQueue.offer(buffer);
if (Thread.currentThread().isInterrupted()) break;
}
}
});
}
Thread putBufferThread[] = new Thread[putBufferThreadsCount];
for (int i = 0; i < putBufferThreadsCount; i++) {
putBufferThread[i] = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
ByteBuffer buffer = bufferQueue.poll();
if (buffer != null) {
reservoir.putBuffer(buffer);
}
if (Thread.currentThread().isInterrupted()) break;
}
}
});
}
for (int i = 0; i < takeBufferThreadsCount; i++) {
takeBufferThreads[i].start();
}
for (int i = 0; i < putBufferThreadsCount; i++) {
putBufferThread[i].start();
}
Thread.sleep(2 * 1000);// Let the threads run for 2 secs
for (int i = 0; i < takeBufferThreadsCount; i++) {
takeBufferThreads[i].interrupt();
takeBufferThreads[i].join();
}
for (int i = 0; i < putBufferThreadsCount; i++) {
putBufferThread[i].interrupt();
putBufferThread[i].join();
}
// None of the BBs we got from pool is growing while in use. So we should not change the
// runningAverage in pool
assertEquals(initialByteBufferSize, this.reservoir.runningAverage);
}