下面列出了java.util.concurrent.ArrayBlockingQueue#peek ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Tuple sweep()
{
Object o;
final ArrayBlockingQueue<Object> queue = this.queue;
final Sink<Object> sink = getSink();
while ((o = queue.peek()) != null) {
if (o instanceof Tuple) {
return (Tuple)o;
}
count++;
sink.put(queue.poll());
}
return null;
}
@Override
public Tuple sweep()
{
Object o;
final ArrayBlockingQueue<Object> queue = this.queue;
final Sink<Object> sink = getSink();
while ((o = queue.peek()) != null) {
if (o instanceof Tuple) {
return (Tuple)o;
}
count++;
sink.put(queue.poll());
}
return null;
}
private void performIntCountTest(String testClass, InstrumentationSettings settings) throws Exception {
// This test is being wrapped in a new thread where the thread's context classlaoder is being set to the classloader of the zip
// we're dynamically loading. We need to do this being ObjectInputStream uses the system classloader by default, not the thread's
// classloader. CoroutineReader has been modified to use the thread's classloader if the system's classloader fails.
try (URLClassLoader classLoader = loadClassesInZipResourceAndInstrument(testClass + ".zip", settings)) {
ArrayBlockingQueue<Throwable> threadResult = new ArrayBlockingQueue<>(1);
Thread thread = new Thread(() -> {
try {
Class<Coroutine> cls = (Class<Coroutine>) classLoader.loadClass(testClass);
Coroutine coroutine = invokeConstructor(cls, new StringBuilder());
// Create and run original for a few cycles
CoroutineRunner runner = new CoroutineRunner(coroutine);
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertFalse((runner = writeReadExecute(runner)).execute()); // coroutine finished executing here
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
// Assert everything continued fine with deserialized version
Object deserializedCoroutine = readField(runner, "coroutine", true);
StringBuilder deserializedBuilder = (StringBuilder) readField(deserializedCoroutine, "builder", true);
assertEquals("started\n"
+ "0\n"
+ "1\n"
+ "2\n"
+ "3\n"
+ "4\n"
+ "5\n"
+ "6\n"
+ "7\n"
+ "8\n"
+ "9\n"
+ "started\n"
+ "0\n"
+ "1\n"
+ "2\n", deserializedBuilder.toString());
} catch (AssertionError | Exception e) {
threadResult.add(e);
}
});
thread.setContextClassLoader(classLoader);
thread.start();
thread.join();
Throwable t = (Throwable) threadResult.peek();
if (t != null) {
if (t instanceof Exception) {
throw (Exception) t;
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new RuntimeException();
}
}
}
}
private void performDoubleCountTest(String testClass, InstrumentationSettings settings) throws Exception {
// This test is being wrapped in a new thread where the thread's context classlaoder is being set to the classloader of the zip
// we're dynamically loading. We need to do this being ObjectInputStream uses the system classloader by default, not the thread's
// classloader. CoroutineReader has been modified to use the thread's classloader if the system's classloader fails.
try (URLClassLoader classLoader = loadClassesInZipResourceAndInstrument(testClass + ".zip", settings)) {
ArrayBlockingQueue<Throwable> threadResult = new ArrayBlockingQueue<>(1);
Thread thread = new Thread(() -> {
try {
Class<Coroutine> cls = (Class<Coroutine>) classLoader.loadClass(testClass);
Coroutine coroutine = invokeConstructor(cls, new StringBuilder());
// Create and run original for a few cycles
CoroutineRunner runner = new CoroutineRunner(coroutine);
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertFalse((runner = writeReadExecute(runner)).execute()); // coroutine finished executing here
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
assertTrue((runner = writeReadExecute(runner)).execute());
// Assert everything continued fine with deserialized version
Object deserializedCoroutine = readField(runner, "coroutine", true);
StringBuilder deserializedBuilder = (StringBuilder) readField(deserializedCoroutine, "builder", true);
assertEquals("started\n"
+ "0.0\n"
+ "1.0\n"
+ "2.0\n"
+ "3.0\n"
+ "4.0\n"
+ "5.0\n"
+ "6.0\n"
+ "7.0\n"
+ "8.0\n"
+ "9.0\n"
+ "started\n"
+ "0.0\n"
+ "1.0\n"
+ "2.0\n", deserializedBuilder.toString());
} catch (AssertionError | Exception e) {
threadResult.add(e);
}
});
thread.setContextClassLoader(classLoader);
thread.start();
thread.join();
Throwable t = (Throwable) threadResult.peek();
if (t != null) {
if (t instanceof Exception) {
throw (Exception) t;
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new RuntimeException();
}
}
}
}