initial
parent
79c516f4f4
commit
08037aa595
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Thread für ``Runnable`` mit Rückgabewert"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-01-RunnableWithResult.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-für-runnable-mit-rückgabewert)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)
|
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Thread für ``Runnable`` mit Rückgabewert"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-01-RunnableWithResult.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-für-runnable-mit-rückgabewert)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)
|
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Asynchrone Ausführung mit ``Callable`` und ``Future``"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-02-Future.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-asynchrone-ausführung-mit-callable-und-future)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)
|
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Asynchrone Ausführung mit ``Callable`` und ``Future``"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-02-Future.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-asynchrone-ausführung-mit-callable-und-future)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)
|
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Thread Pools"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-03-ThreadPoolSize.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-pools)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)
|
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Thread Pools"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-03-ThreadPoolSize.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/04-Solutions.html#laboraufgabe-thread-pools)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/04-threadpools.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/04-threadpools.html)
|
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Counter mit Atomics threadsicher machen"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-01-CounterAtomic.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-Solutions.html#laboraufgabe-counter-mit-atomics-threadsicher-machen)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/05-atomics.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/05-atomics.html)
|
|
@ -0,0 +1,12 @@
|
|||
default:
|
||||
mvn clean compile test
|
||||
exec args:
|
||||
mvn exec:java -Dexec.args={{args}}
|
||||
clean:
|
||||
mvn clean
|
||||
compile:
|
||||
mvn compile
|
||||
test:
|
||||
mvn test
|
||||
javadoc:
|
||||
mvn javadoc:javadoc
|
|
@ -0,0 +1,60 @@
|
|||
<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.05.01-CounterAtomic</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<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,22 @@
|
|||
package pp;
|
||||
|
||||
class Counter {
|
||||
private int c;
|
||||
|
||||
public Counter(int init) {
|
||||
this.c = init;
|
||||
}
|
||||
|
||||
public void inc() {
|
||||
this.c++;
|
||||
}
|
||||
|
||||
public void dec() {
|
||||
this.c--;
|
||||
}
|
||||
|
||||
public int get() {
|
||||
return this.c;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package pp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CounterTest {
|
||||
|
||||
@Test
|
||||
public void testInc() throws InterruptedException {
|
||||
fail();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDec() throws InterruptedException {
|
||||
fail();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
# Laboraufgabe "Counter mit Atomics threadsicher machen"
|
||||
- [Aufgabenstellung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-01-CounterAtomic.html)
|
||||
- [Musterlösung](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/labs/05-Solutions.html#laboraufgabe-counter-mit-atomics-threadsicher-machen)
|
||||
|
||||
# Materialien
|
||||
- [Folien](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/slides/05-atomics.html)
|
||||
- [Skript](https://services.informatik.hs-mannheim.de/~s.leuchter/pp/notes/05-atomics.html)
|
|
@ -0,0 +1,12 @@
|
|||
default:
|
||||
mvn clean compile test
|
||||
exec args:
|
||||
mvn exec:java -Dexec.args={{args}}
|
||||
clean:
|
||||
mvn clean
|
||||
compile:
|
||||
mvn compile
|
||||
test:
|
||||
mvn test
|
||||
javadoc:
|
||||
mvn javadoc:javadoc
|
|
@ -0,0 +1,60 @@
|
|||
<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.05.01-CounterAtomic_solution</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<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;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class AtomicCounter1 implements ICounter {
|
||||
private final AtomicInteger c;
|
||||
|
||||
public AtomicCounter1(int init) {
|
||||
this.c = new AtomicInteger(init);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inc() {
|
||||
var temp = this.c.get();
|
||||
while (!this.c.compareAndSet(temp, temp + 1)) {
|
||||
temp = this.c.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dec() {
|
||||
var temp = this.c.get();
|
||||
while (!this.c.compareAndSet(temp, temp - 1)) {
|
||||
temp = this.c.get();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get() {
|
||||
return this.c.get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package pp;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class AtomicCounter2 implements ICounter {
|
||||
private final AtomicInteger c;
|
||||
|
||||
public AtomicCounter2(int init) {
|
||||
this.c = new AtomicInteger(init);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inc() {
|
||||
this.c.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dec() {
|
||||
this.c.decrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get() {
|
||||
return this.c.get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package pp;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class AtomicCounter3 implements ICounter {
|
||||
private final AtomicInteger c;
|
||||
|
||||
public AtomicCounter3(int init) {
|
||||
this.c = new AtomicInteger(init);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inc() {
|
||||
for (;;) {
|
||||
var current = this.c.get();
|
||||
var next = current + 1;
|
||||
if (this.c.compareAndSet(current, next)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dec() {
|
||||
for (;;) {
|
||||
var current = this.c.get();
|
||||
var next = current - 1;
|
||||
if (this.c.compareAndSet(current, next)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get() {
|
||||
return this.c.get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package pp;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
class AtomicCounter4 implements ICounter {
|
||||
private final AtomicInteger c;
|
||||
|
||||
public AtomicCounter4(int init) {
|
||||
this.c = new AtomicInteger(init);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inc() {
|
||||
this.c.updateAndGet(i -> i + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dec() {
|
||||
this.c.updateAndGet(i -> i - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get() {
|
||||
return this.c.get();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package pp;
|
||||
|
||||
class Counter implements ICounter {
|
||||
private int c;
|
||||
|
||||
public Counter(int init) {
|
||||
this.c = init;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void inc() {
|
||||
this.c++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dec() {
|
||||
this.c--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get() {
|
||||
return this.c;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package pp;
|
||||
|
||||
interface ICounter {
|
||||
|
||||
void inc();
|
||||
|
||||
void dec();
|
||||
|
||||
int get();
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package pp;
|
||||
|
||||
class SynchronizedCounter implements ICounter {
|
||||
private int c;
|
||||
|
||||
public SynchronizedCounter(int init) {
|
||||
this.c = init;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void inc() {
|
||||
this.c++;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void dec() {
|
||||
this.c--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int get() {
|
||||
return this.c;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
package pp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CounterTest {
|
||||
private final static int RUNS = 500000;
|
||||
|
||||
private static void testIncrement(ICounter counter) {
|
||||
assertEquals(0, counter.get());
|
||||
var t1 = new Thread(() -> {
|
||||
for (var j = 0; j < RUNS; j++) {
|
||||
counter.inc();
|
||||
}
|
||||
});
|
||||
var t2 = new Thread(() -> {
|
||||
for (var j = 0; j < RUNS; j++) {
|
||||
counter.inc();
|
||||
}
|
||||
});
|
||||
t1.start();
|
||||
t2.start();
|
||||
try {
|
||||
t1.join();
|
||||
t2.join();
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
assertEquals(RUNS * 2, counter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCounterIncrement() throws InterruptedException {
|
||||
testIncrement(new Counter(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSynchronizedCounterIncrement() throws InterruptedException {
|
||||
testIncrement(new SynchronizedCounter(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter1Increment() throws InterruptedException {
|
||||
testIncrement(new AtomicCounter1(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter2Increment() throws InterruptedException {
|
||||
testIncrement(new AtomicCounter2(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter3Increment() throws InterruptedException {
|
||||
testIncrement(new AtomicCounter3(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter4Increment() throws InterruptedException {
|
||||
testIncrement(new AtomicCounter4(0));
|
||||
}
|
||||
|
||||
private static void testDecrement(ICounter counter) {
|
||||
assertEquals(RUNS * 2, counter.get());
|
||||
var t1 = new Thread(() -> {
|
||||
for (var j = 0; j < RUNS; j++) {
|
||||
counter.dec();
|
||||
}
|
||||
});
|
||||
var t2 = new Thread(() -> {
|
||||
for (var j = 0; j < RUNS; j++) {
|
||||
counter.dec();
|
||||
}
|
||||
});
|
||||
t1.start();
|
||||
t2.start();
|
||||
try {
|
||||
t1.join();
|
||||
t2.join();
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
assertEquals(0, counter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCounterDecrement() throws InterruptedException {
|
||||
testDecrement(new Counter(RUNS * 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSynchronizedCounterDecrement() throws InterruptedException {
|
||||
testDecrement(new SynchronizedCounter(RUNS * 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter1Decrement() throws InterruptedException {
|
||||
testDecrement(new AtomicCounter1(RUNS * 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter2Decrement() throws InterruptedException {
|
||||
testDecrement(new AtomicCounter2(RUNS * 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter3Decrement() throws InterruptedException {
|
||||
testDecrement(new AtomicCounter3(RUNS * 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAtomicCounter4Decrement() throws InterruptedException {
|
||||
testDecrement(new AtomicCounter4(RUNS * 2));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue