本文整理汇总了Java中java.util.concurrent.BlockingQueue.add方法的典型用法代码示例。如果您正苦于以下问题:Java BlockingQueue.add方法的具体用法?Java BlockingQueue.add怎么用?Java BlockingQueue.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.BlockingQueue
的用法示例。
在下文中一共展示了BlockingQueue.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: output
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Output the items in the stream with a function.
* This method will block until the result returns.
* Eg. `output(StatisticOperators.count())` will output the number of items.
*
* @param itemsCollector the function used to output current stream
* @param <Tout> the type of the result
* @return the result
* @throws PSException if failed to the result.
*/
@PSAction(blocking = true)
public <Tout> Tout output(Function<List<Item>, Tout> itemsCollector) throws PSException {
final BlockingQueue<Object> resultQueue = new LinkedBlockingQueue<>();
Callback<Tout> resultHandler = new Callback<Tout>() {
@Override
protected void onInput(Tout input) {
resultQueue.add(input);
}
@Override
protected void onFail(PSException exception) {
resultQueue.add(exception);
}
};
this.output(itemsCollector, resultHandler);
try {
Object resultOrException = resultQueue.take();
if (resultOrException instanceof PSException) {
throw (PSException) resultOrException;
}
return (Tout) resultOrException;
} catch (InterruptedException e) {
throw PSException.INTERRUPTED(e.getMessage());
}
}
示例2: processMessage
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
private void processMessage(String taskId, SimpleMessage message, boolean request)
{
synchronized( stateMutex )
{
if( request )
{
Task task = runningTasks.get(taskId);
if( task != null )
{
task.postMessage(message);
}
}
else
{
BlockingQueue<SimpleMessage> waitingQueue = messageResponses.getIfPresent(message.getMessageId());
if( waitingQueue != null )
{
messageResponses.invalidate(message.getMessageId());
waitingQueue.add(message);
}
}
}
}
示例3: testDelayQueue
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
void testDelayQueue(final BlockingQueue q) throws Throwable {
System.err.println(q.getClass().getSimpleName());
for (int i = 0; i < CAPACITY; i++)
q.add(new PDelay(i));
ArrayBlockingQueue q2 = new ArrayBlockingQueue(SMALL);
try {
q.drainTo(q2, SMALL + 3);
fail("should throw");
} catch (IllegalStateException success) {
equal(SMALL, q2.size());
equal(new PDelay(0), q2.poll());
equal(new PDelay(1), q2.poll());
check(q2.isEmpty());
for (int i = SMALL; i < CAPACITY; i++)
equal(new PDelay(i), q.poll());
equal(0, q.size());
}
}
示例4: main
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
queue.add("add");
//
String peek = queue.peek();
System.out.println(peek);
queue.poll();
boolean offer = queue.offer("offer");
if (!offer) {
System.out.println("it is't possible to do so immediately");
}
System.out.println(queue.take());
queue.put("put operation");
System.out.println(queue.take());
}
示例5: test06
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Test
public void test06() throws Exception {
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
queue.add("1");
queue.add("2");
queue.add("3");
System.out.println(queue.poll());
queue.add("4");
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
}
示例6: spawnNewTask
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
private static void spawnNewTask(StateContext context, BlockingQueue<Continuation> workQueue) {
Continuation cont = RuntimeCallInitialiser.forState(context)
.newCall(FN_INSTANCE, nextId.getAndIncrement(), workQueue);
assert (cont != null);
numLive.incrementAndGet();
workQueue.add(cont);
}
示例7: addsAndRemoves
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Benchmark void addsAndRemoves(int reps) {
int capacity = this.capacity;
BlockingQueue<String> queue = this.queue;
String[] strings = this.strings;
for (int i = 0; i < reps; i++) {
for (int j = 0; j < capacity; j++) {
queue.add(strings[j]);
}
for (int j = 0; j < capacity; j++) {
queue.remove();
}
}
}
示例8: processResponse
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
private boolean processResponse(SimpleMessage message)
{
BlockingQueue<SimpleMessage> waitingQueue = messageResponses.getIfPresent(message.getMessageId());
if( waitingQueue != null )
{
messageResponses.invalidate(message.getMessageId());
waitingQueue.add(message);
return true;
}
return false;
}
示例9: addWaiter
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
public synchronized void addWaiter(BlockingQueue<Object> queue)
{
if( finished )
{
queue.add(this);
}
else
{
waitingList.put(queue, queue);
}
}
示例10: finished
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
public synchronized void finished()
{
finished = true;
locator.clearCallable();
for( BlockingQueue<Object> waiter : waitingList.keySet() )
{
waiter.add(this);
}
}
示例11: Drone
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Creates a drone based on a sprite
* @param uiUpdates - uiupdates
* @param image - Path to the sprite image
*/
Drone(BlockingQueue<UIUpdate> uiUpdates, String image) {
super(uiUpdates, image);
new SpriteAnimation(imageView, Duration.millis(200), DRONE_SPRITE_COLUMNS, DRONE_SPRITE_COLUMNS, 0, 0, Settings.SPRITE_WIDTH, Settings.SPRITE_HEIGTH).play();
heightText = new Text(0, 20, "Height: 0");
heightText.setFill(Color.WHITE);
imageView.setFitHeight(Settings.DRONE_HEIGHT);
imageView.setFitWidth(Settings.DRONE_WIDTH);
imageView.setId("drone");
currentHP = -1;
uiUpdates.add(new AddDrone(heightText));
}
示例12: createTempQueueAndAddEvent
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
private BlockingQueue<GatewaySenderEventImpl> createTempQueueAndAddEvent(
ParallelGatewaySenderEventProcessor pgsep, GatewaySenderEventImpl gsei) {
ParallelGatewaySenderQueue pgsq = (ParallelGatewaySenderQueue) pgsep.getQueue();
Map<Integer, BlockingQueue<GatewaySenderEventImpl>> tempQueueMap =
pgsq.getBucketToTempQueueMap();
BlockingQueue<GatewaySenderEventImpl> tempQueue = new LinkedBlockingQueue();
when(gsei.getShadowKey()).thenReturn(KEY);
tempQueue.add(gsei);
tempQueueMap.put(BUCKET_ID, tempQueue);
return tempQueue;
}
示例13: testEmptyFull
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Queue transitions from empty to full when elements added
*/
public void testEmptyFull() {
BlockingQueue q = populatedQueue(0, 2, 2, false);
assertTrue(q.isEmpty());
assertEquals(2, q.remainingCapacity());
q.add(one);
assertFalse(q.isEmpty());
assertTrue(q.offer(two));
assertFalse(q.isEmpty());
assertEquals(0, q.remainingCapacity());
assertFalse(q.offer(three));
}
示例14: testRemoveElement
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* remove(x) removes x and returns true if present
* TODO: move to superclass CollectionTest.java
*/
public void testRemoveElement() {
final BlockingQueue q = emptyCollection();
final int size = Math.min(q.remainingCapacity(), SIZE);
final Object[] elts = new Object[size];
assertFalse(q.contains(makeElement(99)));
assertFalse(q.remove(makeElement(99)));
checkEmpty(q);
for (int i = 0; i < size; i++)
q.add(elts[i] = makeElement(i));
for (int i = 1; i < size; i += 2) {
for (int pass = 0; pass < 2; pass++) {
assertEquals((pass == 0), q.contains(elts[i]));
assertEquals((pass == 0), q.remove(elts[i]));
assertFalse(q.contains(elts[i]));
assertTrue(q.contains(elts[i - 1]));
if (i < size - 1)
assertTrue(q.contains(elts[i + 1]));
}
}
if (size > 0)
assertTrue(q.contains(elts[0]));
for (int i = size - 2; i >= 0; i -= 2) {
assertTrue(q.contains(elts[i]));
assertFalse(q.contains(elts[i + 1]));
assertTrue(q.remove(elts[i]));
assertFalse(q.contains(elts[i]));
assertFalse(q.remove(elts[i + 1]));
assertFalse(q.contains(elts[i + 1]));
}
checkEmpty(q);
}
示例15: convertVideoToImages
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Converts the specified video file to a series of images and adds them to
* the given shared queue.
*
* @param f the video file
* @param singleImage whether just a single preview image is required
* @param sharedQueue the shared processing queue
* @throws IOException if there is a problem loading the media file
* @throws InterruptedException if loading is interupted
*/
public void convertVideoToImages(File f, boolean singleImage, final BlockingQueue<Image> sharedQueue, VideoLoadedLock videoLoadedLock) throws IOException, InterruptedException {
EmbeddedMediaPlayerComponent mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
JWindow frame = new JWindow();
frame.setBounds(0,0,1,1);
frame.add(mediaPlayerComponent);
frame.setVisible(true);
final MediaPlayer player = mediaPlayerComponent.getMediaPlayer();
player.mute();
player.playMedia(f.getAbsolutePath());
long len = player.getLength();
if (len == 0) {
Thread.sleep(MEDIA_PRELOAD_WAIT);
len = player.getLength();
}
int singleImageSelectionTime = -1;
if (singleImage) {
singleImageSelectionTime = new Random().nextInt(RANDOM_INTRO_WAIT) + MINIMUM_INTRO_WAIT;
if (singleImageSelectionTime > len) {
singleImageSelectionTime = (int) len-1;
}
}
frame.setVisible(false);
log.debug("Player time: {}, Len: {}", player.getTime(), len);
videoLoadedLock.preloadFinished();
try {
while (player.getTime() < len) {
if (cancel) {
break;
}
// Always add images if we don't want a single preview
// image or when the time exceeds the point we want a
// single image from
if (singleImageSelectionTime == -1 || player.getTime() >= singleImageSelectionTime) {
sharedQueue.add(player.getSnapshot());
// We only want one image if single image selection
// is turned on
if (singleImageSelectionTime != -1 && player.getTime() >= singleImageSelectionTime) {
break;
}
}
}
} finally {
player.stop();
player.release();
mediaPlayerComponent.release();
cancel = false;
}
}