Make a java program sleep without threading

假装没事ソ 提交于 2019-12-21 03:42:32

问题


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


回答1:


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.




回答2:


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.




回答3:


Use Thread.sleep():

try
{
    Thread.sleep(1000); // Sleep for one second
}
catch (InterruptedException e)
{
    Thread.currentThread().interrupt();
}

This does not introduce a new Thread into the program or require any other Thread related machinery.




回答4:


Just use:-

try
{
Thread.sleep(<time in ms>);
}
catch(InterruptedException ex}
{
}

This will make the current thread (e.g. main thread) sleep.




回答5:


The static method sleep() in the java.lang.Thread class pauses the current thread -- which could just be the main thread -- when called. You don't need to do anything special to use it.




回答6:


Every Java application executes in a thread in the JVM. Calling the static method Thread.sleep will cause your application's thread (the one that is running) to stop




回答7:


Your program is not Multi-threaded...but it is using a thread. Thread.Sleep will still work.




回答8:


You can use Thread.sleep(); after the calculation without the need to rewrite your whole program with threads!



来源:https://stackoverflow.com/questions/9439110/make-a-java-program-sleep-without-threading

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