本文整理匯總了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);
}