diff --git a/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyInputClass.java b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyInputClass.java new file mode 100644 index 0000000000000000000000000000000000000000..9fb41994d3b1ec2abcf35f09d26bacd4afbe240e --- /dev/null +++ b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyInputClass.java @@ -0,0 +1,21 @@ +package MyThreads; +import java.util.Scanner; + +public class MyInputClass extends Thread { + + public void run() { + Scanner myScanner = new Scanner(System.in); + int ii = 0; + while (true) { + try { + sleep(200); + } catch (InterruptedException e) { + e.printStackTrace(); + } + ii = myScanner.nextInt(); + System.out.println("\nEingabe: " + ii); + } + } +} + + diff --git a/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyOutputClass.java b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyOutputClass.java new file mode 100644 index 0000000000000000000000000000000000000000..da1351c2dc7ad801e1d9f7c8d5f72b1c2f202e3e --- /dev/null +++ b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyOutputClass.java @@ -0,0 +1,30 @@ +package MyThreads; + +public abstract class MyOutputClass { + private String text; + int cnt = 0; + + MyOutputClass(String text) + { + this.text = text; + } + + protected abstract void MachWas(); + + public void run() { + long kk = 0; + while (true) { + cnt++; + System.out.print(text + " "); + MachWas(); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } +} + + diff --git a/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyOutputClassChild.java b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyOutputClassChild.java new file mode 100644 index 0000000000000000000000000000000000000000..e214ff12326d771c1444d5532e1fd1cd091667aa --- /dev/null +++ b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/MyOutputClassChild.java @@ -0,0 +1,17 @@ +package MyThreads; +public class MyOutputClassChild extends MyOutputClass implements Runnable { + + private int crlfCnt = 0; + + public MyOutputClassChild(String text, int cnt) { + super(text); + crlfCnt = cnt; + } + + protected void MachWas() { + if (crlfCnt != 0) { + if (cnt % crlfCnt == 0) { + System.out.println("CRLF"); +}}}} + + diff --git a/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/ThreadDurchInterface_3mitSleep.java b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/ThreadDurchInterface_3mitSleep.java new file mode 100644 index 0000000000000000000000000000000000000000..202ac43d27d4f6d53fd306fc488711b672d13646 --- /dev/null +++ b/VL04_09/ThreadDurchInterface_3mitSleep/src/MyThreads/ThreadDurchInterface_3mitSleep.java @@ -0,0 +1,23 @@ +package MyThreads; +public class ThreadDurchInterface_3mitSleep { + + public static void main(String[] args) { + + //zuerst die Objekte anlegen, die von Thread nicht erben können + //aber dafür das Interface Runnable unterstützen: + MyOutputClassChild myRunnable1 = new MyOutputClassChild("Hallo",4); + MyOutputClassChild myRunnable2 = new MyOutputClassChild("Meti",0); + + //nun alle gewünschten Threads anlegen: + MyInputClass myThread1 = new MyInputClass(); + Thread myThread2 = new Thread(myRunnable1); + Thread myThread3 = new Thread(myRunnable2); + + //Thread-Objekte starten + myThread1.start(); + myThread2.start(); + myThread3.start(); + } +} + +