Reputation: 93
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.
Upvotes: 0
Views: 7433
Reputation: 630
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);
}
}
Upvotes: 0
Reputation: 1196
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);
Upvotes: 1
Reputation: 10561
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();
Upvotes: 3