Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a java program sleep without threading

I have a java program that does some calculations and then uploads the results to a MYSQL database (hosted in another computer in the same network). I sometimes face the problem that the program does calculations faster than it uploads the result. Therefore it is not able to upload all the results. The program currently is not threaded.

Is there a way I can make the program sleep for a few milliseconds after it has done the calculations so that the upload takes place properly. (Like in other languages sleep or wait function)

I can thread the program but that will be too much rewriting. Is there an easier way?

Thanks

like image 406
Ank Avatar asked Feb 24 '12 22:02

Ank


People also ask

How do you make a Java program sleep?

The easiest way to delay a java program is by using Thread. sleep() method. The sleep() method is present in the Thread class. It simply pauses the current thread to sleep for a specific time.

What can I use instead of sleep in Java?

TimeUnit provides a human-readable version of the Thread. sleep() method which can be used in place of the former. For a long time Thread's sleep() method is a standard way to pause a Thread in Java and almost every Java programmer is familiar with that.

How do you stop sleep threads in Java?

interrupt() method: If any thread is in sleeping or waiting for a state then using the interrupt() method, we can interrupt the execution of that thread by showing InterruptedException. A thread that is in the sleeping or waiting state can be interrupted with the help of the interrupt() method of Thread class.

Can sleep method cause another thread to sleep Java?

sleep() Method: Method Whenever Thread. sleep() functions to execute, it always pauses the current thread execution. If any other thread interrupts when the thread is sleeping, then InterruptedException will be thrown.


2 Answers

Is there a way I can make the program sleep for a few milliseconds after it has done the calculations so that the upload takes place properly. (Like in other languages sleep or wait function)

Thread.sleep(milliseconds) is a public static method that will work with single threaded programs as well. Something like the following is a typical pattern:

try {
    // to sleep 10 seconds
    Thread.sleep(10000);
} catch (InterruptedException e) {
    // recommended because catching InterruptedException clears interrupt flag
    Thread.currentThread().interrupt();
    // you probably want to quit if the thread is interrupted
    return;
}

There is no need to implement Runnable or do anything else with thread calls. You can just call it anytime to put a pause in some code.

like image 170
Gray Avatar answered Oct 17 '22 07:10

Gray


You don't have to re-thread or any such thing. All you need to do is call:

Thread.Sleep(5000); // pause the app for 5 seconds

Every application is also a thread, in your case also called a single-threaded app. You can use a threading API like Sleep w/o any other code or refactoring.

Big note of caution though: If you need to use Thread.Sleep to manage your control flow then there's probably something going wrong architecturally. From your OP I am concerned than in what you describe as a single-threaded app you seem to have one operation "outpacing" another. This should not be possible, unless you're receiving asynch events from elsewhere.

Another caveat: Sleep takes a millisecond parameter that's usually arbitrary and just means "wait a little while." The problem is that a "little while" may be OK today but tomorrow your machine will be under greater load and "a little while" will no longer be good enough, your Sleep will lapse and the same error will appear. Sure, you can set the time out to "a long while" but then you'll be waiting "a long while" for each and every transaction... Catch 22.

like image 28
Paul Sasik Avatar answered Oct 17 '22 06:10

Paul Sasik