本文整理汇总了Java中java.util.concurrent.CyclicBarrier.reset方法的典型用法代码示例。如果您正苦于以下问题:Java CyclicBarrier.reset方法的具体用法?Java CyclicBarrier.reset怎么用?Java CyclicBarrier.reset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.CyclicBarrier
的用法示例。
在下文中一共展示了CyclicBarrier.reset方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: makeBarrier
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
synchronized void makeBarrier() {
if (numPlayers < 1) {
log.warning("cannot make barrier for " + numPlayers + " viewers - something is wrong");
log.warning("disabling sychronized playback because probably multiple viewers are active but we are not playing set of sychronized files");
outer.getToggleSyncEnabledAction().actionPerformed(null);
// toggle all the viewers syncenabled menu item
// JOptionPane.showMessageDialog(null,"Disabled sychronized playback because files are not part of sychronized set");
return;
}
log.info("making barrier for " + this);
barrier = new CyclicBarrier(numPlayers, new Runnable() {
public void run() {
// this is run after await synchronization; it updates the time to read events from each AEInputStream
// log.info(Thread.currentThread()+" resetting barrier");
barrier.reset();
setTime(getTime() + getTimesliceUs());
}
});
}
示例2: testReset_BrokenBarrier
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
/**
* A reset of an active barrier causes waiting threads to throw
* BrokenBarrierException
*/
public void testReset_BrokenBarrier() throws InterruptedException {
final CyclicBarrier c = new CyclicBarrier(3);
final CountDownLatch pleaseReset = new CountDownLatch(2);
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
public void realRun() throws Exception {
pleaseReset.countDown();
c.await();
}};
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
public void realRun() throws Exception {
pleaseReset.countDown();
c.await();
}};
t1.start();
t2.start();
await(pleaseReset);
awaitNumberWaiting(c, 2);
c.reset();
awaitTermination(t1);
awaitTermination(t2);
}
示例3: testReset_NoBrokenBarrier
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
/**
* A reset before threads enter barrier does not throw
* BrokenBarrierException
*/
public void testReset_NoBrokenBarrier() throws Exception {
final CyclicBarrier c = new CyclicBarrier(3);
c.reset();
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
c.await();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
c.await();
}});
c.await();
awaitTermination(t1);
awaitTermination(t2);
}
示例4: testResetAfterCommandException
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
/**
* Reset of a barrier after a failed command reinitializes it.
*/
public void testResetAfterCommandException() throws Exception {
final CyclicBarrier barrier =
new CyclicBarrier(3, new Runnable() {
public void run() {
throw new NullPointerException(); }});
for (int i = 0; i < 2; i++) {
final CyclicBarrier start = new CyclicBarrier(3);
Thread t1 = new ThreadShouldThrow(BrokenBarrierException.class) {
public void realRun() throws Exception {
start.await();
barrier.await();
}};
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
public void realRun() throws Exception {
start.await();
barrier.await();
}};
t1.start();
t2.start();
start.await();
awaitNumberWaiting(barrier, 2);
try {
barrier.await();
shouldThrow();
} catch (NullPointerException success) {}
awaitTermination(t1);
awaitTermination(t2);
assertTrue(barrier.isBroken());
assertEquals(0, barrier.getNumberWaiting());
barrier.reset();
assertFalse(barrier.isBroken());
assertEquals(0, barrier.getNumberWaiting());
}
}
示例5: testResetWithoutBreakage
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
/**
* Reset of a non-broken barrier does not break barrier
*/
public void testResetWithoutBreakage() throws Exception {
final CyclicBarrier barrier = new CyclicBarrier(3);
for (int i = 0; i < 3; i++) {
final CyclicBarrier start = new CyclicBarrier(3);
Thread t1 = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
start.await();
barrier.await();
}});
Thread t2 = newStartedThread(new CheckedRunnable() {
public void realRun() throws Exception {
start.await();
barrier.await();
}});
start.await();
barrier.await();
awaitTermination(t1);
awaitTermination(t2);
assertFalse(barrier.isBroken());
assertEquals(0, barrier.getNumberWaiting());
if (i == 1) barrier.reset();
assertFalse(barrier.isBroken());
assertEquals(0, barrier.getNumberWaiting());
}
}
示例6: testResetAfterInterrupt
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
/**
* Reset of a barrier after interruption reinitializes it.
*/
public void testResetAfterInterrupt() throws Exception {
final CyclicBarrier barrier = new CyclicBarrier(3);
for (int i = 0; i < 2; i++) {
final CyclicBarrier start = new CyclicBarrier(3);
Thread t1 = new ThreadShouldThrow(InterruptedException.class) {
public void realRun() throws Exception {
start.await();
barrier.await();
}};
Thread t2 = new ThreadShouldThrow(BrokenBarrierException.class) {
public void realRun() throws Exception {
start.await();
barrier.await();
}};
t1.start();
t2.start();
start.await();
t1.interrupt();
awaitTermination(t1);
awaitTermination(t2);
assertTrue(barrier.isBroken());
assertEquals(0, barrier.getNumberWaiting());
barrier.reset();
assertFalse(barrier.isBroken());
assertEquals(0, barrier.getNumberWaiting());
}
}
示例7: forceUnblockAllExecutors
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
@After
public void forceUnblockAllExecutors() {
for (CyclicBarrier barrier: blockedExecutors) {
barrier.reset();
}
}
示例8: main
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
final int NUMBER_OF_JAVA_PRIORITIES =
Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
final CyclicBarrier barrier =
new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);
for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
final int priority = p;
new Thread("Priority=" + p) {
{
setPriority(priority);
}
public void run() {
try {
barrier.await(); // 1st
barrier.await(); // 2nd
} catch (Exception exc) {
// ignore
}
}
}.start();
}
barrier.await(); // 1st
int matches = 0;
List<String> failed = new ArrayList<>();
try {
String pid = getPid();
String jstack = System.getProperty("java.home") + "/../bin/jstack";
Process process = new ProcessBuilder(jstack, pid)
.redirectErrorStream(true).start();
Pattern pattern = Pattern.compile(
"\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while((line = reader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
matches += 1;
String expected = matcher.group(1);
String actual = matcher.group(2);
if (!expected.equals(actual)) {
failed.add(line);
}
}
}
}
barrier.await(); // 2nd
} finally {
barrier.reset();
}
if (matches != NUMBER_OF_JAVA_PRIORITIES) {
throw new AssertionError("matches: expected " +
NUMBER_OF_JAVA_PRIORITIES + ", but was " + matches);
}
if (!failed.isEmpty()) {
throw new AssertionError(failed.size() + ":" + failed);
}
System.out.println("Test passes.");
}
示例9: main
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
public static void main(String[] args) throws Throwable {
final int NUMBER_OF_JAVA_PRIORITIES =
Thread.MAX_PRIORITY - Thread.MIN_PRIORITY + 1;
final CyclicBarrier barrier =
new CyclicBarrier(NUMBER_OF_JAVA_PRIORITIES + 1);
for (int p = Thread.MIN_PRIORITY; p <= Thread.MAX_PRIORITY; ++p) {
final int priority = p;
new Thread("Priority=" + p) {
{
setPriority(priority);
}
public void run() {
try {
barrier.await(); // 1st
barrier.await(); // 2nd
} catch (Exception exc) {
// ignore
}
}
}.start();
}
barrier.await(); // 1st
int matches = 0;
ArrayList<String> failed = new ArrayList<>();
ProcessBuilder pb = new ProcessBuilder(
JDKToolFinder.getJDKTool("jstack"),
String.valueOf(ProcessTools.getProcessId()));
String[] output = new OutputAnalyzer(pb.start()).getOutput().split("\\n+");
Pattern pattern = Pattern.compile(
"\\\"Priority=(\\d+)\\\".* prio=(\\d+).*");
for (String line : output) {
Matcher matcher = pattern.matcher(line);
if (matcher.matches()) {
matches += 1;
String expected = matcher.group(1);
String actual = matcher.group(2);
if (!expected.equals(actual)) {
failed.add(line);
}
}
}
barrier.await(); // 2nd
barrier.reset();
assertEquals(matches, NUMBER_OF_JAVA_PRIORITIES);
assertTrue(failed.isEmpty(), failed.size() + ":" + failed);
}
示例10: reset
import java.util.concurrent.CyclicBarrier; //导入方法依赖的package包/类
private static void reset(CyclicBarrier barrier) {
barrier.reset();
check(! barrier.isBroken());
equal(barrier.getNumberWaiting(), 0);
}