[BAEL-6027] Add example for Awaitility and Thread.sleep comparison (#13450)

* Add example for Awaitility and Thread.sleep comparison

* Fix unit test method name and indentation.

* Use property in pom.xml for awaitility dependency version.

---------

Co-authored-by: Uhrin Attila <attila.uhrin@frontendart.com>
This commit is contained in:
AttilaUhrin
2023-02-18 04:04:25 +01:00
committed by GitHub
parent e8d3a39eb6
commit 62f75e3859
3 changed files with 88 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package com.baeldung.concurrent;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class RequestProcessor {
private Map<String, String> requestStatuses = new HashMap<>();
public String processRequest() {
String requestId = UUID.randomUUID().toString();
requestStatuses.put(requestId, "PROCESSING");
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.schedule((() -> {
requestStatuses.put(requestId, "DONE");
}), getRandomNumberBetween(500, 2000), TimeUnit.MILLISECONDS);
return requestId;
}
public String getStatus(String requestId) {
return requestStatuses.get(requestId);
}
private int getRandomNumberBetween(int min, int max) {
Random random = new Random();
return random.nextInt(max - min) + min;
}
}