Java 8 Queue - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 Queue class.

This code example shows how to use the following methods:offer
        } 
 
        @Override 
        public void release() { 
            c.clear(value); 
            queue.offer(this); 
        } 
    } 
} 
8
CodeRank
This code example shows how to use the following methods:add
    buf.clear(); // reset mark, limit, etc
    int size = buf.capacity();
    Queue<WeakReference<ByteBuffer>> list = buffersBySize.get(size);
    if (list == null) {
      list = new ConcurrentLinkedQueue<WeakReference<ByteBuffer>>();
      Queue<WeakReference<ByteBuffer>> prev = buffersBySize.putIfAbsent(size, list);
      // someone else put a queue in the map before we did 
      if (prev != null) {
        list = prev;
      } 
    } 
    list.add(new WeakReference<ByteBuffer>(buf));
  } 
   
  /** 
   * Return the number of available buffers of a given size. 
   * This is used only for tests. 
   */ 
  @VisibleForTesting 
  int countBuffersOfSize(int size) {
7
CodeRank
This code example shows how to use the following methods:add, peek, poll
		assertEquals("b", queue.poll());
		assertNull(queue.poll());
	} 
 
	public void testPeek() { 
		assertNull(queue.peek());
		queue.add("a");
		assertEquals("a", queue.peek());
		queue.add("b");
		assertEquals("a", queue.peek());
		queue.add("c");
		assertEquals("a", queue.peek());
		queue.poll();
		assertEquals("b", queue.peek());
		queue.poll();
		assertEquals("c", queue.peek());
		queue.poll();
		assertNull(queue.peek());
	} 
 

7
CodeRank
This code example shows how to use the following methods:add
	/** Tests that a connection that has been idle for more than the set time is closed off.  
	 * @throws SQLException  
	 * @throws CloneNotSupportedException */ 
	@Test 
	public void testIdleConnectionIsKilled() throws SQLException, CloneNotSupportedException { 
		LinkedBlockingQueue<ConnectionHandle> fakeFreeConnections = new LinkedBlockingQueue<ConnectionHandle>(100);
		fakeFreeConnections.add(mockConnection);
		fakeFreeConnections.add(mockConnection);
		BoneCPConfig localconfig = config.clone();
		expect(mockPool.getConfig()).andReturn(localconfig.clone()).anyTimes();
		expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes();
		expect(mockConnectionPartition.getAvailableConnections()).andReturn(2).anyTimes();
		 
//		expect(mockConnectionPartition.getMinConnections()).andReturn(0).once(); 
		expect(mockConnection.isPossiblyBroken()).andReturn(false);
		expect(mockConnection.getConnectionLastUsedInMs()).andReturn(0L);
		 
		// connection should be closed 
		mockConnection.internalClose();
		mockPool.postDestroyConnection(mockConnection);
7
CodeRank
This code example shows how to use the following methods:isEmpty, poll
		} 
		return token;
	} 
	 
	public boolean next(){ 
		queue.poll();
		if(queue.isEmpty()){
			return false; 
		} 
		return true; 
	} 
	public KV getCurrent(){ 
		return queue.peek();
	} 
	public Iterator<KV> iterator(){
	    return queue.iterator();
	} 
	 
	public boolean isOriginal(){ 
		return source.size()==queue.size();
7
CodeRank
This code example shows how to use the following methods:add
      throw new OLockException("Error on creation of the new resource in the pool", e);
    } 
  } 
 
  public void returnResource(final V res) {
    resources.add(res);
    sem.release();
  } 
 
  public Collection<V> getResources() {
    return unmodifiableresources;
  } 
 
  public void close() { 
    sem.drainPermits();
  } 
   
  public void remove(final V res) {
  	this.resources.remove(res);
  } 
7
CodeRank
This code example shows how to use the following methods:size, add, remove
        this.period = period;
    } 
 
    public void newNum(double num) {
        sum += num;
        window.add(num);
        if (window.size() > period) {
            sum -= window.remove();
        } 
    } 
 
    public double getAvg() { 
        if (window.isEmpty()) return 0; // technically the average is undefined
        return sum / window.size();
    } 
 
    public static void main(String[] args) {
        double[] testData = {1,2,3,4,5,5,4,3,2,1};
        int[] windowSizes = {3,5};
        for (int windSize : windowSizes) {
6
CodeRank
This code example shows how to use the following methods:isEmpty, add, peek, remove
   * directory. 
   *  
   * @return {@link String} relative path for localization 
   */ 
  public synchronized String getRelativePathForLocalization() {
    if (nonFullDirectories.isEmpty()) {
      totalSubDirectories++;
      Directory newDir = new Directory(totalSubDirectories);
      nonFullDirectories.add(newDir);
      knownDirectories.put(newDir.getRelativePath(), newDir);
    } 
    Directory subDir = nonFullDirectories.peek();
    if (subDir.incrementAndGetCount() >= perDirectoryFileLimit) {
      nonFullDirectories.remove();
    } 
    return subDir.getRelativePath();
  } 
 
  /** 
   * This method will reduce the file count for the directory represented by 
7
CodeRank
This code example shows how to use the following methods:add
    @Override 
    protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
        if (msg instanceof HttpMessage) {
            boolean contains = ((HttpMessage) msg).headers().contains(SpdyHttpHeaders.Names.STREAM_ID);
            if (!contains) {
                ids.add(NO_ID);
            } else { 
                ids.add(((HttpMessage) msg).headers().getInt(Names.STREAM_ID));
            } 
        } else if (msg instanceof SpdyRstStreamFrame) { 
            ids.remove(((SpdyRstStreamFrame) msg).streamId());
        } 
 
        out.add(ReferenceCountUtil.retain(msg)); 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:add, peek, poll
    } 
 
 
    @Test
    public void testPeek() { 
        assertNull(queue.peek());
        queue.add("a");
        assertEquals("a", queue.peek());
        queue.add("b");
        assertEquals("a", queue.peek());
        queue.add("c");
        assertEquals("a", queue.peek());
        queue.poll();
        assertEquals("b", queue.peek());
        queue.poll();
        assertEquals("c", queue.peek());
        queue.poll();
        assertNull(queue.peek());
    } 
 

7
CodeRank

Related Java 8 Queue Questions & Answers:

See Code Examples for Java 8 Queue Methods: