Skip to content
Snippets Groups Projects
Commit 67c12cc1 authored by Sven Steddin's avatar Sven Steddin
Browse files

Erweiterung Beispiele

parent 408769e2
No related branches found
No related tags found
No related merge requests found
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);
}
}
}
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();
}
}
}
}
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");
}}}}
package MyThreads;
public class ThreadDurchInterface_3mitSleep {
public static void main(String[] args) {
//zuerst die Objekte anlegen, die von Thread nicht erben knnen
//aber dafr das Interface Runnable untersttzen:
MyOutputClassChild myRunnable1 = new MyOutputClassChild("Hallo",4);
MyOutputClassChild myRunnable2 = new MyOutputClassChild("Meti",0);
//nun alle gewnschten 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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment