Java Code Examples for java.util.concurrent.ArrayBlockingQueue#offer()

The following examples show how to use java.util.concurrent.ArrayBlockingQueue#offer() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: openjdk-systemtest   File: TestArrays.java    License: Apache License 2.0 6 votes vote down vote up
public void test() throws Exception{
	
	Vector<Object> vector = new Vector<Object>();
	ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<Object>(50000);

	Random myRandom = new Random();
	for (int j = 0; j<50000; j++){
		if (myRandom.nextInt(2) == 0){
			vector.add(i);
			queue.offer(i);
		}
		else{
			vector.add(k);
			queue.offer(k);
		}
	}
	
	assertTrue(Arrays.equals(vector.toArray(), queue.toArray()));
	
}
 
Example 2
/**
 * Put the message into internal queues, to be serialized and then send to the network channel
 *
 * @param source source
 * @param message data
 * @param target target
 * @param flags flags
 * @param routingParameters routing parameters
 * @param pendingSendMessages the message queue
 * @return true if message is accepted
 */
private boolean offerForSend(int source, Object message, int target, int flags,
                             RoutingParameters routingParameters,
                             ArrayBlockingQueue<OutMessage> pendingSendMessages) {
  if (pendingSendMessages.remainingCapacity() > 0) {
    int path = DEFAULT_PATH;
    if (routingParameters.getExternalRoutes().size() > 0) {
      path = routingParameters.getDestinationId();
    }

    OutMessage sendMessage = new OutMessage(source, edge,
        path, target, flags, routingParameters.getInternalRoutes(),
        routingParameters.getExternalRoutes(), dataType, keyType, this, message);

    // now try to put this into pending
    return pendingSendMessages.offer(sendMessage);
  }
  return false;
}
 
Example 3
/**
 * Put the message into internal queues, to be serialized and then send to the network channel
 *
 * @param source source
 * @param message data
 * @param target target
 * @param flags flags
 * @param routingParameters routing parameters
 * @param pendingSendMessages the message queue
 * @return true if message is accepted
 */
private boolean offerForSend(int source, Object message, int target, int flags,
                             RoutingParameters routingParameters,
                             ArrayBlockingQueue<OutMessage> pendingSendMessages) {
  if (pendingSendMessages.remainingCapacity() > 0) {
    int path = DEFAULT_PATH;
    if (routingParameters.getExternalRoutes().size() > 0) {
      path = routingParameters.getDestinationId();
    }

    OutMessage sendMessage = new OutMessage(source, edge,
        path, target, flags, routingParameters.getInternalRoutes(),
        routingParameters.getExternalRoutes(), dataType, keyType, this, message);

    // now try to put this into pending
    return pendingSendMessages.offer(sendMessage);
  }
  return false;
}
 
Example 4
Source Project: gemfirexd-oss   File: ImportBase.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean addQueueData(ArrayBlockingQueue<QueueData> queue,
    QueueData data, AtomicReference<Throwable> err, Thread[] threads)
    throws CacheClosedException, InterruptedException {
  while (!queue.offer(data, 1, TimeUnit.SECONDS)) {
    // check cancellation
    Misc.checkIfCacheClosing(null);
    // check if a thread failed
    if (err.get() != null) {
      return false;
    }
    // should we have something else?
    boolean someAlive = false;
    for (Thread thr: threads) {
      if (thr.isAlive()) {
        someAlive = true;
        break;
      }
    }
    if (!someAlive) {
      return false;
    }
  }
  return true;
}
 
Example 5
Source Project: gemfirexd-oss   File: ImportBase.java    License: Apache License 2.0 6 votes vote down vote up
private static boolean addQueueData(ArrayBlockingQueue<QueueData> queue,
    QueueData data, AtomicReference<Throwable> err, Thread[] threads)
    throws CacheClosedException, InterruptedException {
  while (!queue.offer(data, 1, TimeUnit.SECONDS)) {
    // check cancellation
    Misc.checkIfCacheClosing(null);
    // check if a thread failed
    if (err.get() != null) {
      return false;
    }
    // should we have something else?
    boolean someAlive = false;
    for (Thread thr: threads) {
      if (thr.isAlive()) {
        someAlive = true;
        break;
      }
    }
    if (!someAlive) {
      return false;
    }
  }
  return true;
}
 
Example 6
Source Project: quaerite   File: IdGrabber.java    License: Apache License 2.0 5 votes vote down vote up
protected int addSet(ArrayBlockingQueue<Set<String>> ids, Set<String> set) throws InterruptedException {
    int sz = set.size();
    //don't bother adding if set size == 0
    if (sz == 0) {
        return sz;
    }
    boolean added = ids.offer(set, 1, TimeUnit.SECONDS);
    LOG.debug("id grabber: " + added + " " + ids.size());
    while (!added) {
        added = ids.offer(set, 1, TimeUnit.SECONDS);
        LOG.debug("waiting to add");
    }
    return sz;
}
 
Example 7
public static void main(String[] args) throws InterruptedException {
    ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(5);
    queue.offer(1);
    queue.offer(2);
    queue.offer(3);
    System.out.println("Queue Contains" + queue);
    System.out.println("Removing From head: " + queue.poll());
    System.out.println("Queue Contains" + queue);
    System.out.println("Removing From head: " + queue.poll());
    System.out.println("Queue Contains" + queue);
    System.out.println("Removing From head: " + queue.poll());
    System.out.println("Queue Contains" + queue);
    System.out.println("Removing From head: " + queue.poll());
    System.out.println("Queue Contains" + queue);
}
 
Example 8
Source Project: twister2   File: TreeBroadcast.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean handleReceivedChannelMessage(ChannelMessage currentMessage) {
  int src = router.mainTaskOfExecutor(instancePlan.getThisWorker(),
      CommunicationContext.DEFAULT_DESTINATION);

  RoutingParameters routingParameters;
  if (routingParametersCache.containsKey(src)) {
    routingParameters = routingParametersCache.get(src);
  } else {
    routingParameters = sendRoutingParameters(src, CommunicationContext.DEFAULT_DESTINATION);
  }

  ArrayBlockingQueue<OutMessage> pendingSendMessages = pendingSendMessagesPerSource.get(src);

  // create a send message to keep track of the serialization at the initial stage
  // the sub-edge is 0
  int di = -1;
  if (routingParameters.getExternalRoutes().size() > 0) {
    di = routingParameters.getDestinationId();
  }
  OutMessage sendMessage = new OutMessage(src,
      currentMessage.getHeader().getEdge(),
      di, CommunicationContext.DEFAULT_DESTINATION, currentMessage.getHeader().getFlags(),
      routingParameters.getInternalRoutes(),
      routingParameters.getExternalRoutes(), dataType, this.keyType, delegate,
      CommunicationContext.EMPTY_OBJECT);
  sendMessage.getChannelMessages().offer(currentMessage);

  // we need to update here
  if (!currentMessage.isOutCountUpdated()) {
    currentMessage.incrementRefCount(routingParameters.getExternalRoutes().size());
    currentMessage.setOutCountUpdated(true);
  }
  // this is a complete message
  sendMessage.setSendState(OutMessage.SendState.SERIALIZED);

  // now try to put this into pending
  return pendingSendMessages.offer(sendMessage);
}
 
Example 9
Source Project: emodb   File: LocalRangeScanUploader.java    License: Apache License 2.0 5 votes vote down vote up
private void submitResultBatch(BatchContext context, ArrayBlockingQueue<Batch> queue, Batch batch, boolean continuedInNextBatch)
        throws IOException, InterruptedException {
    if (!batch.isEmpty()) {
        // Mark this batch as open
        context.openBatch(batch);

        try {
            batch.setContinuedInNextBatch(continuedInNextBatch);

            // Attempt to submit the batch to the result queue without blocking
            if (!queue.offer(batch)) {
                // The queue was full.  Increment the blocked counter and synchronously wait for queue availability
                _blockedRangeScans.inc();
                try {
                    while (!queue.offer(batch, 5, TimeUnit.SECONDS)) {
                        context.propagateExceptionIfPresent();
                    }
                } finally {
                    _blockedRangeScans.dec();
                }
            }
            _batchesSubmitted.inc();
            _batchRowsSubmitted.inc(batch.getResults().size());
        } catch (IOException | InterruptedException e) {
            // Batch was never submitted so un-mark that it is open
            context.closeBatch(batch, e);
            throw e;
        }
    }
}
 
Example 10
Source Project: flink   File: DispatcherTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a failing {@link JobManagerRunner} will be properly cleaned up.
 */
@Test
public void testFailingJobManagerRunnerCleanup() throws Exception {
	final FlinkException testException = new FlinkException("Test exception.");
	final ArrayBlockingQueue<Optional<Exception>> queue = new ArrayBlockingQueue<>(2);

	dispatcher = createAndStartDispatcher(
		heartbeatServices,
		haServices,
		new BlockingJobManagerRunnerFactory(() -> {
			final Optional<Exception> take = queue.take();
			final Exception exception = take.orElse(null);

			if (exception != null) {
				throw exception;
			}
		}));

	final DispatcherGateway dispatcherGateway = dispatcher.getSelfGateway(DispatcherGateway.class);

	CompletableFuture<Acknowledge> submissionFuture = dispatcherGateway.submitJob(jobGraph, TIMEOUT);

	assertThat(submissionFuture.isDone(), is(false));

	queue.offer(Optional.of(testException));

	try {
		submissionFuture.get();
		fail("Should fail because we could not instantiate the JobManagerRunner.");
	} catch (Exception e) {
		assertThat(ExceptionUtils.findThrowable(e, t -> t.equals(testException)).isPresent(), is(true));
	}

	submissionFuture = dispatcherGateway.submitJob(jobGraph, TIMEOUT);

	queue.offer(Optional.empty());

	submissionFuture.get();
}
 
Example 11
Source Project: Flink-CEPplus   File: DispatcherTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests that a failing {@link JobManagerRunner} will be properly cleaned up.
 */
@Test
public void testFailingJobManagerRunnerCleanup() throws Exception {
	final FlinkException testException = new FlinkException("Test exception.");
	final ArrayBlockingQueue<Optional<Exception>> queue = new ArrayBlockingQueue<>(2);

	dispatcher = createAndStartDispatcher(
		heartbeatServices,
		haServices,
		new BlockingJobManagerRunnerFactory(() -> {
			final Optional<Exception> take = queue.take();
			final Exception exception = take.orElse(null);

			if (exception != null) {
				throw exception;
			}
		}));

	dispatcherLeaderElectionService.isLeader(UUID.randomUUID()).get();

	final DispatcherGateway dispatcherGateway = dispatcher.getSelfGateway(DispatcherGateway.class);

	CompletableFuture<Acknowledge> submissionFuture = dispatcherGateway.submitJob(jobGraph, TIMEOUT);

	assertThat(submissionFuture.isDone(), is(false));

	queue.offer(Optional.of(testException));

	try {
		submissionFuture.get();
		fail("Should fail because we could not instantiate the JobManagerRunner.");
	} catch (Exception e) {
		assertThat(ExceptionUtils.findThrowable(e, t -> t.equals(testException)).isPresent(), is(true));
	}

	submissionFuture = dispatcherGateway.submitJob(jobGraph, TIMEOUT);

	queue.offer(Optional.empty());

	submissionFuture.get();
}
 
Example 12
Source Project: flink   File: DispatcherTest.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Tests that a failing {@link JobManagerRunner} will be properly cleaned up.
 */
@Test
public void testFailingJobManagerRunnerCleanup() throws Exception {
	final FlinkException testException = new FlinkException("Test exception.");
	final ArrayBlockingQueue<Optional<Exception>> queue = new ArrayBlockingQueue<>(2);

	dispatcher = createAndStartDispatcher(
		heartbeatServices,
		haServices,
		new BlockingJobManagerRunnerFactory(() -> {
			final Optional<Exception> take = queue.take();
			final Exception exception = take.orElse(null);

			if (exception != null) {
				throw exception;
			}
		}));

	dispatcherLeaderElectionService.isLeader(UUID.randomUUID()).get();

	final DispatcherGateway dispatcherGateway = dispatcher.getSelfGateway(DispatcherGateway.class);

	CompletableFuture<Acknowledge> submissionFuture = dispatcherGateway.submitJob(jobGraph, TIMEOUT);

	assertThat(submissionFuture.isDone(), is(false));

	queue.offer(Optional.of(testException));

	try {
		submissionFuture.get();
		fail("Should fail because we could not instantiate the JobManagerRunner.");
	} catch (Exception e) {
		assertThat(ExceptionUtils.findThrowable(e, t -> t.equals(testException)).isPresent(), is(true));
	}

	submissionFuture = dispatcherGateway.submitJob(jobGraph, TIMEOUT);

	queue.offer(Optional.empty());

	submissionFuture.get();
}