本文整理汇总了Java中java.util.concurrent.Phaser类的典型用法代码示例。如果您正苦于以下问题:Java Phaser类的具体用法?Java Phaser怎么用?Java Phaser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Phaser类属于java.util.concurrent包,在下文中一共展示了Phaser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: interruptibleWriterView
import java.util.concurrent.Phaser; //导入依赖的package包/类
static Writer interruptibleWriterView(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Writer("InterruptibleWriterView") { public void run() {
if (gate != null ) toTheStartingGate(gate);
Lock wl = sl.asWriteLock();
try {
if (timeout < 0)
wl.lockInterruptibly();
else
wl.tryLock(timeout, unit);
stamp(1L); // got the lock
check(!sl.isReadLocked());
check(sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) wl.unlock(); } }};
}
示例2: runTest
import java.util.concurrent.Phaser; //导入依赖的package包/类
private static void runTest() throws Exception {
final Phaser p = new Phaser(2);
blocking = new BlockingThread(p);
blocking.start();
blocked = new BlockedThread(p);
blocked.start();
try {
blocking.join();
testOk = checkBlocked();
p.arriveAndAwaitAdvance(); // #5
} catch (InterruptedException e) {
System.err.println("Unexpected exception.");
e.printStackTrace(System.err);
throw e;
}
}
示例3: interruptibleReaderView
import java.util.concurrent.Phaser; //导入依赖的package包/类
static Reader interruptibleReaderView(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Reader("InterruptibleReaderView") { public void run() {
if (gate != null ) toTheStartingGate(gate);
final Lock rl = sl.asReadLock();
try {
if (timeout < 0)
rl.lockInterruptibly();
else
rl.tryLock(timeout, unit);
stamp(1L); // got the lock
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) rl.unlock(); } }};
}
示例4: testArrive2
import java.util.concurrent.Phaser; //导入依赖的package包/类
/**
* arriveAndDeregister does not wait for others to arrive at barrier
*/
public void testArrive2() {
final Phaser phaser = new Phaser();
assertEquals(0, phaser.register());
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 10; i++) {
assertEquals(0, phaser.register());
threads.add(newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0, phaser.arriveAndDeregister());
}}));
}
for (Thread thread : threads)
awaitTermination(thread);
assertState(phaser, 0, 1, 1);
assertEquals(0, phaser.arrive());
assertState(phaser, 1, 1, 1);
}
示例5: writerIterator
import java.util.concurrent.Phaser; //导入依赖的package包/类
static Iterator<Writer> writerIterator(final StampedLock sl,
final Phaser gate) {
return new Iterator<Writer>() {
int i = 0;
boolean view = false;
public boolean hasNext() { return true; }
public Writer next() {
switch ((i++)&7) {
case 1: case 4: case 7:
return writer(sl, gate, view ^= true);
case 2: case 5:
return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
default:
return interruptibleWriter(sl, LONG_DELAY_MS, MILLISECONDS, gate, view ^= true); }}
public void remove() {throw new UnsupportedOperationException();}};
}
示例6: checkTerminated
import java.util.concurrent.Phaser; //导入依赖的package包/类
private static void checkTerminated(final Phaser phaser) {
check(phaser.isTerminated());
int unarriverParties = phaser.getUnarrivedParties();
int registeredParties = phaser.getRegisteredParties();
int phase = phaser.getPhase();
check(phase < 0);
equal(phase, phaser.arrive());
equal(phase, phaser.arriveAndDeregister());
equal(phase, phaser.arriveAndAwaitAdvance());
equal(phase, phaser.bulkRegister(10));
equal(phase, phaser.register());
try {
equal(phase, phaser.awaitAdvanceInterruptibly(0));
equal(phase, phaser.awaitAdvanceInterruptibly(0, 10, SECONDS));
} catch (Exception ie) {
unexpected(ie);
}
equal(phaser.getUnarrivedParties(), unarriverParties);
equal(phaser.getRegisteredParties(), registeredParties);
}
示例7: arriverIterator
import java.util.concurrent.Phaser; //导入依赖的package包/类
private static Iterator<Arriver> arriverIterator(final Phaser phaser) {
return new Iterator<Arriver>() {
int i = 0;
public boolean hasNext() { return true; }
public Arriver next() {
switch ((i++)&7) {
case 0: case 4:
return arriver(phaser);
case 1: case 5:
return awaiter(phaser);
case 2: case 6: case 7:
return awaiter(phaser, -1, SECONDS);
default:
return awaiter(phaser, 30, SECONDS); }}
public void remove() {throw new UnsupportedOperationException();}};
}
示例8: interruptibleReader
import java.util.concurrent.Phaser; //导入依赖的package包/类
static Reader interruptibleReader(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Reader("InterruptibleReader") { public void run() {
if (gate != null ) toTheStartingGate(gate);
try {
if (timeout < 0)
stamp(sl.readLockInterruptibly());
else
stamp(sl.tryReadLock(timeout, unit));
check(sl.validate(stamp()));
check(sl.isReadLocked());
check(!sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) sl.unlockRead(stamp()); } }};
}
示例9: interruptibleWriter
import java.util.concurrent.Phaser; //导入依赖的package包/类
static Writer interruptibleWriter(final StampedLock sl,
final long timeout,
final TimeUnit unit,
final Phaser gate) {
return new Writer("InterruptibleWriter") { public void run() {
if (gate != null ) toTheStartingGate(gate);
try {
if (timeout < 0)
stamp(sl.writeLockInterruptibly());
else
stamp(sl.tryWriteLock(timeout, unit));
check(sl.validate(stamp()));
check(!sl.isReadLocked());
check(sl.isWriteLocked());
} catch (Throwable x) { thrown(x);
} finally { if (stamp() != 0L) sl.unlockWrite(stamp()); } }};
}
示例10: writerIterator
import java.util.concurrent.Phaser; //导入依赖的package包/类
static Iterator<Writer> writerIterator(final StampedLock sl,
final Phaser gate) {
return new Iterator<Writer>() {
int i = 0;
boolean view = false;
public boolean hasNext() { return true; }
public Writer next() {
switch ((i++)&7) {
case 1: case 4: case 7:
return writer(sl, gate, view ^= true);
case 2: case 5:
return interruptibleWriter(sl, -1, SECONDS, gate, view ^= true);
default:
return interruptibleWriter(sl, 30, SECONDS, gate, view ^= true); }}
public void remove() {throw new UnsupportedOperationException();}};
}
示例11: testAwaitAdvanceAfterInterrupt
import java.util.concurrent.Phaser; //导入依赖的package包/类
/**
* awaitAdvance continues waiting if interrupted before waiting
*/
public void testAwaitAdvanceAfterInterrupt() {
final Phaser phaser = new Phaser();
assertEquals(0, phaser.register());
final CountDownLatch pleaseArrive = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
Thread.currentThread().interrupt();
assertEquals(0, phaser.register());
assertEquals(0, phaser.arrive());
pleaseArrive.countDown();
assertTrue(Thread.currentThread().isInterrupted());
assertEquals(1, phaser.awaitAdvance(0));
assertTrue(Thread.interrupted());
}});
await(pleaseArrive);
assertThreadBlocks(t, Thread.State.WAITING);
assertEquals(0, phaser.arrive());
awaitTermination(t);
Thread.currentThread().interrupt();
assertEquals(1, phaser.awaitAdvance(0));
assertTrue(Thread.interrupted());
}
示例12: testAwaitAdvanceBeforeInterrupt
import java.util.concurrent.Phaser; //导入依赖的package包/类
/**
* awaitAdvance continues waiting if interrupted while waiting
*/
public void testAwaitAdvanceBeforeInterrupt() {
final Phaser phaser = new Phaser();
assertEquals(0, phaser.register());
final CountDownLatch pleaseArrive = new CountDownLatch(1);
Thread t = newStartedThread(new CheckedRunnable() {
public void realRun() {
assertEquals(0, phaser.register());
assertEquals(0, phaser.arrive());
assertFalse(Thread.currentThread().isInterrupted());
pleaseArrive.countDown();
assertEquals(1, phaser.awaitAdvance(0));
assertTrue(Thread.interrupted());
}});
await(pleaseArrive);
assertThreadBlocks(t, Thread.State.WAITING);
t.interrupt();
assertEquals(0, phaser.arrive());
awaitTermination(t);
Thread.currentThread().interrupt();
assertEquals(1, phaser.awaitAdvance(0));
assertTrue(Thread.interrupted());
}
示例13: checkTerminated
import java.util.concurrent.Phaser; //导入依赖的package包/类
private static void checkTerminated(final Phaser phaser) {
check(phaser.isTerminated());
int unarriverParties = phaser.getUnarrivedParties();
int registeredParties = phaser.getRegisteredParties();
int phase = phaser.getPhase();
check(phase < 0);
equal(phase, phaser.arrive());
equal(phase, phaser.arriveAndDeregister());
equal(phase, phaser.arriveAndAwaitAdvance());
equal(phase, phaser.bulkRegister(10));
equal(phase, phaser.register());
try {
equal(phase, phaser.awaitAdvanceInterruptibly(0));
equal(phase, phaser.awaitAdvanceInterruptibly(0, 10, SECONDS));
} catch (Exception ex) {
unexpected(ex);
}
equal(phaser.getUnarrivedParties(), unarriverParties);
equal(phaser.getRegisteredParties(), registeredParties);
}
示例14: test
import java.util.concurrent.Phaser; //导入依赖的package包/类
void test(String[] args) throws Throwable {
final Phaser parent = new Phaser();
final Phaser child1 = new Phaser(parent);
final Phaser child2 = new Phaser(parent);
Thread t1 = new Thread(runner(child1));
Thread t2 = new Thread(runner(child2));
t1.start();
t2.start();
for (int prevPhase = 0, phase; ; prevPhase = phase) {
phase = child2.getPhase();
check(phase >= prevPhase);
if (System.nanoTime() - quittingTimeNanos > 0) {
System.err.printf("phase=%d%n", phase);
child1.forceTermination();
break;
}
}
t1.join();
t2.join();
}
示例15: testRegisterEmptySubPhaser
import java.util.concurrent.Phaser; //导入依赖的package包/类
/**
* register on a subphaser that is currently empty succeeds, even
* in the presence of another non-empty subphaser
*/
public void testRegisterEmptySubPhaser() {
Phaser root = new Phaser();
Phaser child1 = new Phaser(root, 1);
Phaser child2 = new Phaser(root, 0);
assertEquals(0, child2.register());
assertState(root, 0, 2, 2);
assertState(child1, 0, 1, 1);
assertState(child2, 0, 1, 1);
assertEquals(0, child2.arriveAndDeregister());
assertState(root, 0, 1, 1);
assertState(child1, 0, 1, 1);
assertState(child2, 0, 0, 0);
assertEquals(0, child2.register());
assertEquals(0, child2.arriveAndDeregister());
assertState(root, 0, 1, 1);
assertState(child1, 0, 1, 1);
assertState(child2, 0, 0, 0);
assertEquals(0, child1.arriveAndDeregister());
assertTerminated(root, 1);
assertTerminated(child1, 1);
assertTerminated(child2, 1);
}