Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a98339b5ce | ||
|
|
8406ceaaa8 |
@@ -1,10 +1,22 @@
|
|||||||
package lecture.multithreading.timeout;
|
package lecture.multithreading.timeout;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
System.out.println("You Only Have 10 Seconds...");
|
||||||
|
System.out.print("Enter Your Name: ");
|
||||||
|
|
||||||
|
MyRunnable myRunnable = new MyRunnable();
|
||||||
|
Thread thread = new Thread(myRunnable);
|
||||||
|
thread.setDaemon(true);
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
String name = scanner.nextLine();
|
||||||
|
System.out.println("Hello " + name + "!");
|
||||||
|
|
||||||
|
scanner.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package lecture.multithreading.timeout;
|
||||||
|
|
||||||
|
public class MyRunnable implements Runnable {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (int i = 0; i <= 10; i++) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println("Thread Was Interrupted");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == 10) {
|
||||||
|
System.out.println("\nTime is Up!");
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user