Class ThreadUtils

java.lang.Object
io.karatelabs.common.ThreadUtils

public final class ThreadUtils extends Object
Shared concurrency helpers usable from karate-core and from any code that embeds karate-core as a library.

Daemon thread factories are the cleanup contract every long-lived subsystem in karate-core (HttpServer, WsClient, ProcessHandle, ...) relies on: owners call close() / shutdownNow() explicitly, and if those are skipped for any reason (test crash, programmer error, embedder shutdown sequence) the daemon flag guarantees the JVM still terminates.

Two shapes are offered to cover both common cases:

  • daemonFactory(String) — numbered, for pools (cached / fixed thread pools, Netty event loop groups). Thread names are prefix + N (1-based).
  • daemonFactory(String, boolean) — pass numbered=false for a fixed-name factory suitable for Executors.newSingleThreadExecutor / newSingleThreadScheduledExecutor, where the thread name should match across restarts and grep cleanly.
  • Method Details

    • daemonFactory

      public static ThreadFactory daemonFactory(String prefix)
      Numbered daemon thread factory. Each spawned thread is named prefix + N (N starts at 1). Right for pools where multiple threads can coexist.
    • daemonFactory

      public static ThreadFactory daemonFactory(String name, boolean numbered)
      Daemon thread factory.
      Parameters:
      name - thread name (or name prefix when numbered is true)
      numbered - when true, threads are named name + N (1-based); when false, every spawned thread is named exactly name — suitable for single-thread executors where a stable name helps logs / thread dumps / grep