Initial Commit with existing project materials
parent
b891960aa0
commit
bd28a1e622
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>pp.hsma</groupId>
|
||||
<artifactId>pp.A4</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,41 @@
|
|||
package io.dama.ffi.hoh;
|
||||
|
||||
public class Main {
|
||||
static SimplifiedList<Integer> list;
|
||||
|
||||
static Runnable sliceSum(int start, int end, int expected) {
|
||||
return () -> {
|
||||
var sum = 0;
|
||||
for (var i = start; i < end; i++) {
|
||||
sum += list.get(i);
|
||||
}
|
||||
if (sum != expected) {
|
||||
System.out.println("Fehler in "
|
||||
+ Thread.currentThread().getName() + ": " + sum);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void main(String... args) throws InterruptedException {
|
||||
list = new ThreadsafeSimplifiedList<>();
|
||||
for (int i = 0; i < 5000; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
var thread0 = new Thread(sliceSum(0, 1250, 780625));
|
||||
var thread1 = new Thread(sliceSum(1250, 2500, 2343125));
|
||||
var thread2 = new Thread(sliceSum(2500, 3750, 3905625));
|
||||
var thread3 = new Thread(sliceSum(3750, 5000, 5468125));
|
||||
var start = System.currentTimeMillis();
|
||||
thread0.start();
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread3.start();
|
||||
thread0.join();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
thread3.join();
|
||||
System.out.printf("%s: %d ms\n", list.getClass().toString(),
|
||||
System.currentTimeMillis() - start);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package io.dama.ffi.hoh;
|
||||
|
||||
public interface SimplifiedList<T> {
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
* @param index index of the element to return
|
||||
* @return the element at the specified position in this list
|
||||
*/
|
||||
public T get(int index);
|
||||
|
||||
/**
|
||||
* Appends the specified element to the end of this list. There are no
|
||||
* limitations on what elements may be added to this list.
|
||||
*
|
||||
* @param element element to be appended to this list
|
||||
* @return true
|
||||
* @see java.util.Collection#add(Object)
|
||||
*
|
||||
*/
|
||||
public boolean add(T element);
|
||||
|
||||
/**
|
||||
* Replaces the element at the specified position in this list with the
|
||||
* specified element.
|
||||
*
|
||||
* @param index index of the element to replace
|
||||
* @param element element to be stored at the specified position
|
||||
* @return the element previously at the specified position
|
||||
*/
|
||||
public T set(int index, T element);
|
||||
|
||||
/**
|
||||
* Returns true if this list contains no elements.
|
||||
*
|
||||
* @return true if this list contains no elements
|
||||
*/
|
||||
public boolean isEmpty();
|
||||
|
||||
/**
|
||||
* delayed passing through of parameter
|
||||
*
|
||||
* @param element element to pass through
|
||||
* @return passed though element
|
||||
*
|
||||
*/
|
||||
public default T delay(T element) {
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
return element;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package io.dama.ffi.hoh;
|
||||
|
||||
public class ThreadsafeSimplifiedList<T> implements SimplifiedList<T> {
|
||||
private Node<T> first;
|
||||
|
||||
private class Node<U> {
|
||||
private U element;
|
||||
private Node<U> prev;
|
||||
private Node<U> next;
|
||||
|
||||
private Node(U element, Node<U> prev, Node<U> next) {
|
||||
super();
|
||||
this.element = element;
|
||||
this.prev = prev;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
public ThreadsafeSimplifiedList() {
|
||||
super();
|
||||
this.first = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the element at the specified position in this list.
|
||||
*
|
||||
* @param index index of the element to return
|
||||
* @return the element at the specified position in this list
|
||||
*/
|
||||
@Override
|
||||
public synchronized T get(int index) {
|
||||
var ptr = this.first;
|
||||
for (var j = 0; j < index; j++) {
|
||||
ptr = ptr.next;
|
||||
}
|
||||
return delay(ptr.element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the specified element to the end of this list. There are no
|
||||
* limitations on what elements may be added to this list.
|
||||
*
|
||||
* @param element element to be appended to this list
|
||||
* @return true
|
||||
* @see java.util.Collection#add(Object)
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public synchronized boolean add(T element) {
|
||||
if (this.first != null) {
|
||||
var ptr = this.first;
|
||||
while (ptr.next != null) {
|
||||
ptr = ptr.next;
|
||||
}
|
||||
ptr.next = new Node<>(element, ptr, null);
|
||||
} else {
|
||||
this.first = new Node<>(element, null, null);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the element at the specified position in this list with the
|
||||
* specified element.
|
||||
*
|
||||
* @param index index of the element to replace
|
||||
* @param element element to be stored at the specified position
|
||||
* @return the element previously at the specified position
|
||||
*/
|
||||
@Override
|
||||
public synchronized T set(int index, T element) {
|
||||
var ptr = this.first;
|
||||
for (var j = 0; j < index; j++) {
|
||||
ptr = ptr.next;
|
||||
}
|
||||
var prevElement = ptr.element;
|
||||
ptr.element = element;
|
||||
return prevElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this list contains no elements.
|
||||
*
|
||||
* @return true if this list contains no elements
|
||||
*/
|
||||
@Override
|
||||
public synchronized boolean isEmpty() {
|
||||
return this.first == null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue