Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java6中等待方法结果(超时)的更优雅的方式_Java - Fatal编程技术网

在Java6中等待方法结果(超时)的更优雅的方式

在Java6中等待方法结果(超时)的更优雅的方式,java,Java,我试图在Java6中找到一种方法来等待一个方法的结果,该方法读取数据库并等待一些信息 实际代码与此非常相似: Result result = getSomeResult(); Long maxTries = 10; Long sleepTimeInMs = 2000; // 10 tries and 2000 ms for each call is 20000 ms of timeout in total int tries = 0; while (tries < maxTries &a

我试图在Java6中找到一种方法来等待一个方法的结果,该方法读取数据库并等待一些信息

实际代码与此非常相似:

Result result = getSomeResult();
Long maxTries = 10;
Long sleepTimeInMs = 2000; // 10 tries and 2000 ms for each call is 20000 ms of timeout in total

int tries = 0;
while (tries < maxTries && (result == null || result.isProcessing())) {
    Thread.sleep(sleepTimeInMs);
    result = getSomeResult();
    tries++;
}
Result=getSomeResult();
长最大值=10;
长睡眠时间inms=2000;//10次尝试,每次呼叫2000毫秒,总超时时间为20000毫秒
int=0;
而(trys
在上面的代码中,我不仅仅是在等待结果;我正在等待一个非空的结果,并且处于特定的条件下(不是处理)


谢谢

这是一种延迟运行部分代码的方法,您可以对其进行延迟

        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
          //the action you want to do after reading database
        }
    }, delay_time);

或者,您可以将读取数据库的方法设置为“异步”

packt,我猜您将来会在这里找到答案:或者在这里:@ΦXocę웃Пepeúpaツ Future wait for the method返回一些结果,但我正在等待一个“not null”且不是“processing”的结果。@GriffeyDog,对不起,我使用的是Java 6。我将编辑问题以添加此信息。