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
+62 -62
View File
@@ -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