Java 8 BlockingQueue - 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 BlockingQueue class.

This code example shows how to use the following methods:offer
        int count = size.incrementAndGet();
        if (count > capacity) {
            size.decrementAndGet();
            return false; 
        } 
        boolean offered = queue.offer(e);
        if (!offered) {
            size.decrementAndGet();
        } 
        return offered;
    } 
 
    @Override 
    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
        // note, not used in ThreadPoolExecutor 
        throw new ElasticSearchIllegalStateException("offer with timeout not allowed on size queue"); 
    } 
 
    @Override 
    public void put(E e) throws InterruptedException {
8
CodeRank
This code example shows how to use the following methods:poll
		} 
	} 
 
	public <T extends BaseResponsePacket> T commit(BasePacket packet) {
		try { 
			BlockingQueue<BaseResponsePacket> queue = new ArrayBlockingQueue<BaseResponsePacket>(1);
			results.put(packet.getRequest(), queue);
			Session session = sessionFactory.getSession(this, address, timeout,
					unit);
			packet.setTimeout(timeout); // set timeout, should before getBuffer
			ByteBuffer bb = packet.getBuffer();
			//System.out.println("start commit@" + System.currentTimeMillis()); 
			this.handle(session, session.commit(bb.array()));
			//System.out.println("end commit@" + System.currentTimeMillis()); 
			BaseResponsePacket result = queue.poll(timeout, unit);
			//System.out.println("get response@" + System.currentTimeMillis()); 
			if (result == null) {
				//session.close(); 
				//System.out.println("get null response"); 
				throw new TimeOutException("timeout: " + timeout + ", server: " + toString());
7
CodeRank
This code example shows how to use the following methods:add
	 * able to create new connections.  
	 * @throws SQLException  
	 * @throws CloneNotSupportedException */ 
	@Test 
	public void testConnectionMarkedBroken() throws SQLException, CloneNotSupportedException { 
		BlockingQueue<ConnectionHandle> fakeFreeConnections = new LinkedBlockingQueue<ConnectionHandle>(100);
		fakeFreeConnections.add(mockConnection);
		 
		BoneCPConfig localconfig = config.clone();
		expect(mockPool.getConfig()).andReturn(localconfig).anyTimes();
		expect(mockConnectionPartition.getAvailableConnections()).andReturn(1).anyTimes();
 
		expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes();
 		expect(mockConnection.isPossiblyBroken()).andReturn(true);
		 
		// connection should be closed 
		mockConnection.internalClose();
		mockPool.postDestroyConnection(mockConnection);
		expectLastCall().once(); 
		replay(mockPool, mockConnection, mockConnectionPartition, mockExecutor);
7
CodeRank
This code example shows how to use the following methods:poll
            server.getConfig().removeAllEventHandlers();
            server.getConfig().addEventHandler(
                    new BlockedFileSyslogServerEventHandler(queue, syslogFile.getAbsolutePath(), false));
        } 
        Assert.assertTrue(makeOneLog());
        queue.poll(15 * ADJUSTED_SECOND, TimeUnit.MILLISECONDS);
        List<ModelNode> syslogLogs = readFile(syslogFile, 1);
        ModelNode syslogLog = syslogLogs.get(0);
        Assert.assertEquals("core", syslogLog.get("type").asString());
        Assert.assertEquals("false", syslogLog.get("r/o").asString());
        Assert.assertEquals("false", syslogLog.get("booting").asString());
        Assert.assertTrue(log.get("version").isDefined());
        Assert.assertEquals("IAmAdmin", syslogLog.get("user").asString());
        Assert.assertFalse(syslogLog.get("domainUUID").isDefined());
        Assert.assertEquals("NATIVE", syslogLog.get("access").asString());
        Assert.assertTrue(syslogLog.get("remote-address").isDefined());
        Assert.assertEquals("true", syslogLog.get("success").asString());
        List<ModelNode> syslogOperations = syslogLog.get("ops").asList();
        Assert.assertEquals(1, syslogOperations.size());
 
6
CodeRank
This code example shows how to use the following methods:size, add
        Message msg = new Message();
        Message msg1 = new Message();
        Message msg2 = new Message();
        Message msg3 = new Message();
 
        BlockingQueue<Message> buffer = new LinkedBlockingDeque<Message>();
 
        buffer.add(msg);
        buffer.add(msg1);
        buffer.add(msg2);
        buffer.add(msg3);
 
        List<Message> list = new ArrayList<Message>();
        CGUtils.moveElements(buffer, list, blockSize);
 
        assertEquals("Some elements are missing", 4, list.size());
        assertEquals("Some elements are still there to drain", 0, buffer.size());
    } 
 
    public void testGetStringProperty() throws Exception { 

6
CodeRank
This code example shows how to use the following methods:take
    } 
 
    public T getResult() { 
        Object result; 
        try { 
            result = queue.take(); 
        } catch (InterruptedException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        } 
 
        if (result instanceof Throwable) {
            throw UncheckedException.throwAsUncheckedException(attachCallerThreadStackTrace((Throwable) result));
        } 
        if (result == NULL) { 
            return null; 
        } 
        return resultType.cast(result);
    } 
 
    private Throwable attachCallerThreadStackTrace(Throwable failure) {
6
CodeRank
This code example shows how to use the following methods:add
	 * able to create new connections.  
	 * @throws SQLException  
	 * @throws CloneNotSupportedException */ 
	@Test
	public void testConnectionMarkedBroken() throws SQLException, CloneNotSupportedException {
		BlockingQueue<ConnectionHandle> fakeFreeConnections = new LinkedBlockingQueue<ConnectionHandle>(100);
		fakeFreeConnections.add(mockConnection);
		 
		BoneCPConfig localconfig = config.clone();
		expect(mockPool.getConfig()).andReturn(localconfig).anyTimes();
		expect(mockConnectionPartition.getAvailableConnections()).andReturn(1).anyTimes();
 
		expect(mockConnectionPartition.getFreeConnections()).andReturn(fakeFreeConnections).anyTimes();
 		expect(mockConnection.isPossiblyBroken()).andReturn(true);
		 
		// connection should be closed 
		mockConnection.internalClose();
		mockPool.postDestroyConnection(mockConnection);
		expectLastCall().once(); 
		replay(mockPool, mockConnection, mockConnectionPartition, mockExecutor);
6
CodeRank
This code example shows how to use the following methods:offer
            ch = (SocketChannel) ctx.channel();
        } 
 
        @Override 
        public void messageReceived(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
            queue.offer(msg.readByte());
        } 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:poll
   * @return The new connection. 
   * @throws InterruptedException If the timeout expires. 
   */ 
  public WebSocketConnection getConnection(long timeout, TimeUnit unit)
      throws InterruptedException { 
    return connections.poll(timeout, unit);
  } 
} 
6
CodeRank
This code example shows how to use the following methods:size, offer, put
    public boolean requestIncoming(Request request, long timeout) {
        boolean retval = true;
        if (matcher.apply(request)) { 
            try { 
                if(timeout > 0) {
                    retval = queue.offer(request, timeout, TimeUnit.MILLISECONDS);
                } else { 
                    queue.put(request); 
                } 
            } catch (InterruptedException e) {
                LOGGER.log(Level.WARNING,
                        "Unexpected interruption while blocking on the request queue"); 
            } 
            if (LOGGER.isLoggable(Level.FINE)) {
                LOGGER.fine(this + " queue size " + queue.size());
            } 
 
        } 
        return retval;
    } 
6
CodeRank

Related Java 8 BlockingQueue Questions & Answers:

See Code Examples for Java 8 BlockingQueue Methods: