本文整理匯總了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());
}