ConcurrencyTester

Introduction

Sometimes, we need to simulate the concurrency testing of a certain business by calling it from multiple threads. To address this need, Hutool provides a simple concurrent testing class named ConcurrencyTester.

Usage

ConcurrencyTester tester = ThreadUtil.concurrencyTest(100, () -> {
    // The logic content to be tested
    long delay = RandomUtil.randomLong(100, 1000);
    ThreadUtil.sleep(delay);
    Console.log("{} test finished, delay: {}", Thread.currentThread().getName(), delay);
});

// Obtain the total execution time in milliseconds
Console.log(tester.getInterval());

This example demonstrates how to use the concurrencyTest method in ThreadUtil to simulate the concurrent execution of 100 threads. Each thread randomly generates a delay between 100 and 1000 milliseconds and then sleeps for the specified duration before logging the test completion message. The ConcurrencyTester class can be used to measure the total execution time of all threads and provide other metrics for testing.