Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MyTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Thread t1 = new Thread(() -> {
- while (true) {
- System.out.println("Enter command...");
- String cmd = scanner.nextLine();
- if (Thread.interrupted()) {
- System.out.println(
- "Sorry, your command is disregarded, because your time has left!");
- }
- System.out.println("You have entered " + cmd);
- }
- });
- Thread t2 = new Thread(() -> {
- int x = 10;
- while (x > 0) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- x--;
- System.out.println("Seconds " + x);
- }
- t1.interrupt();
- System.out.println("You cannot enter new commands!");
- });
- t1.start();
- t2.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement