From fa093f1db72034dec12f2aa274ab48fac9b2e15f Mon Sep 17 00:00:00 2001 From: 2wenty1ne Date: Tue, 15 Oct 2024 17:06:33 +0200 Subject: [PATCH] herje --- pom.xml | 61 +++++++++++++++++++++++++++++++++++++++++ src/src/pp/Factory.java | 32 +++++++++++++++++++++ src/src/pp/Type.java | 38 +++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 pom.xml create mode 100644 src/src/pp/Factory.java create mode 100644 src/src/pp/Type.java diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f318950 --- /dev/null +++ b/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + pp + pp.02.03-Lock + 1.0-SNAPSHOT + jar + + + pp.Factory + 10 + UTF-8 + + + + org.junit.jupiter + junit-jupiter + 5.10.0 + test + + + org.projectlombok + lombok + 1.18.30 + provided + + + net.jcip + jcip-annotations + 1.0 + provided + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0 + + + org.apache.maven.plugins + maven-compiler-plugin + 3.9.0 + + + org.codehaus.mojo + exec-maven-plugin + 3.1.0 + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.5.0 + + private + en_US + + + + + diff --git a/src/src/pp/Factory.java b/src/src/pp/Factory.java new file mode 100644 index 0000000..7539bfb --- /dev/null +++ b/src/src/pp/Factory.java @@ -0,0 +1,32 @@ +package pp; + +public class Factory { + + private static Type instance; + private static Object lock = new Object(); + + public static Type getInstance() { + Type.prepare(); + if (instance == null) { + instance = new Type(); + } + return instance; + } + + public static void main(String... args) throws InterruptedException { + var now = System.currentTimeMillis(); + var threads = new Thread[100]; + for (var i = 0; i < 100; i++) { + threads[i] = new Thread(() -> { + Type object = Factory.getInstance(); + System.out.println(Thread.currentThread().getName() + ": serial of instance = " + object.getSerial()); + }, String.format("InstanceGrabber-%02d", i)); + threads[i].start(); + } + for (var i = 0; i < 100; i++) { + threads[i].join(); + } + var time = System.currentTimeMillis() - now; + System.out.println("Dauer: " + time + "ms"); + } +} \ No newline at end of file diff --git a/src/src/pp/Type.java b/src/src/pp/Type.java new file mode 100644 index 0000000..261399c --- /dev/null +++ b/src/src/pp/Type.java @@ -0,0 +1,38 @@ +package pp; + +import net.jcip.annotations.*; + +@ThreadSafe +public final class Type { + //TODO Vlt Ordner Struktur Problem von Maven src/main/java/pp + private static int counter = 0; + private int serial = 0; + private int number = 0; + + public static void prepare() { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + public Type() { + synchronized (Type.class) { + Type.counter++; + this.serial = Type.counter; + } + } + + public synchronized int getNumber() { + return this.number; + } + + public synchronized void setNumber(int number) { + this.number = number; + } + + public synchronized int getSerial() { + return this.serial; + } +} \ No newline at end of file