public static void main(String[] args) {
        Thread mainThread = Thread.currentThread();

        GenericContainer<?> genericContainer = null;

        try {
            genericContainer = new GenericContainer<>(TestImages.TINY_IMAGE).withCommand("top");
            genericContainer.start();

            Set<Thread> threads = new HashSet<>(Thread.getAllStackTraces().keySet());
            threads.remove(mainThread);

            Set<Thread> nonDaemonThreads = threads.stream().filter(it -> !it.isDaemon()).collect(Collectors.toSet());

            if (!nonDaemonThreads.isEmpty()) {
                String nonDaemonThreadNames = nonDaemonThreads
                    .stream()
                    .map(Thread::getName)
                    .collect(Collectors.joining("\n", "\n", ""));

                fail("Expected all threads to be daemons but the following are not:\n" + nonDaemonThreadNames);
            }
        } finally {
            if (genericContainer != null) {
                genericContainer.stop();
            }
        }
    }
This test forks a new JVM, otherwise it's not possible to reliably diff the threads
core/src/test/java/org/testcontainers/DaemonTest.java