本文整理了Java中java.util.concurrent.BlockingQueue.put()
方法的一些代码示例,展示了BlockingQueue.put()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BlockingQueue.put()
方法的具体详情如下:
包路径:java.util.concurrent.BlockingQueue
类名称:BlockingQueue
方法名:put
[英]Inserts the specified element into this queue, waiting if necessary for space to become available.
[中]
代码示例来源:origin: stackoverflow.com
class MyHouse {
final BlockingQueue<Object> queue = new LinkedBlockingQueue<>();
void eatFood() throws InterruptedException {
// take next item from the queue (sleeps while waiting)
Object food = queue.take();
// and do something with it
System.out.println("Eating: " + food);
}
void deliverPizza() throws InterruptedException {
// in producer threads, we push items on to the queue.
// if there is space in the queue we can return immediately;
// the consumer thread(s) will get to it later
queue.put("A delicious pizza");
}
}
代码示例来源:origin: stanfordnlp/CoreNLP
/**
* Wait for all threads to finish.
*
* @param destroyThreadpool -- if true, then destroy the worker threads
* so that the main thread can shutdown.
*/
public void join(boolean destroyThreadpool) {
// Make blocking calls to the last processes that are running
if ( ! threadPool.isShutdown()) {
try {
for (int i = nThreads; i > 0; --i) {
idleProcessors.take();
}
if (destroyThreadpool) {
threadPool.shutdown();
// Sanity check. The threadpool should be done after iterating over
// the processors.
threadPool.awaitTermination(10, TimeUnit.SECONDS);
} else {
// Repopulate the list of processors
for (int i = 0; i < nThreads; ++i) {
idleProcessors.put(i);
}
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
代码示例来源:origin: apache/geode
@Override
public void run() {
try {
for (;;) {
SystemFailure.checkFailure();
Runnable job = takeQueue.take();
putQueue.put(job);
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
// this thread is being shutdown so just return;
return;
}
}
};
代码示例来源:origin: loklak/loklak_server
@Override
public MessageDigest call() {
try {
filechunk c;
while(true) {
c = this.filed.take();
if (c == poison) break;
this.digest.update(c.b, 0, c.n);
this.empty.put(c);
}
} catch (final InterruptedException e) {
DAO.severe(e);
}
return this.digest;
}
代码示例来源:origin: apache/geode
@Override
public void run() {
try {
for (;;) {
SystemFailure.checkFailure();
Runnable task = takeQueue.take();
if (forFnExec) {
// In the function case, offer the request to the work queue.
// If it fails, execute it anyway. This will cause the RejectedExecutionHandler to
// spin off a thread for it.
if (!putQueue.offer(task, OFFER_TIME, TimeUnit.MILLISECONDS)) {
execute(task);
}
} else {
// In the non-function case, put the request on the work queue.
putQueue.put(task);
}
}
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
// this thread is being shutdown so just return;
return;
}
}
};
代码示例来源:origin: stackoverflow.com
public void run() {
for (int i = 0; i < n; i++)
try { q.put(i); } catch (InterruptedException ex) {}
long sum = 0;
for (int i = 0; i < n; i++)
try { sum += q.take(); } catch (InterruptedException ex) {}
return sum;
代码示例来源:origin: LeonardoZ/java-concurrency-patterns
while (!Thread.currentThread().isInterrupted()) {
try {
UUID uuid = uuidQueue.take();
System.out.println("Consumed: " + uuid + " by " + Thread.currentThread().getName());
UUID randomUUID = UUID.randomUUID();
System.out.println("Produced: " + randomUUID + " by " + Thread.currentThread().getName());
uuidQueue.put(randomUUID);
代码示例来源:origin: spring-projects/spring-batch
value = results.take();
if (isContinuable(value)) {
results.put(value);
synchronized (lock) {
while (count > results.size()) {
lock.wait();
value = results.take();
count--;
代码示例来源:origin: stackoverflow.com
while ((line = br.readLine()) != null) {
queue.put(line);
try {
line = queue.take();
代码示例来源:origin: geoserver/geoserver
Object o;
while (!completed
&& (o = sourceQueue.take())
!= TERMINATOR) {
Resource r = (Resource) o;
T mapped = mapper.apply(r);
if (mapped != null) {
queue.put(mapped);
Long.MAX_VALUE, TimeUnit.MILLISECONDS);
queue.put(TERMINATOR);
} catch (InterruptedException e) {
LOGGER.log(
代码示例来源:origin: apache/ignite
/**
* @param q Queue to test.
* @throws InterruptedException If interrupted.
*/
private static long testBlockingQueue(BlockingQueue<Object> q) throws InterruptedException {
GridTimer timer = new GridTimer("blocking-queue");
for (int i = 0; i < CNT; i++)
q.put(new Object());
for (int i = 0; i < CNT; i++) {
Object o = q.take();
assert o != null;
}
timer.stop();
return timer.duration();
}
}
代码示例来源:origin: neo4j/neo4j
@Test
public void shouldWaitForOngoingForceToCompleteBeforeForcingAgain() throws Throwable
{
channelCommandQueue.put( ChannelCommand.dummy );
// The 'emptyBuffer...' command will be put into the queue, and then it'll block on 'force' because the queue
// will be at capacity.
final BatchingTransactionAppender appender = life.add( createTransactionAppender() );
life.start();
Runnable runnable = createForceAfterAppendRunnable( appender );
Future<?> future = executor.submit( runnable );
forceSemaphore.acquire();
Thread otherThread = fork( runnable );
awaitThreadState( otherThread, MILLISECONDS_TO_WAIT, Thread.State.TIMED_WAITING );
assertThat( channelCommandQueue.take(), is( ChannelCommand.dummy ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) );
future.get();
otherThread.join();
assertTrue( channelCommandQueue.isEmpty() );
}
代码示例来源:origin: neo4j/neo4j
@Test
public void shouldBatchUpMultipleWaitingForceRequests() throws Throwable
channelCommandQueue.put( ChannelCommand.dummy );
assertThat( channelCommandQueue.take(), is( ChannelCommand.dummy ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) );
assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) );
代码示例来源:origin: azkaban/azkaban
@Test
public void testExplicitlySpecifiedPriorities() throws IOException,
InterruptedException {
final ExecutableFlow flow1 = createExecutableFlow("exec1", 5, 3, 1);
final ExecutableFlow flow2 = createExecutableFlow("exec2", 6, 3, 2);
final ExecutableFlow flow3 = createExecutableFlow("exec3", 2, 3, 3);
final ExecutionReference dummyRef = new ExecutionReference(0);
final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue =
new PriorityBlockingQueue<>(10,
new ExecutableFlowPriorityComparator());
queue.put(new Pair<>(dummyRef, flow1));
queue.put(new Pair<>(dummyRef, flow2));
queue.put(new Pair<>(dummyRef, flow3));
Assert.assertEquals(flow2, queue.take().getSecond());
Assert.assertEquals(flow1, queue.take().getSecond());
Assert.assertEquals(flow3, queue.take().getSecond());
}
代码示例来源:origin: azkaban/azkaban
@Test
public void testMixedSpecifiedPriorities() throws IOException,
InterruptedException {
final ExecutableFlow flow1 = createExecutableFlow("exec1", 3, 3, 1);
final ExecutableFlow flow2 = createExecutableFlow("exec2", 2, 3, 2);
final ExecutableFlow flow3 = createExecutableFlow("exec3", -2, 3, 3);
final ExecutionReference dummyRef = new ExecutionReference(0);
final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue =
new PriorityBlockingQueue<>(10,
new ExecutableFlowPriorityComparator());
queue.put(new Pair<>(dummyRef, flow1));
queue.put(new Pair<>(dummyRef, flow2));
queue.put(new Pair<>(dummyRef, flow3));
Assert.assertEquals(flow3, queue.take().getSecond());
Assert.assertEquals(flow1, queue.take().getSecond());
Assert.assertEquals(flow2, queue.take().getSecond());
}
代码示例来源:origin: azkaban/azkaban
@Test
public void testEqualPriorities() throws IOException, InterruptedException {
final ExecutableFlow flow1 = createExecutableFlow("exec1", 3, 1, 1);
final ExecutableFlow flow2 = createExecutableFlow("exec2", 2, 2, 2);
final ExecutableFlow flow3 = createExecutableFlow("exec3", -2, 3, 3);
final ExecutableFlow flow4 = createExecutableFlow("exec3", 3, 4, 4);
final ExecutionReference dummyRef = new ExecutionReference(0);
final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue =
new PriorityBlockingQueue<>(10,
new ExecutableFlowPriorityComparator());
queue.put(new Pair<>(dummyRef, flow4));
queue.put(new Pair<>(dummyRef, flow1));
queue.put(new Pair<>(dummyRef, flow2));
queue.put(new Pair<>(dummyRef, flow3));
Assert.assertEquals(flow3, queue.take().getSecond());
Assert.assertEquals(flow1, queue.take().getSecond());
Assert.assertEquals(flow4, queue.take().getSecond());
Assert.assertEquals(flow2, queue.take().getSecond());
}
代码示例来源:origin: azkaban/azkaban
@Test
public void testEqualUpdateTimeAndPriority() throws IOException,
InterruptedException {
final ExecutableFlow flow1 = createExecutableFlow("exec1", 3, 1, 1);
final ExecutableFlow flow2 = createExecutableFlow("exec2", 2, 2, 2);
final ExecutableFlow flow3 = createExecutableFlow("exec3", -2, 2, 3);
final ExecutableFlow flow4 = createExecutableFlow("exec3", 3, 4, 4);
final ExecutionReference dummyRef = new ExecutionReference(0);
final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue =
new PriorityBlockingQueue<>(10,
new ExecutableFlowPriorityComparator());
queue.put(new Pair<>(dummyRef, flow4));
queue.put(new Pair<>(dummyRef, flow1));
queue.put(new Pair<>(dummyRef, flow2));
queue.put(new Pair<>(dummyRef, flow3));
Assert.assertEquals(flow3, queue.take().getSecond());
Assert.assertEquals(flow1, queue.take().getSecond());
Assert.assertEquals(flow4, queue.take().getSecond());
Assert.assertEquals(flow2, queue.take().getSecond());
}
}
代码示例来源:origin: mcxiaoke/android-volley
try {
request = mCacheQueue.take();
} catch (InterruptedException e) {
request.addMarker("cache-miss");
mNetworkQueue.put(request);
continue;
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
代码示例来源:origin: chentao0707/SimplifyReader
final Request<?> request = mCacheQueue.take();
request.addMarker("cache-queue-take");
request.addMarker("cache-miss");
mNetworkQueue.put(request);
continue;
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
代码示例来源:origin: jiangqqlmj/FastDev4Android
final Request<?> request = mCacheQueue.take();
request.addMarker("cache-queue-take");
request.addMarker("cache-miss");
mNetworkQueue.put(request);
continue;
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
内容来源于网络,如有侵权,请联系作者删除!