docs: add editorconfig + formatting

This commit is contained in:
Ilkka Seppälä
2024-03-29 14:30:50 +02:00
parent f80cc468b2
commit 6322d538cd
23 changed files with 1823 additions and 1480 deletions
+41 -41
View File
@@ -3,7 +3,7 @@ title: Balking
category: Concurrency
language: en
tag:
- Decoupling
- Decoupling
---
## Intent
@@ -44,36 +44,36 @@ Here are the relevant parts of the `WashingMachine` class.
@Slf4j
public class WashingMachine {
private final DelayProvider delayProvider;
private WashingMachineState washingMachineState;
private final DelayProvider delayProvider;
private WashingMachineState washingMachineState;
public WashingMachine(DelayProvider delayProvider) {
this.delayProvider = delayProvider;
this.washingMachineState = WashingMachineState.ENABLED;
}
public WashingMachineState getWashingMachineState() {
return washingMachineState;
}
public void wash() {
synchronized (this) {
var machineState = getWashingMachineState();
LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), machineState);
if (this.washingMachineState == WashingMachineState.WASHING) {
LOGGER.error("Cannot wash if the machine has been already washing!");
return;
}
this.washingMachineState = WashingMachineState.WASHING;
public WashingMachine(DelayProvider delayProvider) {
this.delayProvider = delayProvider;
this.washingMachineState = WashingMachineState.ENABLED;
}
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
this.delayProvider.executeAfterDelay(50, TimeUnit.MILLISECONDS, this::endOfWashing);
}
public synchronized void endOfWashing() {
washingMachineState = WashingMachineState.ENABLED;
LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());
}
public WashingMachineState getWashingMachineState() {
return washingMachineState;
}
public void wash() {
synchronized (this) {
var machineState = getWashingMachineState();
LOGGER.info("{}: Actual machine state: {}", Thread.currentThread().getName(), machineState);
if (this.washingMachineState == WashingMachineState.WASHING) {
LOGGER.error("Cannot wash if the machine has been already washing!");
return;
}
this.washingMachineState = WashingMachineState.WASHING;
}
LOGGER.info("{}: Doing the washing", Thread.currentThread().getName());
this.delayProvider.executeAfterDelay(50, TimeUnit.MILLISECONDS, this::endOfWashing);
}
public synchronized void endOfWashing() {
washingMachineState = WashingMachineState.ENABLED;
LOGGER.info("{}: Washing completed.", Thread.currentThread().getId());
}
}
```
@@ -81,7 +81,7 @@ Here's the simple `DelayProvider` interface used by the `WashingMachine`.
```java
public interface DelayProvider {
void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task);
void executeAfterDelay(long interval, TimeUnit timeUnit, Runnable task);
}
```
@@ -90,18 +90,18 @@ Now we introduce the application using the `WashingMachine`.
```java
public static void main(String...args){
final var washingMachine=new WashingMachine();
var executorService=Executors.newFixedThreadPool(3);
for(int i=0;i< 3;i++){
executorService.execute(washingMachine::wash);
}
executorService.shutdown();
try{
executorService.awaitTermination(10,TimeUnit.SECONDS);
}catch(InterruptedException ie){
LOGGER.error("ERROR: Waiting on executor service shutdown!");
Thread.currentThread().interrupt();
}
}
var executorService=Executors.newFixedThreadPool(3);
for(int i=0;i< 3;i++){
executorService.execute(washingMachine::wash);
}
executorService.shutdown();
try{
executorService.awaitTermination(10,TimeUnit.SECONDS);
}catch(InterruptedException ie){
LOGGER.error("ERROR: Waiting on executor service shutdown!");
Thread.currentThread().interrupt();
}
}
```
Here is the console output of the program.