本文整理汇总了Java中java.util.concurrent.Phaser.arriveAndAwaitAdvance方法的典型用法代码示例。如果您正苦于以下问题:Java Phaser.arriveAndAwaitAdvance方法的具体用法?Java Phaser.arriveAndAwaitAdvance怎么用?Java Phaser.arriveAndAwaitAdvance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.Phaser
的用法示例。
在下文中一共展示了Phaser.arriveAndAwaitAdvance方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
}
示例2: realMain
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
private static void realMain(String[] args) throws Throwable {
final Phaser phaser = new Phaser(3);
Runnable trivialRunnable = new Runnable() {
public void run() {
phaser.arriveAndAwaitAdvance();
}
};
int count0 = Thread.activeCount();
Executor e1 = newSingleThreadExecutor();
Executor e2 = newSingleThreadExecutor(defaultThreadFactory());
e1.execute(trivialRunnable);
e2.execute(trivialRunnable);
phaser.arriveAndAwaitAdvance();
equal(Thread.activeCount(), count0 + 2);
e1 = e2 = null;
for (int i = 0; i < 10 && Thread.activeCount() > count0; i++)
tryWaitForFinalizersToRun();
for (int i = 0; i < 10; ++i) { // give JVM a chance to settle.
if (Thread.activeCount() == count0)
return;
Thread.sleep(1000);
}
equal(Thread.activeCount(), count0);
}
示例3: test
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
@Test
public void test(){
ThreadFactory threadFactory = new NamedThreadFactory(null, ScalingThreadPoolExecutorTests.class.getSimpleName(),
false);
ThreadPoolExecutor executor = new ScalingThreadPoolExecutor(0, MAX_THREADS, 0, TimeUnit.SECONDS, threadFactory);
Phaser phaser = new Phaser(1);
List<Future<?>> futures = new ArrayList<>();
for(int i = 0; i < MAX_THREADS; i++){
Assert.assertEquals(executor.getActiveCount(), i);
Assert.assertEquals(executor.getPoolSize(), i);
Assert.assertEquals(executor.getQueue().size(), 0);
phaser.register();
futures.add(executor.submit(new WaitRunnable(phaser)));
phaser.arriveAndAwaitAdvance();
}
Assert.assertEquals(executor.getActiveCount(), MAX_THREADS);
Assert.assertEquals(executor.getPoolSize(), MAX_THREADS);
Assert.assertEquals(executor.getQueue().size(), 0);
futures.add(executor.submit(new WaitRunnable(phaser)));
Assert.assertEquals(executor.getActiveCount(), MAX_THREADS);
Assert.assertEquals(executor.getPoolSize(), MAX_THREADS);
Assert.assertEquals(executor.getQueue().size(), 1);
futures.add(executor.submit(new WaitRunnable(phaser)));
Assert.assertEquals(executor.getActiveCount(), MAX_THREADS);
Assert.assertEquals(executor.getPoolSize(), MAX_THREADS);
Assert.assertEquals(executor.getQueue().size(), 2);
phaser.arrive();
FutureTool.getAllVaried(futures);
Assert.assertEquals(executor.getActiveCount(), 0);
Assert.assertEquals(executor.getCompletedTaskCount(), MAX_THREADS + 2);
Assert.assertEquals(executor.getQueue().size(), 0);
executor.shutdownNow();
}
示例4: testBlockingOnSimpleMonitor
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
/**
* Tests that blocking on a single monitor properly increases the
* blocked count at least by 1. Also asserts that the correct lock name is provided.
*/
private static void testBlockingOnSimpleMonitor() throws Exception {
System.out.println("testBlockingOnSimpleMonitor");
final Object lock1 = new Object();
System.out.println("Lock1 = " + lock1);
final Phaser p = new Phaser(2);
LockerThread lt = newLockerThread(new Runnable() {
@Override
public void run() {
p.arriveAndAwaitAdvance(); // phase[1]
synchronized(lock1) {
System.out.println("[LockerThread obtained Lock1]");
p.arriveAndAwaitAdvance(); // phase[2]
}
p.arriveAndAwaitAdvance(); // phase[3]
}
});
lt.start();
long tid = lt.getId();
ThreadInfo ti = mbean.getThreadInfo(tid);
String lockName = null;
synchronized(lock1) {
p.arriveAndAwaitAdvance(); // phase[1]
waitForThreadState(lt, Thread.State.BLOCKED);
do {
lockName = mbean.getThreadInfo(tid).getLockName();
} while (lockName == null);
}
p.arriveAndAwaitAdvance(); // phase[2]
testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);
p.arriveAndDeregister(); // phase[3]
lt.join();
printok();
}
示例5: testWaitingOnSimpleMonitor
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
/**
* Tests that waiting on a single monitor properly increases the waited
* count by 1 and the waited time by a positive number.
*/
private static void testWaitingOnSimpleMonitor() throws Exception {
System.out.println("testWaitingOnSimpleMonitor");
final Object lock1 = new Object();
final Phaser p = new Phaser(2);
LockerThread lt = newLockerThread(new Runnable() {
@Override
public void run() {
p.arriveAndAwaitAdvance(); // phase[1]
synchronized(lock1) {
System.out.println("[LockerThread obtained Lock1]");
try {
lock1.wait(300);
} catch (InterruptedException ex) {
// ignore
}
p.arriveAndAwaitAdvance(); // phase[2]
}
p.arriveAndAwaitAdvance(); // phase[3]
}
});
lt.start();
ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
synchronized(lock1) {
p.arriveAndAwaitAdvance(); // phase[1]
waitForThreadState(lt, Thread.State.BLOCKED);
}
p.arriveAndAwaitAdvance(); // phase[2]
testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 1);
p.arriveAndDeregister(); // phase[3]
lt.join();
printok();
}
示例6: toTheStartingGate
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
static void toTheStartingGate(Phaser gate) {
try {
gate.arriveAndAwaitAdvance();
} catch (Throwable t) {
unexpected(t);
}
}
示例7: timeTasks
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
static long timeTasks(Phaser phaser) {
phaser.arriveAndAwaitAdvance();
long start = System.nanoTime();
phaser.arriveAndAwaitAdvance();
phaser.arriveAndAwaitAdvance();
return System.nanoTime() - start;
}
示例8: main
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
try (ServerSocket ss = new ServerSocket(0)) {
final int port = ss.getLocalPort();
final Phaser phaser = new Phaser(THREADS + 1);
for (int i=0; i<100; i++) {
final Socket s = new Socket("localhost", port);
s.setSoLinger(false, 0);
try (Socket sa = ss.accept()) {
sa.setSoLinger(false, 0);
final InputStream is = s.getInputStream();
Thread[] threads = new Thread[THREADS];
for (int j=0; j<THREADS; j++) {
threads[j] = new Thread() {
public void run() {
try {
phaser.arriveAndAwaitAdvance();
while (is.read() != -1)
Thread.sleep(50);
} catch (Exception x) {
if (!(x instanceof SocketException
&& x.getMessage().equalsIgnoreCase("socket closed")))
x.printStackTrace();
// ok, expect Socket closed
}
}};
}
for (int j=0; j<100; j++)
threads[j].start();
phaser.arriveAndAwaitAdvance();
s.close();
for (int j=0; j<100; j++)
threads[j].join();
}
}
}
}
示例9: main
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/test/InfiniteLoop", new RespHandler());
server.start();
try {
InetSocketAddress address = server.getAddress();
URL url = new URL("http://localhost:" + address.getPort()
+ "/test/InfiniteLoop");
final Phaser phaser = new Phaser(2);
for (int i=0; i<10; i++) {
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
final InputStream is = uc.getInputStream();
final Thread thread = new Thread() {
public void run() {
try {
phaser.arriveAndAwaitAdvance();
while (is.read() != -1)
Thread.sleep(50);
} catch (Exception x) { x.printStackTrace(); }
}};
thread.start();
phaser.arriveAndAwaitAdvance();
is.close();
System.out.println("returned from close");
thread.join();
}
} finally {
server.stop(0);
}
}
示例10: testArriveAndAwaitAdvance1
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
/**
* arriveAndAwaitAdvance throws IllegalStateException with no
* unarrived parties
*/
public void testArriveAndAwaitAdvance1() {
Phaser phaser = new Phaser();
try {
phaser.arriveAndAwaitAdvance();
shouldThrow();
} catch (IllegalStateException success) {}
}
示例11: reporterFinalFlush
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
@Test
public void reporterFinalFlush() {
Phaser phaser = new Phaser(1);
final MockM3Server server = new MockM3Server(phaser, true, socketAddress);
Thread serverThread = new Thread(new Runnable() {
@Override
public void run() {
server.serve();
}
});
try {
serverThread.start();
M3Reporter reporter = new M3Reporter.Builder(socketAddress)
.service("test-service")
.commonTags(DEFAULT_TAGS)
.maxQueueSize(MAX_QUEUE_SIZE)
.maxPacketSizeBytes(MAX_PACKET_SIZE_BYTES)
.build();
phaser.register();
reporter.reportTimer("final-flush-timer", null, Duration.ofMillis(10));
reporter.close();
phaser.arriveAndAwaitAdvance();
List<MetricBatch> batches = server.getService().getBatches();
assertEquals(1, batches.size());
assertNotNull(batches.get(0));
assertEquals(1, batches.get(0).getMetrics().size());
} finally {
server.close();
}
}
示例12: testBlockingOnNestedMonitor
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
/**
* Tests that blocking on a nested monitor properly increases the
* blocked count at least by 1 - it is not affected by the nesting depth.
* Also asserts that the correct lock name is provided.
*/
private static void testBlockingOnNestedMonitor() throws Exception {
System.out.println("testBlockingOnNestedMonitor");
final Object lock1 = new Object();
final Object lock2 = new Object();
System.out.println("Lock1 = " + lock1);
System.out.println("Lock2 = " + lock2);
final Phaser p = new Phaser(2);
LockerThread lt = newLockerThread(new Runnable() {
@Override
public void run() {
p.arriveAndAwaitAdvance(); // phase[1]
synchronized(lock1) {
System.out.println("[LockerThread obtained Lock1]");
p.arriveAndAwaitAdvance(); // phase[2]
p.arriveAndAwaitAdvance(); // phase[3]
synchronized(lock2) {
System.out.println("[LockerThread obtained Lock2]");
p.arriveAndAwaitAdvance(); // phase[4]
}
p.arriveAndAwaitAdvance(); // phase[5]
}
}
});
lt.start();
long tid = lt.getId();
ThreadInfo ti = mbean.getThreadInfo(tid);
String lockName = null;
synchronized(lock1) {
p.arriveAndAwaitAdvance(); // phase[1]
waitForThreadState(lt, Thread.State.BLOCKED);
do {
lockName = mbean.getThreadInfo(tid).getLockName();
} while (lockName == null);
}
p.arriveAndAwaitAdvance(); // phase[2]
ti = testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock1);
synchronized(lock2) {
p.arriveAndAwaitAdvance(); // phase [3]
waitForThreadState(lt, Thread.State.BLOCKED);
do {
lockName = mbean.getThreadInfo(tid).getLockName();
} while (lockName == null);
}
p.arriveAndAwaitAdvance(); // phase [4]
testBlocked(ti, () -> mbean.getThreadInfo(tid), lockName, lock2);
p.arriveAndDeregister();
lt.join();
printok();
}
示例13: testMultiWaitingOnSimpleMonitor
import java.util.concurrent.Phaser; //导入方法依赖的package包/类
/**
* Tests that waiting multiple times on the same monitor subsequently
* increases the waited count by the number of subsequent calls and the
* waited time by a positive number.
*/
private static void testMultiWaitingOnSimpleMonitor() throws Exception {
System.out.println("testWaitingOnMultipleMonitors");
final Object lock1 = new Object();
final Phaser p = new Phaser(2);
LockerThread lt = newLockerThread(new Runnable() {
@Override
public void run() {
p.arriveAndAwaitAdvance(); // phase[1]
synchronized(lock1) {
System.out.println("[LockerThread obtained Lock1]");
for (int i = 0; i < 3; i++) {
try {
lock1.wait(300);
} catch (InterruptedException ex) {
// ignore
}
p.arriveAndAwaitAdvance(); // phase[2-4]
}
}
p.arriveAndAwaitAdvance(); // phase[5]
}
});
lt.start();
ThreadInfo ti1 = mbean.getThreadInfo(lt.getId());
synchronized(lock1) {
p.arriveAndAwaitAdvance(); //phase[1]
waitForThreadState(lt, Thread.State.BLOCKED);
}
int phase = p.getPhase();
while ((p.arriveAndAwaitAdvance() - phase) < 3); // phase[2-4]
testWaited(ti1, () -> mbean.getThreadInfo(lt.getId()), 3);
p.arriveAndDeregister(); // phase[5]
lt.join();
printok();
}