mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +00:00
docs: add editorconfig + formatting
This commit is contained in:
+62
-62
@@ -3,7 +3,7 @@ title: Active Object
|
||||
category: Concurrency
|
||||
language: en
|
||||
tag:
|
||||
- Performance
|
||||
- Performance
|
||||
---
|
||||
|
||||
## Intent
|
||||
@@ -29,57 +29,57 @@ itself, we can use the Active Object pattern.
|
||||
|
||||
```java
|
||||
public abstract class ActiveCreature {
|
||||
private final Logger logger = LoggerFactory.getLogger(ActiveCreature.class.getName());
|
||||
private final Logger logger = LoggerFactory.getLogger(ActiveCreature.class.getName());
|
||||
|
||||
private BlockingQueue<Runnable> requests;
|
||||
private BlockingQueue<Runnable> requests;
|
||||
|
||||
private String name;
|
||||
private String name;
|
||||
|
||||
private Thread thread;
|
||||
private Thread thread;
|
||||
|
||||
public ActiveCreature(String name) {
|
||||
this.name = name;
|
||||
this.requests = new LinkedBlockingQueue<Runnable>();
|
||||
thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
requests.take().run();
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
public ActiveCreature(String name) {
|
||||
this.name = name;
|
||||
this.requests = new LinkedBlockingQueue<Runnable>();
|
||||
thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
requests.take().run();
|
||||
} catch (InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
thread.start();
|
||||
}
|
||||
);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void eat() throws InterruptedException {
|
||||
requests.put(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("{} is eating!", name());
|
||||
logger.info("{} has finished eating!", name());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
public void eat() throws InterruptedException {
|
||||
requests.put(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("{} is eating!", name());
|
||||
logger.info("{} has finished eating!", name());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void roam() throws InterruptedException {
|
||||
requests.put(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("{} has started to roam the wastelands.", name());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
public void roam() throws InterruptedException {
|
||||
requests.put(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
logger.info("{} has started to roam the wastelands.", name());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return this.name;
|
||||
}
|
||||
public String name() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -91,9 +91,9 @@ For example, the Orc class:
|
||||
```java
|
||||
public class Orc extends ActiveCreature {
|
||||
|
||||
public Orc(String name) {
|
||||
super(name);
|
||||
}
|
||||
public Orc(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@@ -103,25 +103,25 @@ thread of control:
|
||||
|
||||
```java
|
||||
public static void main(String[]args){
|
||||
var app=new App();
|
||||
app.run();
|
||||
}
|
||||
var app=new App();
|
||||
app.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
ActiveCreature creature;
|
||||
try{
|
||||
for(int i=0;i<creatures;i++){
|
||||
creature=new Orc(Orc.class.getSimpleName().toString()+i);
|
||||
creature.eat();
|
||||
creature.roam();
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}catch(InterruptedException e){
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
Runtime.getRuntime().exit(1);
|
||||
}
|
||||
ActiveCreature creature;
|
||||
try{
|
||||
for(int i=0;i<creatures;i++){
|
||||
creature=new Orc(Orc.class.getSimpleName().toString()+i);
|
||||
creature.eat();
|
||||
creature.roam();
|
||||
}
|
||||
Thread.sleep(1000);
|
||||
}catch(InterruptedException e){
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
Runtime.getRuntime().exit(1);
|
||||
}
|
||||
```
|
||||
|
||||
## Class diagram
|
||||
|
||||
Reference in New Issue
Block a user