I want to give a time to specific code. Means like a game. It is something like "Give answer in 20 seconds, otherwise game will be over". More specifically i just want to give time to user to give the input. I've searched a lot, tried a lot like java.util.timer and all. But I'm new at java so i couldn't do that. Can anyone help me with this??
Asked
Active
Viewed 76 times
-3
-
2If you've tried something and need help, then you will want to show the fruits of your efforts -- please show your pertinent code. You will also want to give more detail about your problem -- is this a GUI? If so what GUI library? Swing? JavaFx? Other? – Hovercraft Full Of Eels Jul 15 '15 at 15:36
-
try to "learn" about **Threads**. your answer lies beneath it :) – nafas Jul 15 '15 at 15:40
-
Again, please tell us more details about your problem. For all we know, this could be a Swing GUI, and if so, the best solution would change radically from the answers that you're seeing (in this situation you'd need a Swing Timer). So you see, the quality of the answers that you get will all depend on the quality of your question, and if you want decent answers with solutions that will work for you and in your situation, then please improve this question. – Hovercraft Full Of Eels Jul 15 '15 at 16:08
-
................. or not. – Hovercraft Full Of Eels Jul 15 '15 at 20:17
1 Answers
0
You can use this: Thread.sleep(20000);
This code will wait 20 seconds. You can change the time in miliseconds depending on how long do you want to wait. If you do not want to freeze your code then try this lambda expression:
new Thread(() -> {
Thread.sleep(20000);
//add something to do after waiting
}).start();
It will create and start a new Thread and your code after starting the new Thread will continue without waiting.

Ladas125
- 265
- 3
- 14