herje
commit
fa093f1db7
|
@ -0,0 +1,61 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>pp</groupId>
|
||||
<artifactId>pp.02.03-Lock</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<exec.mainClass>pp.Factory</exec.mainClass>
|
||||
<maven.compiler.release>10</maven.compiler.release>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency><!-- für Unit-Tests -->
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.10.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency><!-- für Lombok -->
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.30</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency><!-- für net.jcip Annotationen -->
|
||||
<groupId>net.jcip</groupId>
|
||||
<artifactId>jcip-annotations</artifactId>
|
||||
<version>1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin><!-- für Unit-Tests [mvn test] -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
<plugin><!-- [mvn compile] -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.9.0</version>
|
||||
</plugin>
|
||||
<plugin><!-- [mvn exec:java] -->
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</plugin>
|
||||
<plugin><!-- [mvn javadoc:javadoc] -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<configuration>
|
||||
<show>private</show>
|
||||
<locale>en_US</locale>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue