BAEL-861 Introduction to Awaitlity (#2150)

* BAEL-748 quick guide to @Value

* BAEL-748 changes from review

* BAEL-748 inject comma-separated values into array

* BAEL-768 Introduction to Netty

* BAEL-768 remove commented code

* BAEL-861 Introduction to Awaitility

* BAEL-861 rename Test to UnitTest
This commit is contained in:
Anton
2017-07-07 06:12:41 +03:00
committed by KevinGilmore
parent b9b230f83e
commit 20b9f1bfa9
3 changed files with 147 additions and 0 deletions
@@ -0,0 +1,50 @@
package com.baeldung.awaitility;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
public class AsyncService {
private final int DELAY = 1000;
private final int INIT_DELAY = 2000;
private AtomicLong value = new AtomicLong(0);
private Executor executor = Executors.newFixedThreadPool(4);
private volatile boolean initialized = false;
public void initialize() {
executor.execute(() -> {
sleep(INIT_DELAY);
initialized = true;
});
}
public boolean isInitialized() {
return initialized;
}
public void addValue(long val) {
if (!isInitialized()) {
throw new IllegalStateException("Service is not initialized");
}
executor.execute(() -> {
sleep(DELAY);
value.addAndGet(val);
});
}
public long getValue() {
if (!isInitialized()) {
throw new IllegalStateException("Service is not initialized");
}
return value.longValue();
}
private void sleep(int delay) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}