Class ThreadSafetyChecker

java.lang.Object
cloud.opencode.base.test.concurrent.ThreadSafetyChecker

public final class ThreadSafetyChecker extends Object
Thread Safety Checker 线程安全检查器

Utility for checking thread safety of objects.

用于检查对象线程安全性的工具。

Features | 主要功能:

  • Thread safety checking utilities - 线程安全检查工具
  • Counter and operation safety checks - 计数器和操作安全检查

Usage Examples | 使用示例:

CheckResult result = ThreadSafetyChecker.checkCounter(
    () -> counter.incrementAndGet(),
    counter::get, 10, 1000);
assertTrue(result.passed());
Since:
JDK 25, opencode-base-test V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • checkCounter

      public static ThreadSafetyChecker.CheckResult checkCounter(Runnable incrementTask, Supplier<Integer> getCountTask, int threads, int incrementsPerThread)
      Check if a counter is thread-safe 检查计数器是否线程安全
      Parameters:
      incrementTask - the increment task | 递增任务
      getCountTask - the get count task | 获取计数任务
      threads - the number of threads | 线程数
      incrementsPerThread - increments per thread | 每线程递增次数
      Returns:
      the result | 结果
    • isThreadSafe

      public static boolean isThreadSafe(Runnable operation, int threads, int iterations)
      Check if object operations are thread-safe 检查对象操作是否线程安全
      Parameters:
      operation - the operation | 操作
      threads - the number of threads | 线程数
      iterations - iterations per thread | 每线程迭代次数
      Returns:
      true if thread-safe | 如果线程安全返回true