This commit is contained in:
2024-02-09 23:12:28 +01:00
parent d84ea072ef
commit 5d7694ce0f
11 changed files with 87 additions and 2 deletions

View File

@@ -1,6 +1,10 @@
package Klausur_ueb;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadTraining {
public static void main(String[] args) {
@@ -38,7 +42,7 @@ class MyThread2 extends Thread {
wait(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (Exception e) {
} catch (Exception ignored) {
}
@@ -57,4 +61,45 @@ class CoolNumber {
return tmp;
}
}
class ExecuteTask {
public static void main(String[] arg) {
ExecutorService executor = Executors.newFixedThreadPool(3);
int sumWartezeit = 1000;
java.util.Random zufall = new java.util.Random();
for (int i = 0; i < 10; i++) {
String name = "" + i;
int wartezeit = zufall.nextInt(1000);
sumWartezeit = sumWartezeit + wartezeit;
Runnable task = new Task(name, wartezeit);
System.out.println("Task " + name + " mit Wartezeit " + wartezeit + " wird an den Threadpool uebergeben.");
executor.execute(task);
}
try {
Thread.sleep(sumWartezeit);
executor.shutdown();
executor.awaitTermination(sumWartezeit, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
}
}
}
class Task implements Runnable {
private String name;
private int wartezeit;
public Task(String name, int wartezeit) {
this.name = name;
this.wartezeit = wartezeit;
}
public void run() {
System.out.println("Task " + name + " beginnt um " + System.currentTimeMillis() + ".");
try {
Thread.sleep(wartezeit);
} catch (InterruptedException e) {
}
System.out.println("Task " + name + " ist fertig um " + System.currentTimeMillis() + " .");
}
}