Java Code Examples for java.util.concurrent.ArrayBlockingQueue#peek()

The following examples show how to use java.util.concurrent.ArrayBlockingQueue#peek() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractReservoir.java    From Bats with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 2
Source File: AbstractReservoir.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 3
Source File: SerializationTest.java    From coroutines with GNU Lesser General Public License v3.0 4 votes vote down vote up
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();
            }
        }
    }
}
 
Example 4
Source File: SerializationTest.java    From coroutines with GNU Lesser General Public License v3.0 4 votes vote down vote up
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();
            }
        }
    }
}