78 lines
2.8 KiB
Java
78 lines
2.8 KiB
Java
|
package com.iluwatar.lockableobject;
|
||
|
|
||
|
import com.iluwatar.lockableobject.domain.Creature;
|
||
|
import com.iluwatar.lockableobject.domain.Elf;
|
||
|
import com.iluwatar.lockableobject.domain.Feind;
|
||
|
import com.iluwatar.lockableobject.domain.Human;
|
||
|
import com.iluwatar.lockableobject.domain.Orc;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
import java.util.concurrent.ExecutorService;
|
||
|
import java.util.concurrent.Executors;
|
||
|
import java.util.concurrent.TimeUnit;
|
||
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
||
|
/**
|
||
|
* The Lockable Object pattern is a concurrency pattern. Instead of using the "synchronized" word
|
||
|
* upon the methods to be synchronized, the object which implements the Lockable interface handles
|
||
|
* the request.
|
||
|
*
|
||
|
* <p>In this example, we create a new Lockable object with the SwordOfAragorn implementation of it.
|
||
|
* Afterwards we create 6 Creatures with the Elf, Orc and Human implementations and assign them each
|
||
|
* to a Fiend object and the Sword is the target object. Because there is only one Sword and it uses
|
||
|
* the Lockable Object pattern, only one creature can hold the sword at a given time. When the sword
|
||
|
* is locked, any other alive Fiends will try to lock, which will result in a race to lock the
|
||
|
* sword.
|
||
|
*
|
||
|
* @author Noam Greenshtain
|
||
|
*/
|
||
|
@Slf4j
|
||
|
public class App implements Runnable {
|
||
|
|
||
|
private static final int WAIT_TIME = 3;
|
||
|
private static final int WORKERS = 2;
|
||
|
private static final int MULTIPLICATION_FACTOR = 3;
|
||
|
|
||
|
/**
|
||
|
* main method.
|
||
|
*
|
||
|
* @param args as arguments for the main method.
|
||
|
*/
|
||
|
public static void main(String[] args) {
|
||
|
var app = new App();
|
||
|
app.run();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void run() {
|
||
|
// The target object for this example.
|
||
|
var sword = new SwordOfAragorn();
|
||
|
// Creation of creatures.
|
||
|
List<Creature> creatures = new ArrayList<>();
|
||
|
for (var i = 0; i < WORKERS; i++) {
|
||
|
creatures.add(new Elf(String.format("Elf %s", i)));
|
||
|
creatures.add(new Orc(String.format("Orc %s", i)));
|
||
|
creatures.add(new Human(String.format("Human %s", i)));
|
||
|
}
|
||
|
int totalFiends = WORKERS * MULTIPLICATION_FACTOR;
|
||
|
ExecutorService service = Executors.newFixedThreadPool(totalFiends);
|
||
|
// Attach every creature and the sword is a Fiend to fight for the sword.
|
||
|
for (var i = 0; i < totalFiends; i = i + MULTIPLICATION_FACTOR) {
|
||
|
service.submit(new Feind(creatures.get(i), sword));
|
||
|
service.submit(new Feind(creatures.get(i + 1), sword));
|
||
|
service.submit(new Feind(creatures.get(i + 2), sword));
|
||
|
}
|
||
|
// Wait for program to terminate.
|
||
|
try {
|
||
|
if (!service.awaitTermination(WAIT_TIME, TimeUnit.SECONDS)) {
|
||
|
LOGGER.info("The master of the sword is now {}.", sword.getLocker().getName());
|
||
|
}
|
||
|
} catch (InterruptedException e) {
|
||
|
LOGGER.error(e.getMessage());
|
||
|
Thread.currentThread().interrupt();
|
||
|
} finally {
|
||
|
service.shutdown();
|
||
|
}
|
||
|
}
|
||
|
}
|