Input using thread

主宰稳场 提交于 2019-12-11 13:03:33

问题


import java.util.Scanner;

public class ThreadClass {

    public static void main(String[] args) 
    {
        System.out.println("Type the following in 5 seconds");
        System.out.println("The quick brown fox jumps over the lazy dog");

        try {
            Scanner sc = new Scanner(System.in);
            String str=sc.nextLine();
            Thread.sleep(1000);
            System.out.println("Your time is over");

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

In the above program, I want that my program should ask the user to enter input within five seconds(or 5000 milliseconds whatever) and should automatically close and display a message "Your time is over" after the time is over. The problem with this code is that whenever the user enters the input until and unless the Enter key is pressed the thread doesn't seem to work. I have tried tho write the Scanner code outside of try catch block and even below thread but none of them seems to work. I think the problem is with my Scanner class but cannot figure out how to input data otherwise.


回答1:


Just a hint: you'll have to have 2 threads - one for the input and another for the timer. once the timer expires, interrupt the input thread. For example:

final Thread subject1 = new Thread(new Runnable() {

  public void run() {
    while (!Thread.interrupted()) {
      Thread.yield();
    }
    System.out.println("subject 1 stopped!");
  }
});
final Thread subject2 = new Thread(new Runnable() {

  public void run() {
    while (!Thread.interrupted()) {
      Thread.yield();
    }
    System.out.println("subject 2 stopped!");
  }
});
final Thread coordinator = new Thread(new Runnable() {

  public void run() {
    try {
      Thread.sleep(500);
    } catch (InterruptedException ex) { }
    System.out.println("coordinator stopping!");
    subject1.interrupt();
    subject2.interrupt();
  }
});
subject1.start();
subject2.start();
coordinator.start();



回答2:


why don't you try this code by calculating time if your only motive is to check weather user inserts input in 5 seconds or not

Scanner sc = new Scanner(System.in);
        long begantime = System.nanoTime();
        System.out.println("Enter something");
        String input = sc.nextLine();
        int totalTime = (System.nanoTime()-beganTime)/1000000000;
        if (time > 5)
            System.out.println("failed");
        else
            System.out.println(input);



回答3:


import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

public class Test {
    static volatile boolean isTimeCopleted = true;

    public static void main(String[] args) {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                if (isTimeCopleted) {
                    System.out.println("Your time is over");
                    System.exit(0);
                }
            }
        };
        timer.schedule(task, 5000);

        System.out.println("Type the following in 5 seconds");
        System.out.println("The quick brown fox jumps over the lazy dog");

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        isTimeCopleted = false;


        System.out.println("Process Done");
        System.exit(0);
    }
}


来源:https://stackoverflow.com/questions/37135654/input-using-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!