Aufgabe 1.2 bearbeitet
This commit is contained in:
parent
2e7bf12166
commit
526808525a
|
@ -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.");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue