Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • jordine/se3sose2022vorlesung
  • jr125/se3sose2022vorlesung
  • lb189/se3sose2022vorlesung
  • mb345/se3sose2022vorlesung
4 results
Show changes
Commits on Source (43)
Showing
with 764 additions and 1 deletion
......@@ -55,3 +55,28 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# macOS gitignore
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
.DS_Store
# Ignore .class files
/target/
*.class
\ No newline at end of file
......@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
FROM nginx
COPY html-dir /usr/share/nginx/html
\ No newline at end of file
version: '2'
services:
web:
image: nginx
volumes:
- "./html-dir:/usr/share/nginx/html:ro"
ports:
- "8082:80"
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello SE3!</h1>
Test, Test! <br>
Content, Content
</body>
</html>
\ No newline at end of file
......@@ -13,4 +13,13 @@
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/javazoom/jlayer -->
<dependency>
<groupId>javazoom</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package de.hdm.jordine.vorlesung.clientserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
// Adapted from https://www.baeldung.com/a-guide-to-java-sockets
public class EchoServer {
private ServerSocket serverSocket;
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
clientSocket = serverSocket.accept();
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (".".equals(inputLine)) {
out.println("good bye");
break;
}
out.println(inputLine.toUpperCase());
}
}
public void stop() throws IOException {
in.close();
out.close();
clientSocket.close();
serverSocket.close();
}
public static void main(String[] args) throws IOException {
EchoServer server=new EchoServer();
server.start(9191);
}
}
package de.hdm.jordine.vorlesung.clientserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class GreetClient {
private Socket clientSocket;
private PrintWriter out;
private BufferedReader in;
public void startConnection(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public String sendMessage(String msg) throws IOException {
out.println(msg);
String resp = in.readLine();
return resp;
}
public void stopConnection() throws IOException {
in.close();
out.close();
clientSocket.close();
}
public static void main(String[] args) throws IOException {
GreetClient client = new GreetClient();
Scanner in = new Scanner(System.in);
client.startConnection("127.0.0.1", 9191);
while (true){
String text = in.nextLine();
String response = client.sendMessage(text);
System.out.println("Server Response: " + response);
if (text.equals(".")){
break;
}
}
client.stopConnection();
}
}
\ No newline at end of file
package de.hdm.jordine.vorlesung.concurrency.demo;
import javazoom.jl.player.Player;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Scanner;
// Inspired by https://www.delftstack.com/de/howto/java/java-play-mp3/
public class AudioPlayerDemo01 {
private Player player;
public void playAudio(String path){
try {
FileInputStream fileInputStream = new FileInputStream(path);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
player = new Player(bufferedInputStream);
player.play();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AudioPlayerDemo01 demo = new AudioPlayerDemo01();
Scanner sc = new Scanner(System.in);
System.out.println("Write stop to stop the music: ");
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_01.mp3");
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_02.mp3");
if (sc.nextLine().equalsIgnoreCase("stop")) {
demo.player.close();
}
}
}
package de.hdm.jordine.vorlesung.concurrency.demo;
import javazoom.jl.player.Player;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Scanner;
// Inspired by https://www.delftstack.com/de/howto/java/java-play-mp3/
public class AudioPlayerDemo02 {
private Player player;
public void playAudio(String path){
try {
FileInputStream fileInputStream = new FileInputStream(path);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
player = new Player(bufferedInputStream);
player.play();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AudioPlayerDemo02 demo = new AudioPlayerDemo02();
Scanner sc = new Scanner(System.in);
System.out.println("Write stop to stop the music: ");
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_01.mp3");
}
});
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_02.mp3");
}
});
t0.start();
t1.start();
if (sc.nextLine().equalsIgnoreCase("stop")) {
demo.player.close();
}
}
}
package de.hdm.jordine.vorlesung.concurrency.demo;
import javazoom.jl.player.Player;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Scanner;
// Inspired by https://www.delftstack.com/de/howto/java/java-play-mp3/
public class AudioPlayerDemo03 {
private Player player;
public synchronized void playAudio(String path){
try {
FileInputStream fileInputStream = new FileInputStream(path);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
player = new Player(bufferedInputStream);
player.play();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
AudioPlayerDemo03 demo = new AudioPlayerDemo03();
Scanner sc = new Scanner(System.in);
System.out.println("Write stop to stop the music: ");
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_01.mp3");
}
});
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_02.mp3");
}
});
t0.start();
t1.start();
if (sc.nextLine().equalsIgnoreCase("stop")) {
demo.player.close();
}
}
}
package de.hdm.jordine.vorlesung.concurrency.demo;
import javazoom.jl.player.Player;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;
// Inspired by https://www.delftstack.com/de/howto/java/java-play-mp3/
public class AudioPlayerDemo04 {
private Player player;
private ReentrantLock lock = new ReentrantLock();
public void playAudio(String path){
try {
lock.lock();
FileInputStream fileInputStream = new FileInputStream(path);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
player = new Player(bufferedInputStream);
System.out.println("Playing " + path);
player.play();
lock.unlock();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
AudioPlayerDemo04 demo = new AudioPlayerDemo04();
Scanner sc = new Scanner(System.in);
System.out.println("Write stop to stop the music: ");
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Starting t0");
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_01.mp3");
}
});
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Starting t1");
demo.playAudio("/Users/tobiasjordine/Documents/HdM/SE3/sose2022/misc/demo_audio/sample_02.mp3");
}
});
t0.start();
t1.start();
if (sc.nextLine().equalsIgnoreCase("stop")) {
demo.player.close();
}
}
}
package de.hdm.jordine.vorlesung.concurrency.issues;
import java.util.LinkedList;
import java.util.Queue;
public class BlockingQueue<T> {
private Queue<T> queue = new LinkedList<T>();
private int capacity;
public BlockingQueue(int capacity) {
this.capacity = capacity;
}
public synchronized void put(T element) throws InterruptedException {
while(queue.size() == capacity) {
System.out.println("waitng to add");
wait();
}
queue.add(element);
notify(); // notifyAll() for multiple producer/consumer threads
}
public synchronized T take() throws InterruptedException {
while(queue.isEmpty()) {
System.out.println("waitng to remove");
wait();
}
T item = queue.remove();
notify(); // notifyAll() for multiple producer/consumer threads
return item;
}
public static void main(String[] args) {
BlockingQueue<String> b = new BlockingQueue<>(1);
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
try {
b.put("Hello");
System.out.println("adding hello");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t0.start();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
try {
b.put("World");
System.out.println("adding World");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
String text = b.take();
System.out.println("removing " + text);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t2.start();
}
}
package de.hdm.jordine.vorlesung.concurrency.issues;
import java.util.*;
//based on https://www.geeksforgeeks.org/difference-traditional-collections-concurrent-collections-java/
public class ConcurrentModificationDemo{
public static void main(String[] args) throws InterruptedException {
Collection<Integer> asyncCollection = new ArrayList<>();
Runnable listOperations = () -> {
for (int i = 0; i < 100; i++) {
asyncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
}
};
Thread thread1 = new Thread(listOperations);
Thread thread2 = new Thread(listOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println(asyncCollection.size());
}
}
package de.hdm.jordine.vorlesung.concurrency.issues;
// based on https://www.tutorialspoint.com/java/java_thread_deadlock.htm
public class DeadLock {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1){
System.out.println("Thread 1 has lock1");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 1 waiting for lock2");
synchronized (lock2){
System.out.println("Thread 1 has lock2 and lock1");
}
}
}
});
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock2){
System.out.println("Thread 2 has lock2");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 2 waiting for lock1");
synchronized (lock1){
System.out.println("Thread 2 has lock1 and lock2");
}
}
}
});
t0.start();
t1.start();
}
}
package de.hdm.jordine.vorlesung.concurrency.issues;
public class LostUpdate {
//private volatile int counter = 0;
private int counter = 0;
public void increment(){
counter = counter + 1;
}
public static void main(String[] args) throws InterruptedException {
LostUpdate lu = new LostUpdate();
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
lu.increment();
}
}
});
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10000; i++) {
lu.increment();
}
}
});
long start = System.currentTimeMillis();
t0.start();
t1.start();
// t0.join(); // required for synchronisation with main thread
// t1.join(); // required for synchronisation with main thread
long end = System.currentTimeMillis();
System.out.println("Counter value: " + lu.counter);
System.out.println(end - start);
}
}
package de.hdm.jordine.vorlesung.concurrency.solutions;
import java.util.*;
//based on https://www.baeldung.com/java-synchronized-collections
public class ConcurrentModificationSolutionDemo{
public static void main(String[] args) throws InterruptedException {
Collection<Integer> syncCollection = Collections.synchronizedCollection(new ArrayList<>());
Runnable listOperations = () -> {
for (int i = 0; i < 100; i++) {
syncCollection.addAll(Arrays.asList(1, 2, 3, 4, 5, 6));
}
};
Thread thread1 = new Thread(listOperations);
Thread thread2 = new Thread(listOperations);
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println(syncCollection.size());
}
}
package de.hdm.jordine.vorlesung.concurrency.solutions;
// based on https://www.tutorialspoint.com/java/java_thread_deadlock.htm
public class DeadLockSolution {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();
Thread t0 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1){
System.out.println("Thread 1 has lock1");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 1 waiting for lock2");
synchronized (lock2){
System.out.println("Thread 1 has lock1 and lock2");
}
}
}
});
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1){
System.out.println("Thread 2 has lock2");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 2 waiting for lock1");
synchronized (lock2){
System.out.println("Thread 2 has lock1 and lock2");
}
}
}
});
t0.start();
t1.start();
}
}
package de.hdm.jordine.vorlesung.concurrency.solutions;
import java.util.concurrent.*;
// based on https://www.baeldung.com/java-future
public class FutureDemo {
private ExecutorService executor;
public Future<Integer> calculate(Integer input) {
executor = Executors.newFixedThreadPool(10);
return executor.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
Thread.sleep(1000);
return input * input;
}
});
}
public void shutdownExecutor() throws InterruptedException {
executor.shutdown();
try {
if (!executor.awaitTermination(800, TimeUnit.MILLISECONDS)) {
executor.shutdownNow();
}
} catch (InterruptedException e) {
executor.shutdownNow();
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureDemo demo = new FutureDemo();
System.out.println("Starting calculation");
Future<Integer> result0 = demo.calculate(2);
System.out.println("2 x 2 = " + result0.get());
Future<Integer> result1 = demo.calculate(4);
while (!result1.isDone()){
System.out.println("calculating...");
Thread.sleep(300);
}
System.out.println("4 x 4 = " + result1.get());
demo.shutdownExecutor();
System.out.println("exit");
System.exit(0);
}
}
package de.hdm.jordine.vorlesung.concurrency.solutions;
import java.util.stream.IntStream;
import java.util.stream.Stream;
//based on https://mkyong.com/java8/java-8-parallel-streams-examples/
public class JavaStreams {
public static boolean isPrime(int number) {
if (number <= 1) return false;
return !IntStream.rangeClosed(2, number / 2).anyMatch(i -> number % i == 0);
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
long count = Stream.iterate(0, n -> n + 1)
.limit(1_000_000)
.parallel()
.filter(JavaStreams::isPrime)
.peek(x -> System.out.format("%s\t", x))
.count();
long end = System.currentTimeMillis();
long duration = end - start;
System.out.println("\n\nDuration: " + duration/1000.0);
}
}