From 526808525aed949b58114eb709133297fe474ae0 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 3 Mar 2022 14:23:57 +0100 Subject: [PATCH] Aufgabe 1.2 bearbeitet --- .../java/ch/zhaw/prog2/printer/PrinterB.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 code/Printer/src/main/java/ch/zhaw/prog2/printer/PrinterB.java diff --git a/code/Printer/src/main/java/ch/zhaw/prog2/printer/PrinterB.java b/code/Printer/src/main/java/ch/zhaw/prog2/printer/PrinterB.java new file mode 100644 index 0000000..0c3b96a --- /dev/null +++ b/code/Printer/src/main/java/ch/zhaw/prog2/printer/PrinterB.java @@ -0,0 +1,45 @@ +package ch.zhaw.prog2.printer; + +public class PrinterB { + + // test program + public static void main(String[] arg) { + Thread threadA = new Thread(new PrinterRunnable("PrinterA", '.', 10)); + threadA.start(); + + Thread threadB = new Thread(new PrinterRunnable("PrinterB", '*', 20)); + threadB.start(); + + while(threadA.isAlive() || threadB.isAlive()){ + + } + System.out.println("Ende Main Thread"); + } + + + private static class PrinterRunnable implements Runnable { + char symbol; + int sleepTime; + String name; + + public PrinterRunnable(String name, char symbol, int sleepTime) { + this.name = name; + this.symbol = symbol; + this.sleepTime = sleepTime; + } + + @Override + public void run() { + System.out.println(name + " run started..."); + for (int i = 1; i < 100; i++) { + System.out.print(symbol); + try { + Thread.sleep(sleepTime); + } catch (InterruptedException e) { + System.out.println(e.getMessage()); + } + } + System.out.println('\n' + name + " run ended."); + } + } +}