List of usage examples for java.util Deque offerFirst
boolean offerFirst(E e);
From source file:Main.java
public static void main(String[] args) { Deque<Integer> deque = new ArrayDeque<Integer>(8); deque.add(1);//from w w w .jav a 2 s . c o m deque.add(2); deque.add(3); deque.add(4); deque.offerFirst(10); System.out.println(deque); }
From source file:no.sesat.search.http.filters.SiteLocatorFilter.java
private static void doChainFilter(final FilterChain chain, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { final HttpSession session = request.getSession(); // fetch the user's deque final Deque<ServletRequest> deque = getUsersDeque(session); // lock to execute final ReentrantLock lock = (ReentrantLock) session.getAttribute(USER_REQUEST_LOCK); // deque has a time limit. start counting. long timeLeft = WAIT_TIME; try {/*from www .j av a 2 s .c om*/ // attempt to join deque if (deque.offerFirst(request)) { timeLeft = tryLock(request, deque, lock, timeLeft); } if (lock.isHeldByCurrentThread()) { // waiting is over. and we can execute chain.doFilter(request, response); } else { // we failed to execute. return 409 response. if (response instanceof HttpServletResponse) { LOG.warn(" -- response 409 " + (0 < timeLeft ? "(More then " + REQUEST_QUEUE_SIZE + " requests already in queue)" : "(Timeout: Waited " + WAIT_TIME + " ms)")); response.sendError(HttpServletResponse.SC_CONFLICT); } } } finally { // take out of deque first deque.remove(request); // release the lock, waiting up the next request if (lock.isHeldByCurrentThread()) { lock.unlock(); } } }
From source file:org.apache.giraph.comm.flow_control.CreditBasedFlowControl.java
/** * Try to send as much as cached requests to a given worker * * @param taskId id of the worker to send cached requests to *///from w w w . java 2 s . c o m private void trySendCachedRequests(int taskId) { Deque<WritableRequest> requestDeque = perWorkerUnsentRequestMap.get(taskId); AdjustableSemaphore openRequestPermit = perWorkerOpenRequestMap.get(taskId).getLeft(); while (true) { WritableRequest request; synchronized (requestDeque) { request = requestDeque.pollFirst(); if (request == null) { break; } // See whether the sender client has any unused credit if (!openRequestPermit.tryAcquire()) { requestDeque.offerFirst(request); break; } } unsentRequestPermit.release(); // At this point, we have a request, and we reserved a credit for the // sender client. So, we send the request to the client and update // the state. nettyClient.doSend(taskId, request); if (aggregateUnsentRequests.decrementAndGet() == 0) { synchronized (aggregateUnsentRequests) { aggregateUnsentRequests.notifyAll(); } } } }