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

查看:156
本文介绍了NetBeans / Java / New提示:在循环中调用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();
}
}


推荐答案

致电在循环中睡眠通常会导致性能不佳。例如:

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.

Something像这样:

Something like this:

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 / New提示:在循环中调用Thread.sleep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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