NetBeans/Java/新提示:在循环中调用 Thread.sleep [英] NetBeans / Java / New hint: Thread.sleep called in loop

查看:29
本文介绍了NetBeans/Java/新提示:在循环中调用 Thread.sleep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 NetBeans 中,有一个新提示说:Thread.sleep 在循环中调用.

In NetBeans, there's a new hint that says: Thread.sleep called in loop.

问题 1:如何/何时会出现循环睡眠的问题?

Question 1: How/when can it be a problem to sleep in a loop?

问题 2:如果有问题,我该怎么办?

Question 2: If it's a problem, what should I do instead?

更新:问题 3: 这是一些代码.在这种情况下,告诉我是否应该在循环中使用其他东西而不是 Thread.Sleep.简而言之,这是由侦听客户端 TCP 连接的服务器使用的.如果达到与客户端的最大会话数,则此处使用睡眠.在这种情况下,我希望应用程序等待免费会话可用.

UPDATE: Question 3: Here's some code. Tell me in this case if I should be using something else instead of Thread.Sleep in a loop. In short, this is used by a server which listens for client TCP connections. The sleep is used here in case the max number of sessions with clients is reached. In this situation, I want the application to wait until a free session becomes available.

public class SessionManager {
    private static final int DEFAULT_PORT = 7500;
    private static final int SLEEP_TIME = 200;
    private final DatabaseManager database = new DatabaseManager();
    private final ServerSocket serverSocket = new ServerSocket(DEFAULT_PORT);

public SessionManager() throws IOException, SQLException
{
}

public void listen()
{
while (true)
    if (Session.getSessionCount() < Session.getMaxSessionCount())
        try
        {
             new Thread(new Session(database, serverSocket.accept())).start();
        }
        catch (IOException ex) { ex.printStackTrace(); }
    else
        try
        {
            Thread.sleep(SLEEP_TIME);
        }
        catch (InterruptedException ex) { ex.printStackTrace(); }
}

public static void main(String[] args) throws IOException, SQLException
{
new SessionManager().listen();
}
}

推荐答案

在循环中调用 sleep 通常会导致性能不佳.例如:

Calling sleep in a loop typically leads to poor performance. For example:

while (true) {
    if (stream.available() > 0) {
       // read input
    }
    sleep(MILLISECONDS);
}

如果 MILLISECONDS 太大,那么这段代码需要很长时间才能意识到输入可用.

If MILLISECONDS is too large, then this code will take a long time to realize that input is available.

如果 MILLISECONDS 太小,那么这段代码会浪费大量的系统资源来检查尚未到达的输入.

If MILLISECONDS is too small, then this code will waste a lot of system resources check for input that hasn't arrived yet.

sleep 在循环中的其他使用通常也是有问题的.通常有更好的方法.

Other uses of sleep in a loop are typically questionable as well. There's usually a better way.

如果有问题,我该怎么办?

If it's a problem, what should I do instead?

把代码贴出来,也许我们可以给你一个合理的答案.

Post the code and maybe we can give you a sensible answer.

编辑

IMO,解决问题的更好方法是使用 ThreadPoolExecutor.

IMO, a better way to solve the problem is to use a ThreadPoolExecutor.

像这样:

public void listen() {
    BlockingQueue queue = new SynchronousQueue();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(
            1, Session.getMaxSessionCount(), 100, TimeUnit.SECONDS, queue);
    while (true) {
        try {
            queue.submit(new Session(database, serverSocket.accept()));
        } catch (IOException ex) { 
            ex.printStackTrace();
        }
    }
}

这会配置执行器以匹配您的代码当前的工作方式.您可以通过多种其他方式做到这一点;请参阅上面的 javadoc 链接.

This configures the executor to match the way your code currently works. There are a number of other ways you could do it; see the javadoc link above.

这篇关于NetBeans/Java/新提示:在循环中调用 Thread.sleep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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