让java程序在没有线程的情况下休眠 [英] Make a java program sleep without threading

查看:247
本文介绍了让java程序在没有线程的情况下休眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java程序进行一些计算,然后将结果上传到MYSQL数据库(托管在同一网络中的另一台计算机上)。我有时会面临程序执行计算的速度快于上传结果的问题。因此无法上传所有结果。该程序目前没有线程。

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?

谢谢

推荐答案


有没有办法让程序在完成计算后睡几毫秒,以便正确上传。 (与其他语言一样,睡眠或等待功能)

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(毫秒)是一个公共静态方法,也适用于单线程程序。类似下面的东西是典型的模式:

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;
}

无需实现 Runnable 或用线程调用做任何其他事情。您可以随时调用它来暂停某些代码。

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.

这篇关于让java程序在没有线程的情况下休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆