@Test(timeout = 60000)
public void testConcurrentUsage() throws Throwable {

    final AtomicBoolean finished = new AtomicBoolean();
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    // With 50 threads issue #6087 is reproducible on every run.
    Thread[] threads = new Thread[50];
    final CyclicBarrier barrier = new CyclicBarrier(threads.length);
    for (int i = 0; i < threads.length; i++) {
        Thread t = new Thread(new Runnable() {
            Queue<LeakAwareResource> resources = new ArrayDeque<LeakAwareResource>(100);
            @Override
            public void run() {
                try {
                    barrier.await();
                    // Run 10000 times or until the test is marked as finished.
                    for (int b = 0; b < 1000 && !finished.get(); b++) {
                        // Allocate 100 LeakAwareResource per run and close them after it.
                        for (int a = 0; a < 100; a++) {
                            DefaultResource resource = new DefaultResource();
                            ResourceLeakTracker<Resource> leak = DefaultResource.detector.track(resource);
                            LeakAwareResource leakAwareResource = new LeakAwareResource(resource, leak);
                            resources.add(leakAwareResource);
                        }
                        if (closeResources(true)) {
                            finished.set(true);
                        }
                    }
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                } catch (Throwable e) {
                    error.compareAndSet(null, e);
                } finally {
                    // Just close all resource now without assert it to eliminate more reports.
                    closeResources(false);
                }
            }
            private boolean closeResources(boolean checkClosed) {
                for (;;) {
                    LeakAwareResource r = resources.poll();
                    if (r == null) {
                        return false;
                    }
                    boolean closed = r.close();
                    if (checkClosed && !closed) {
                        error.compareAndSet(null,
                                new AssertionError("ResourceLeak.close() returned 'false' but expected 'true'"));
                        return true;
                    }
                }
            }
        });
        threads[i] = t;
        t.start();
    }
    // Just wait until all threads are done.
    for (Thread t: threads) {
        t.join();
    }
    // Check if we had any leak reports in the ResourceLeakDetector itself
    DefaultResource.detector.assertNoErrors();
    assertNoErrors(error);
}