Assigment finished

main
2wenty1ne 2024-10-14 20:24:46 +02:00
commit 3f505e751b
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,24 @@
public class Runner {
public static void main(String... args) {
var task = new Task();
var thread = new Thread(task);
thread.setUncaughtExceptionHandler((t, e) -> {
System.err.println("Unhandled Exception: " + e.getMessage());
System.err.println(" Thread: " + t.threadId() + " - " + t.getName());
System.err.println(" Thread State: " + t.getState());
e.printStackTrace(System.err);
});
thread.start();
// var killer = new Thread(() -> {
// try {
// Thread.sleep(300);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// task.stopRequest();
// });
//killer.start();
(new Thread(() -> task.stopRequest())).start();
}
}

33
src/src/Task.java 100644
View File

@ -0,0 +1,33 @@
public class Task implements Runnable {
private volatile Thread self;
private volatile boolean stopped = false;
public void stopRequest() {
this.stopped = true;
if (this.self != null) {
this.self.interrupt();
}
}
public boolean isStopped() {
return this.stopped;
}
@Override
public void run() {
this.self = Thread.currentThread();
// 1. Initialisierungsphase
var i = 100;
while (!isStopped()) {
// 2. Arbeitsphase
System.out.println("i=" + i);
try {
Thread.sleep(1000 / i--);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// 3. Aufräumphase
System.out.println("fertig.");
}
}