siteName > > lockable-object
Threads
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.
 *
 * <p>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<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();
    }
  }
}
Variables All
No.FromNameValue
1class@65LOGGERLogger[com.iluwatar.lockableobject.App]
2class@65WAIT_TIME3
3class@65WORKERS2
4class@65MULTIPLICATION_FACTOR3
565args[Ljava.lang.String;@5e057aee
END 0 00
Output All Filter Merge
Process FilterThread 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.PNPIDTIDTNTLMessage
1java26784592680214pool-1-thread-1com.iluwatar.lockableobject.SwordOfAragornIElf 0 is now trying to acquire The Sword of Aragorn!
2java26784592680215pool-1-thread-2com.iluwatar.lockableobject.SwordOfAragornIOrc 0 is now trying to acquire The Sword of Aragorn!
3java26784592680214pool-1-thread-1com.iluwatar.lockableobject.domain.FeindIElf 0 has acquired the sword!
4java26784592680220pool-1-thread-3com.iluwatar.lockableobject.SwordOfAragornIHuman 0 is now trying to acquire The Sword of Aragorn!
5java26784592680215pool-1-thread-2com.iluwatar.lockableobject.domain.FeindIA duel between Orc 0 and Elf 0 has been started!
6java26784592680220pool-1-thread-3com.iluwatar.lockableobject.domain.FeindIA duel between Human 0 and Elf 0 has been started!
7java26784592680222pool-1-thread-4com.iluwatar.lockableobject.SwordOfAragornIElf 1 is now trying to acquire The Sword of Aragorn!
8java26784592680222pool-1-thread-4com.iluwatar.lockableobject.domain.FeindIA duel between Elf 1 and Elf 0 has been started!
9java26784592680225pool-1-thread-5com.iluwatar.lockableobject.SwordOfAragornIOrc 1 is now trying to acquire The Sword of Aragorn!
10java26784592680225pool-1-thread-5com.iluwatar.lockableobject.domain.FeindIA duel between Orc 1 and Elf 0 has been started!
11java26784592680228pool-1-thread-6com.iluwatar.lockableobject.SwordOfAragornIHuman 1 is now trying to acquire The Sword of Aragorn!
12java26784592680228pool-1-thread-6com.iluwatar.lockableobject.domain.FeindIA duel between Human 1 and Elf 0 has been started!
13java26784592680225pool-1-thread-5com.iluwatar.lockableobject.domain.CreatureIORC Orc 1 has been slayed!
14java26784592680215pool-1-thread-2com.iluwatar.lockableobject.domain.CreatureIORC Orc 0 has been slayed!
15java26784592680220pool-1-thread-3com.iluwatar.lockableobject.domain.CreatureIELF Elf 0 has been slayed!
16java26784592680220pool-1-thread-3com.iluwatar.lockableobject.SwordOfAragornIThe Sword of Aragorn is now free!
17java26784592680222pool-1-thread-4com.iluwatar.lockableobject.SwordOfAragornIElf 1 is now trying to acquire The Sword of Aragorn!
18java26784592680228pool-1-thread-6com.iluwatar.lockableobject.SwordOfAragornIHuman 1 is now trying to acquire The Sword of Aragorn!
19java26784592680222pool-1-thread-4com.iluwatar.lockableobject.domain.FeindIElf 1 has acquired the sword!
20java26784592680220pool-1-thread-3com.iluwatar.lockableobject.SwordOfAragornIHuman 0 is now trying to acquire The Sword of Aragorn!
21java26784592680220pool-1-thread-3com.iluwatar.lockableobject.domain.FeindIA duel between Human 0 and Elf 1 has been started!
22java26784592680228pool-1-thread-6com.iluwatar.lockableobject.domain.FeindIA duel between Human 1 and Elf 1 has been started!
23java26784592680228pool-1-thread-6com.iluwatar.lockableobject.domain.CreatureIHUMAN Human 1 has been slayed!
24java26784592680220pool-1-thread-3com.iluwatar.lockableobject.domain.CreatureIELF Elf 1 has been slayed!
25java26784592680220pool-1-thread-3com.iluwatar.lockableobject.SwordOfAragornIThe Sword of Aragorn is now free!
26java26784592680220pool-1-thread-3com.iluwatar.lockableobject.SwordOfAragornIHuman 0 is now trying to acquire The Sword of Aragorn!
27java26784592680220pool-1-thread-3com.iluwatar.lockableobject.domain.FeindIHuman 0 has acquired the sword!
28java26784592679977com.iluwatar.locom.iluwatar.lockableobject.AppIThe master of the sword is now Human 0.
END 0 0 0 0 0 00
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