ThreadsDemo

Added Interface part,
Still missing anon and lambda part
This commit is contained in:
Karsten Jeppesen
2021-02-22 07:45:55 +01:00
parent 1a88f622ad
commit 6c1a72cd70
3 changed files with 31 additions and 12 deletions

View File

@@ -5,27 +5,23 @@ public class ThreadsDemo {
ThreadsExtend myThreadA = new ThreadsExtend("A");
ThreadsExtend myThreadB = new ThreadsExtend("B");
ThreadsExtend myThreadC = new ThreadsExtend("C");
Runnable myRunnableD = new ThreadsInterface("D");
Runnable myRunnableE = new ThreadsInterface("E");
myThreadA.start();
myThreadB.start();
myThreadC.start();
Thread myThreadD = new Thread( myRunnableD );
myThreadD.start();
Thread myThreadE = new Thread( myRunnableE );
myThreadE.start();
try {
myThreadA.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
myThreadB.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
myThreadC.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -6,7 +6,15 @@ public class ThreadsExtend extends Thread {
MyString = InitString;
}
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(MyString + " running");
}
}

View File

@@ -0,0 +1,15 @@
public class ThreadsInterface implements Runnable {
private String MyString;
public ThreadsInterface(String InitString) {
MyString = InitString;
}
@Override
public void run() {
System.out.println(MyString + " Interface");
}
}