本文整理汇总了Java中java.util.concurrent.SynchronousQueue.drainTo方法的典型用法代码示例。如果您正苦于以下问题:Java SynchronousQueue.drainTo方法的具体用法?Java SynchronousQueue.drainTo怎么用?Java SynchronousQueue.drainTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.SynchronousQueue
的用法示例。
在下文中一共展示了SynchronousQueue.drainTo方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testDrainToWithActivePut
import java.util.concurrent.SynchronousQueue; //导入方法依赖的package包/类
public void testDrainToWithActivePut(boolean fair) {
final SynchronousQueue q = new SynchronousQueue(fair);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
q.put(one);
}});
ArrayList l = new ArrayList();
long startTime = System.nanoTime();
while (l.isEmpty()) {
q.drainTo(l);
if (millisElapsedSince(startTime) > LONG_DELAY_MS)
fail("timed out");
Thread.yield();
}
assertEquals(1, l.size());
assertSame(one, l.get(0));
awaitTermination(t);
}
示例2: testDrainToN
import java.util.concurrent.SynchronousQueue; //导入方法依赖的package包/类
/**
* drainTo(c, n) empties up to n elements of queue into c
*/
public void testDrainToN() throws InterruptedException {
final SynchronousQueue q = new SynchronousQueue();
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
q.put(one);
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws InterruptedException {
q.put(two);
}});
ArrayList l = new ArrayList();
int drained;
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
assertEquals(1, drained);
assertEquals(1, l.size());
while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
assertEquals(1, drained);
assertEquals(2, l.size());
assertTrue(l.contains(one));
assertTrue(l.contains(two));
awaitTermination(t1);
awaitTermination(t2);
}
示例3: testDrainTo
import java.util.concurrent.SynchronousQueue; //导入方法依赖的package包/类
public void testDrainTo(boolean fair) {
final SynchronousQueue q = new SynchronousQueue(fair);
ArrayList l = new ArrayList();
q.drainTo(l);
assertEquals(0, q.size());
assertEquals(0, l.size());
}