mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-20 01:25:41 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -29,86 +29,87 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* ActiveCreature class is the base of the active object example.
|
||||
*
|
||||
*/
|
||||
/** ActiveCreature class is the base of the active object example. */
|
||||
public abstract class ActiveCreature {
|
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ActiveCreature.class.getName());
|
||||
|
||||
private BlockingQueue<Runnable> requests;
|
||||
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
private Thread thread; // Thread of execution.
|
||||
|
||||
|
||||
private int status; // status of the thread of execution.
|
||||
|
||||
/**
|
||||
* Constructor and initialization.
|
||||
*/
|
||||
/** Constructor and initialization. */
|
||||
protected ActiveCreature(String name) {
|
||||
this.name = name;
|
||||
this.status = 0;
|
||||
this.requests = new LinkedBlockingQueue<>();
|
||||
thread = new Thread(() -> {
|
||||
boolean infinite = true;
|
||||
while (infinite) {
|
||||
try {
|
||||
requests.take().run();
|
||||
} catch (InterruptedException e) {
|
||||
if (this.status != 0) {
|
||||
logger.error("Thread was interrupted. --> {}", e.getMessage());
|
||||
}
|
||||
infinite = false;
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread =
|
||||
new Thread(
|
||||
() -> {
|
||||
boolean infinite = true;
|
||||
while (infinite) {
|
||||
try {
|
||||
requests.take().run();
|
||||
} catch (InterruptedException e) {
|
||||
if (this.status != 0) {
|
||||
logger.error("Thread was interrupted. --> {}", e.getMessage());
|
||||
}
|
||||
infinite = false;
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Eats the porridge.
|
||||
*
|
||||
* @throws InterruptedException due to firing a new Runnable.
|
||||
*/
|
||||
public void eat() throws InterruptedException {
|
||||
requests.put(() -> {
|
||||
logger.info("{} is eating!", name());
|
||||
logger.info("{} has finished eating!", name());
|
||||
});
|
||||
requests.put(
|
||||
() -> {
|
||||
logger.info("{} is eating!", name());
|
||||
logger.info("{} has finished eating!", name());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Roam the wastelands.
|
||||
*
|
||||
* @throws InterruptedException due to firing a new Runnable.
|
||||
*/
|
||||
public void roam() throws InterruptedException {
|
||||
requests.put(() ->
|
||||
logger.info("{} has started to roam in the wastelands.", name())
|
||||
);
|
||||
requests.put(() -> logger.info("{} has started to roam in the wastelands.", name()));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the creature.
|
||||
*
|
||||
* @return the name of the creature.
|
||||
*/
|
||||
public String name() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Kills the thread of execution.
|
||||
*
|
||||
* @param status of the thread of execution. 0 == OK, the rest is logging an error.
|
||||
*/
|
||||
public void kill(int status) {
|
||||
this.status = status;
|
||||
this.thread.interrupt();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the status of the thread of execution.
|
||||
*
|
||||
* @return the status of the thread of execution.
|
||||
*/
|
||||
public int getStatus() {
|
||||
|
||||
@@ -30,17 +30,17 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The Active Object pattern helps to solve synchronization difficulties without using
|
||||
* 'synchronized' methods. The active object will contain a thread-safe data structure
|
||||
* (such as BlockingQueue) and use to synchronize method calls by moving the logic of the method
|
||||
* into an invocator(usually a Runnable) and store it in the DSA.
|
||||
*
|
||||
* The Active Object pattern helps to solve synchronization difficulties without using
|
||||
* 'synchronized' methods. The active object will contain a thread-safe data structure (such as
|
||||
* BlockingQueue) and use to synchronize method calls by moving the logic of the method into an
|
||||
* invocator(usually a Runnable) and store it in the DSA.
|
||||
*
|
||||
* <p>In this example, we fire 20 threads to modify a value in the target class.
|
||||
*/
|
||||
public class App implements Runnable {
|
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(App.class.getName());
|
||||
|
||||
|
||||
private static final int NUM_CREATURES = 3;
|
||||
|
||||
/**
|
||||
@@ -48,11 +48,11 @@ public class App implements Runnable {
|
||||
*
|
||||
* @param args command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) {
|
||||
var app = new App();
|
||||
app.run();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
List<ActiveCreature> creatures = new ArrayList<>();
|
||||
|
||||
@@ -24,14 +24,10 @@
|
||||
*/
|
||||
package com.iluwatar.activeobject;
|
||||
|
||||
/**
|
||||
* An implementation of the ActiveCreature class.
|
||||
*
|
||||
*/
|
||||
/** An implementation of the ActiveCreature class. */
|
||||
public class Orc extends ActiveCreature {
|
||||
|
||||
public Orc(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,17 +27,16 @@ package com.iluwatar.activeobject;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
class ActiveCreatureTest {
|
||||
|
||||
@Test
|
||||
void executionTest() throws InterruptedException {
|
||||
ActiveCreature orc = new Orc("orc1");
|
||||
assertEquals("orc1",orc.name());
|
||||
assertEquals(0,orc.getStatus());
|
||||
orc.eat();
|
||||
orc.roam();
|
||||
orc.kill(0);
|
||||
}
|
||||
|
||||
|
||||
class ActiveCreatureTest {
|
||||
|
||||
@Test
|
||||
void executionTest() throws InterruptedException {
|
||||
ActiveCreature orc = new Orc("orc1");
|
||||
assertEquals("orc1", orc.name());
|
||||
assertEquals(0, orc.getStatus());
|
||||
orc.eat();
|
||||
orc.roam();
|
||||
orc.kill(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,11 +28,10 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user