java ( 2678459 ) - com.iluwatar.lo ( 2679977 ) stack: com.iluwatar.lockableobject.App.main(App.java:65) org.codehaus.mojo.exec.ExecJavaMojo.doMain(ExecJavaMojo.java:385) org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:374) org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:296) java.base/java.lang.Thread.run(Thread.java:840)
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
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.
*
* In this example, we create a new Lockable object with the SwordOfAragorn implementation of it.
* Afterward 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 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();
}
}
}
No. | From | Name | Value |
---|---|---|---|
1 | class@65 | LOGGER | Logger[com.iluwatar.lockableobject.App] |
2 | class@65 | WAIT_TIME | 3 |
3 | class@65 | WORKERS | 2 |
4 | class@65 | MULTIPLICATION_FACTOR | 3 |
5 | 65 | args | [Ljava.lang.String;@5e057aee |
END | 0 | 0 | 0 |
Process Filter | Thread Filter | ||||||
---|---|---|---|---|---|---|---|
2678459 java | 2680215 pool-1-thread-2 | 2680228 pool-1-thread-6 | 2680214 pool-1-thread-1 | 2680222 pool-1-thread-4 | 2679977 com.iluwatar.lo | 2680225 pool-1-thread-5 | 2680220 pool-1-thread-3 |
No. | PN | PID | TID | TN | T | L | Message |
---|---|---|---|---|---|---|---|
1 | java | 2678459 | 2680214 | pool-1-thread-1 | com.iluwatar.lockableobject.SwordOfAragorn | I | Elf 0 is now trying to acquire The Sword of Aragorn! |
2 | java | 2678459 | 2680215 | pool-1-thread-2 | com.iluwatar.lockableobject.SwordOfAragorn | I | Orc 0 is now trying to acquire The Sword of Aragorn! |
3 | java | 2678459 | 2680214 | pool-1-thread-1 | com.iluwatar.lockableobject.domain.Feind | I | Elf 0 has acquired the sword! |
4 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.SwordOfAragorn | I | Human 0 is now trying to acquire The Sword of Aragorn! |
5 | java | 2678459 | 2680215 | pool-1-thread-2 | com.iluwatar.lockableobject.domain.Feind | I | A duel between Orc 0 and Elf 0 has been started! |
6 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.domain.Feind | I | A duel between Human 0 and Elf 0 has been started! |
7 | java | 2678459 | 2680222 | pool-1-thread-4 | com.iluwatar.lockableobject.SwordOfAragorn | I | Elf 1 is now trying to acquire The Sword of Aragorn! |
8 | java | 2678459 | 2680222 | pool-1-thread-4 | com.iluwatar.lockableobject.domain.Feind | I | A duel between Elf 1 and Elf 0 has been started! |
9 | java | 2678459 | 2680225 | pool-1-thread-5 | com.iluwatar.lockableobject.SwordOfAragorn | I | Orc 1 is now trying to acquire The Sword of Aragorn! |
10 | java | 2678459 | 2680225 | pool-1-thread-5 | com.iluwatar.lockableobject.domain.Feind | I | A duel between Orc 1 and Elf 0 has been started! |
11 | java | 2678459 | 2680228 | pool-1-thread-6 | com.iluwatar.lockableobject.SwordOfAragorn | I | Human 1 is now trying to acquire The Sword of Aragorn! |
12 | java | 2678459 | 2680228 | pool-1-thread-6 | com.iluwatar.lockableobject.domain.Feind | I | A duel between Human 1 and Elf 0 has been started! |
13 | java | 2678459 | 2680225 | pool-1-thread-5 | com.iluwatar.lockableobject.domain.Creature | I | ORC Orc 1 has been slayed! |
14 | java | 2678459 | 2680215 | pool-1-thread-2 | com.iluwatar.lockableobject.domain.Creature | I | ORC Orc 0 has been slayed! |
15 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.domain.Creature | I | ELF Elf 0 has been slayed! |
16 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.SwordOfAragorn | I | The Sword of Aragorn is now free! |
17 | java | 2678459 | 2680222 | pool-1-thread-4 | com.iluwatar.lockableobject.SwordOfAragorn | I | Elf 1 is now trying to acquire The Sword of Aragorn! |
18 | java | 2678459 | 2680228 | pool-1-thread-6 | com.iluwatar.lockableobject.SwordOfAragorn | I | Human 1 is now trying to acquire The Sword of Aragorn! |
19 | java | 2678459 | 2680222 | pool-1-thread-4 | com.iluwatar.lockableobject.domain.Feind | I | Elf 1 has acquired the sword! |
20 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.SwordOfAragorn | I | Human 0 is now trying to acquire The Sword of Aragorn! |
21 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.domain.Feind | I | A duel between Human 0 and Elf 1 has been started! |
22 | java | 2678459 | 2680228 | pool-1-thread-6 | com.iluwatar.lockableobject.domain.Feind | I | A duel between Human 1 and Elf 1 has been started! |
23 | java | 2678459 | 2680228 | pool-1-thread-6 | com.iluwatar.lockableobject.domain.Creature | I | HUMAN Human 1 has been slayed! |
24 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.domain.Creature | I | ELF Elf 1 has been slayed! |
25 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.SwordOfAragorn | I | The Sword of Aragorn is now free! |
26 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.SwordOfAragorn | I | Human 0 is now trying to acquire The Sword of Aragorn! |
27 | java | 2678459 | 2680220 | pool-1-thread-3 | com.iluwatar.lockableobject.domain.Feind | I | Human 0 has acquired the sword! |
28 | java | 2678459 | 2679977 | com.iluwatar.lo | com.iluwatar.lockableobject.App | I | The master of the sword is now Human 0. |
END | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Functions and Shortcuts
No. | Function | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | JavaDesignPatterns |
Update: | 20240509 |
Commit: | bf6456ba6 |
Source Code: | lockable-object |
BuildTool: | Java17 |
Compiler: | Java17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |