本文整理汇总了Java中java.util.concurrent.ConcurrentLinkedQueue.add方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentLinkedQueue.add方法的具体用法?Java ConcurrentLinkedQueue.add怎么用?Java ConcurrentLinkedQueue.add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ConcurrentLinkedQueue
的用法示例。
在下文中一共展示了ConcurrentLinkedQueue.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
public static void main(String[] args) {
int i = 0;
// Without bug fix, OutOfMemoryError was observed at iteration 65120
int iterations = 10 * 65120;
try {
ConcurrentLinkedQueue<Long> queue = new ConcurrentLinkedQueue<>();
queue.add(0L);
while (i++ < iterations) {
queue.add(1L);
queue.remove(1L);
}
} catch (Error t) {
System.err.printf("failed at iteration %d/%d%n", i, iterations);
throw t;
}
}
示例2: addToPacketBuffer
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
/**
* Adds the IP packet to a buffer.
* The packets are forwarded to corresponding destination when the destination
* MAC address is known via ARP response.
*
* @param ipPacket IP packet to add to the buffer
*/
public void addToPacketBuffer(IPv4 ipPacket) {
// Better not buffer TCP packets due to out-of-order packet transfer
if (ipPacket.getProtocol() == IPv4.PROTOCOL_TCP) {
return;
}
Ip4Address destIpAddress = Ip4Address.valueOf(ipPacket.getDestinationAddress());
if (ipPacketQueue.get(destIpAddress) == null) {
ConcurrentLinkedQueue<IPv4> queue = new ConcurrentLinkedQueue<>();
queue.add(ipPacket);
ipPacketQueue.put(destIpAddress, queue);
} else {
ipPacketQueue.get(destIpAddress).add(ipPacket);
}
}
示例3: traversalOperationsCollapseRandomNodes
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
/**
* Checks that traversal operations collapse a random pattern of
* dead nodes as could normally only occur with a race.
*/
@Test(dataProvider = "traversalActions")
public void traversalOperationsCollapseRandomNodes(
Consumer<ConcurrentLinkedQueue> traversalAction) {
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
int n = rnd.nextInt(6);
for (int i = 0; i < n; i++) q.add(i);
ArrayList nulledOut = new ArrayList();
for (Object p = head(q); p != null; p = next(p))
if (item(p) != null && rnd.nextBoolean()) {
nulledOut.add(item(p));
ITEM.setVolatile(p, null);
}
traversalAction.accept(q);
int c = nodeCount(q);
assertEquals(q.size(), c - (q.contains(n - 1) ? 0 : 1));
for (int i = 0; i < n; i++)
assertTrue(nulledOut.contains(i) ^ q.contains(i));
}
示例4: pendCompensationLogContext
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
private void pendCompensationLogContext(LogProcessContext logProcessContext) {
try {
ConcurrentLinkedQueue<LogProcessContext> set = compensationLogContextCache.get(getParentTransactionId(),new Callable<ConcurrentLinkedQueue<LogProcessContext>>(){
@Override
public ConcurrentLinkedQueue<LogProcessContext> call() throws Exception {
return new ConcurrentLinkedQueue<LogProcessContext>();
}
});
set.add(logProcessContext);
} catch (ExecutionException e) {
//it's OK to just log,this operation is just for efficient
LOG.error("cache pending transaction error",e);
}
}
示例5: testConcurrency_MultiInstance_Ordering
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
@Test
public void testConcurrency_MultiInstance_Ordering() throws InterruptedException {
final ConcurrentLinkedQueue<Thread> queue = new ConcurrentLinkedQueue<>();
final AtomicInteger lockedCounter = new AtomicInteger();
int totalThreads = Runtime.getRuntime().availableProcessors()*2;
for (int i = 0; i < totalThreads; i++) {
Thread t1 = new Thread(() -> {
Lock lock = redisson.getFairLock("testConcurrency_MultiInstance2");
queue.add(Thread.currentThread());
lock.lock();
Thread t = queue.poll();
assertThat(t).isEqualTo(Thread.currentThread());
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
lockedCounter.incrementAndGet();
lock.unlock();
});
Thread.sleep(10);
t1.start();
}
await().atMost(30, TimeUnit.SECONDS).until(() -> assertThat(lockedCounter.get()).isEqualTo(totalThreads));
}
示例6: requestLimit
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
@Before("@annotation(limit)")
public void requestLimit(JoinPoint joinPoint, RequestLimit limit) throws Exception {
DefaultRequestLimitKey key = new DefaultRequestLimitKey(joinPoint);
if (isForbidden(key, limit.period())) {
return;
}
// 时间
Long now = System.currentTimeMillis();
Long before = now - limit.time();
ConcurrentLinkedQueue<Long> queue = limitAccessTime.get(key);
int count = 1;
if (queue != null) {
Iterator<Long> itr = queue.iterator();
while (itr.hasNext()) {
long accessTime = itr.next();
if (accessTime < before) {
itr.remove();
} else {
count++;
}
}
} else {
queue = new ConcurrentLinkedQueue<>();
}
if (count > limit.count()) {
logger.info(key+ " 超过了次数限制" + limit.count());
throw new RequestLimitException(key+ " 超过了次数限制" + limit.count());
}
queue.add(now);
limitAccessTime.put(key, queue);
}
示例7: put
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
public void put(K key) {
Iterator<ConcurrentLinkedQueue<K>> it = _buckets.iterator();
ConcurrentLinkedQueue<K> bucket = it.next();
bucket.add(key);
if (isDelete) {
while (it.hasNext()) {
bucket = it.next();
if (contains(key, bucket)) {
bucket.remove(key);
}
}
}
}
示例8: put
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
public void put(Node node, ClientRequest request) {
// the lock protects the put from a concurrent removal of the queue for the node
synchronized (unsent) {
ConcurrentLinkedQueue<ClientRequest> requests = unsent.get(node);
if (requests == null) {
requests = new ConcurrentLinkedQueue<>();
unsent.put(node, requests);
}
requests.add(request);
}
}
示例9: bulkRemovalOperationsCollapseNodes
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
@Test(dataProvider = "bulkRemovalActions")
public void bulkRemovalOperationsCollapseNodes(
Consumer<ConcurrentLinkedQueue> bulkRemovalAction) {
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
int n = 1 + rnd.nextInt(5);
for (int i = 0; i < n; i++) q.add(i);
bulkRemovalAction.accept(q);
assertEquals(nodeCount(q), 1);
assertInvariants(q);
}
示例10: generateLogEvents
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
private ConcurrentLinkedQueue<TestTuple> generateLogEvents() {
ConcurrentLinkedQueue<TestTuple> events = new ConcurrentLinkedQueue<>();
Random random = new Random();
for (int ii = 0; ii < 1000; ii++) {
LogEvent logEvent = mock(LogEvent.class);
int increment = random.nextInt(3) - 1;
when(logEvent.getTimeMillis()).thenReturn(DEFAULT_TEST_TIME_IN_MILLIS + increment * 60000 + random.nextInt(60000));
events.add(new TestTuple(logEvent, increment));
}
return events;
}
示例11: testAddNull
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
/**
* add(null) throws NPE
*/
public void testAddNull() {
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
try {
q.add(null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例12: testClear
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
/**
* clear removes all elements
*/
public void testClear() {
ConcurrentLinkedQueue q = populatedQueue(SIZE);
q.clear();
assertTrue(q.isEmpty());
assertEquals(0, q.size());
q.add(one);
assertFalse(q.isEmpty());
q.clear();
assertTrue(q.isEmpty());
}
示例13: testContainsAll
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
/**
* containsAll(c) is true when c contains a subset of elements
*/
public void testContainsAll() {
ConcurrentLinkedQueue q = populatedQueue(SIZE);
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.containsAll(p));
assertFalse(p.containsAll(q));
p.add(new Integer(i));
}
assertTrue(p.containsAll(q));
}
示例14: testIteratorOrdering
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
/**
* iterator ordering is FIFO
*/
public void testIteratorOrdering() {
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
q.add(one);
q.add(two);
q.add(three);
int k = 0;
for (Iterator it = q.iterator(); it.hasNext();) {
assertEquals(++k, it.next());
}
assertEquals(3, k);
}
示例15: inCompletionOrder
import java.util.concurrent.ConcurrentLinkedQueue; //导入方法依赖的package包/类
/**
* Returns a list of delegate futures that correspond to the futures received in the order that
* they complete. Delegate futures return the same value or throw the same exception as the
* corresponding input future returns/throws.
*
* <p>Cancelling a delegate future has no effect on any input future, since the delegate future
* does not correspond to a specific input future until the appropriate number of input futures
* have completed. At that point, it is too late to cancel the input future. The input future's
* result, which cannot be stored into the cancelled delegate future, is ignored.
*
* @since 17.0
*/
@Beta
@GwtIncompatible // TODO
public static <T> ImmutableList<ListenableFuture<T>> inCompletionOrder(
Iterable<? extends ListenableFuture<? extends T>> futures) {
// A CLQ may be overkill here. We could save some pointers/memory by synchronizing on an
// ArrayDeque
final ConcurrentLinkedQueue<SettableFuture<T>> delegates = Queues.newConcurrentLinkedQueue();
ImmutableList.Builder<ListenableFuture<T>> listBuilder = ImmutableList.builder();
// Using SerializingExecutor here will ensure that each CompletionOrderListener executes
// atomically and therefore that each returned future is guaranteed to be in completion order.
// N.B. there are some cases where the use of this executor could have possibly surprising
// effects when input futures finish at approximately the same time _and_ the output futures
// have directExecutor listeners. In this situation, the listeners may end up running on a
// different thread than if they were attached to the corresponding input future. We believe
// this to be a negligible cost since:
// 1. Using the directExecutor implies that your callback is safe to run on any thread.
// 2. This would likely only be noticeable if you were doing something expensive or blocking on
// a directExecutor listener on one of the output futures which is an antipattern anyway.
SerializingExecutor executor = new SerializingExecutor(directExecutor());
for (final ListenableFuture<? extends T> future : futures) {
SettableFuture<T> delegate = SettableFuture.create();
// Must make sure to add the delegate to the queue first in case the future is already done
delegates.add(delegate);
future.addListener(
new Runnable() {
@Override
public void run() {
delegates.remove().setFuture(future);
}
},
executor);
listBuilder.add(delegate);
}
return listBuilder.build();
}