user2638464
user2638464

Reputation:

Timeout Method in Java

So my question is really not important, but I'm writing a program that solves all sorts of algebraic equations. I'm using Java, which I'm fairly new to, and I wondered if there was any method like SetTimeout() in JavaScript. This is the language I've been using for quite a while, and I've used it a lot.

My actual program consists of lots of code and like 10 different files, so I can't post all of it here to show you exactly what I'm doing, but is there a simple way to set a timeout? Basically, at the end of my program, I want to print "Program has Ended" and then wait 5 seconds and go System.exit(0).

I know how to print the words on the screen and end the program, just not how to wait five seconds in between.

Upvotes: 0

Views: 78

Answers (2)

Prateek
Prateek

Reputation: 1926

What you actually want to get done is make the main thread sleep for a specific period of time which you can accomplish through Thread.sleep(time_in_milliseconds). For your case it is 5 seconds or 5000 milliseconds

Upvotes: 1

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62854

You can try using Thread.sleep(long millis), which

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

For example:

System.out.println("Program has Ended");
try {
   Thread.sleep(5000); //5000 milliseconds = 5 seconds.
} catch (InterruptedException e) {
   e.printStackTrace();
}
System.exit(0);

Upvotes: 2

Related Questions