Example usage for java.util.concurrent ExecutorService submit

List of usage examples for java.util.concurrent ExecutorService submit

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService submit.

Prototype

<T> Future<T> submit(Runnable task, T result);

Source Link

Document

Submits a Runnable task for execution and returns a Future representing that task.

Usage

From source file:com.b2international.index.GroovyMemoryLeakTest.java

@Ignore
@Test//from   w  w w  .j a v  a 2s .  com
public void tryToGenerateMemoryLeak() throws Exception {
    final List<String> orderedItems = newArrayList();
    final Map<String, Data> documents = newHashMap();

    for (int i = 0; i < NUM_DOCS; i++) {
        String item = null;
        while (item == null || orderedItems.contains(item)) {
            item = RandomStringUtils.randomAlphabetic(10);
        }
        orderedItems.add(item);

        final Data data = new Data();
        data.setField1(item);
        data.setFloatField(100.0f - i);
        documents.put(Integer.toString(i), data);
    }

    indexDocuments(documents);

    ExecutorService executor = Executors.newFixedThreadPool(2);

    final Runnable theQuery = () -> {
        for (int i = 0; i < 10_000; i++) {
            final Query<Data> query = Query.select(Data.class)
                    .where(Expressions.scriptScore(Expressions.matchAll(), "floatField")).limit(NUM_DOCS)
                    .sortBy(SortBy.SCORE).build();
            search(query);
        }
    };

    // run 4 threads to simulate a bit higher load on the index
    executor.submit(theQuery, null);
    executor.submit(theQuery, null);
    executor.submit(theQuery, null);
    executor.submit(theQuery, null);

    executor.shutdown();
    // this won't pass at all, even if the fix is applied
    // the purpose of this test to detect and verify the GC via external monitoring thus it cannot be automated properly
    assertTrue(executor.awaitTermination(5, TimeUnit.MINUTES));
}