Assigment finished
commit
3f505e751b
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue