在Java中停止执行几秒钟的最佳方法是什么? [英] What is the best way to stop execution for few seconds in java?

查看:1198
本文介绍了在Java中停止执行几秒钟的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手.我试图编写一个程序来检查数据库状态(是否过载).

I am new to Java. I tried to write a program to check database status (overloaded or not).

代码:

package com.test.service;

import java.util.LinkedList;
import java.util.List;

public class PersonImpl {

    public PersonService personService;

    public void InsertPersonDetails(Person person) {

        if(personService.isOverLoaded()) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            //pass control to dao
        }
    }
}

在这里,我将从isOverLoaded中获取boolean.如果isOverLoaded值为yes,那么我们应该停止将控制传递给dao几秒钟,然后再次检查isOverLoaded方法.

Here I will get boolean from isOverLoaded. If isOverLoaded value is yes then we should stop passing control to dao for few seconds and then check isOverLoaded method again.

我们将不胜感激:)

推荐答案

我会去

I would go for an ScheduledExecutorService. Create a thread and as long as isOverloaded() returns true re schedule the execution.

这篇关于在Java中停止执行几秒钟的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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