Java Thread.sleep()是否释放了处理器?

Java Thread.sleep()是否释放了处理器?,java,multithreading,jvm,thread-safety,cpu,Java,Multithreading,Jvm,Thread Safety,Cpu,我在服务器中有一个CPUx2,我有一个包含许多线程的程序,如果所有线程都需要很长时间才能完成某件事情,是否可以使用Thread.Sleep(10)让CPU将作业释放到另一个线程中?我可以使用thread.sleep吗?它会让CPU自动切换其他线程以提高性能吗 更新日期:2016/06/06: 每个线程都专注于通过执行器从互联网上获取HTTP内容,虽然我想给它更多的延迟时间,但我不太确定我在代码中添加了TimeUnit.millizes.sleep(10)是使用毫秒还是纳秒,以及有多少时间槽让CP

我在服务器中有一个CPUx2,我有一个包含许多线程的程序,如果所有线程都需要很长时间才能完成某件事情,是否可以使用Thread.Sleep(10)让CPU将作业释放到另一个线程中?我可以使用thread.sleep吗?它会让CPU自动切换其他线程以提高性能吗

更新日期:2016/06/06: 每个线程都专注于通过执行器从互联网上获取HTTP内容,虽然我想给它更多的延迟时间,但我不太确定我在代码中添加了TimeUnit.millizes.sleep(10)是使用毫秒还是纳秒,以及有多少时间槽让CPU自动切换另一个线程,以使整体表现公平:

@Override
public void run() {
    //this.RunActual = System.currentTimeMillis();

    if ("Started".equals(this.JobStatus)) {
        String startDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").format(Calendar.getInstance().getTime());
        System.out.println(this.HttpRequestAddress + " has started at " + startDate);
        try {
            this.url = new URL("http://" + this.HttpRequestAddress + ":" + this.HttpRequestPort);
            this.conn = (HttpURLConnection) this.url.openConnection();
            this.conn.setRequestMethod(this.HttpRequestMethod);
            this.conn.setReadTimeout(this.HttpRequestReadTimeout);
            this.conn.setConnectTimeout(this.HttpRequestConnectionTimeout);
            this.conn.setInstanceFollowRedirects(false);
            for (HttpHeader hh : this.HttpRequestHeader) {
                this.conn.setRequestProperty(hh.Name, hh.Value);
            }
            this.conn.connect();
            this.responseCode = 0;
            this.responseCode = this.conn.getResponseCode();
            System.out.println(this.HttpRequestAddress + " has response header " + this.conn.getHeaderFields().toString());
            if (this.responseCode == HttpURLConnection.HTTP_OK) {
                if (this.HttpResponseKeyword != null) {
                    boolean hasKeyword = false;
                    this.br = new BufferedReader(new InputStreamReader(this.conn.getInputStream(), this.httpResponseEncoding));
                    while ((this.charRead = this.br.read(this.buffer, 0, this.BUFFER_SIZE)) > 0) {
                        this.sb.append(this.buffer, 0, this.charRead);
                        //System.out.println(this.sb.toString());
                        if (this.HttpResponseContain && this.sb.indexOf(this.HttpResponseKeyword) > 0) {
                            hasKeyword = true;
                            break;
                        }
                        this.sb.setLength(0);
                        TimeUnit.MILLISECONDS.sleep(10);
                    }
                    if (this.HttpResponseContain && hasKeyword) {
                        System.out.println(this.HttpRequestAddress + " should include keyword, now it is included.");
                    } else if (this.HttpResponseContain && !hasKeyword) {
                        System.out.println(this.HttpRequestAddress + " should include keyword, but it is not included.");
                    } else if (!this.HttpResponseContain && !hasKeyword) {
                        System.out.println(this.HttpRequestAddress + " should not include keyword, now it is not included.");
                    } else if (!this.HttpResponseContain && hasKeyword) {
                        System.out.println(this.HttpRequestAddress + " should not include keyword, but it is included.");
                    }
                }
            }
        } catch (Exception ex) {
            String errorDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").format(Calendar.getInstance().getTime());
            System.err.println(this.HttpRequestAddress + " has error at " + errorDate + " with " + ex.toString());
        }
        String endDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").format(Calendar.getInstance().getTime());
        System.out.println(this.HttpRequestAddress + " has end at " + endDate);
    }

    /*
    if (this.RunNext != 0) {
        long c = this.RunActual - this.RunNext;
        if (c > 0) {
            System.out.println(this.HttpRequestAddress + " has slowed " + c + " milliseconda.");
        }
    }*/

    //this.RunNext = System.currentTimeMillis() + this.JobInterval;
}

正如您所猜测的,调用
Thread.sleep()
可以释放大多数主要操作系统中的处理器。但是,在大多数操作系统上,您不需要调用
Thread.sleep()
来释放处理器,因为操作系统会每隔一段时间切换到其他线程。使用
Thread.sleep()
相对于不断检查完成条件的循环可能会提高效率,但更好的解决方案通常是使用等待和通知,这样线程可以在需要时唤醒,而不必每隔一段时间进行检查。

如您所猜测,调用
Thread.sleep()
在大多数主要操作系统中释放处理器。但是,在大多数操作系统上,您不需要调用
Thread.sleep()
来释放处理器,因为操作系统会每隔一段时间切换到其他线程。使用
Thread.sleep()
相对于不断检查完成条件的循环,可能会提高效率,但更好的解决方案通常是使用等待和通知,这样线程可以在需要时唤醒,而不必每隔一段时间进行检查。

Thread.sleep()
将处理器释放到另一个可运行线程或进程,并在睡眠时间到期之前将当前线程标记为不可运行

但是,在重新编辑时,代码的大部分时间都会在从网络读取时被阻止。在这段代码中添加睡眠是完全没有意义的。不要。操作系统已经知道如何调度,TCP已经知道如何共享带宽。

Thread.sleep()
将处理器释放到另一个可运行的线程或进程,并将当前线程标记为不可运行,直到睡眠时间到期


但是,在重新编辑时,代码的大部分时间都会在从网络读取时被阻止。在这段代码中添加睡眠是完全没有意义的。不要。操作系统已经知道如何调度,TCP已经知道如何共享带宽。

这个问题太广泛了。这取决于线程在做什么。有些人可能会问,为什么要使用大量线程。请给出问题的代码示例,说明您的问题您试图解决的问题是什么?如果线程100%使用您的CPU,那么它们将以尽可能快的速度运行,添加
sleep()
对总体性能没有帮助。这些工作还需要完成。您是否试图“限制”它们,降低它们的速度,以便它们使用更少的CPU?为什么?如果只是希望其他程序正常运行,请降低线程的线程优先级。这样,它们将使用所有可用的CPU,但仍允许其他程序以最小的延迟运行。几乎没有任何好的理由调用
Thread.sleep()
。如果您在实时应用程序中使用它使事情在适当的时候发生,那么最好使用标准JRE的
ScheduledThreadPoolExecutor
类或一些GUI框架的
Timer
类。如果您使用它来平衡不同线程完成的工作量,那么可能还有其他更好的方法。如果你用它来确保事情按正确的顺序发生,那么你可能犯了一个可怕的错误。这个问题太广泛了。这取决于线程在做什么。有些人可能会问,为什么要使用大量线程。请给出问题的代码示例,说明您的问题您试图解决的问题是什么?如果线程100%使用您的CPU,那么它们将以尽可能快的速度运行,添加
sleep()
对总体性能没有帮助。这些工作还需要完成。您是否试图“限制”它们,降低它们的速度,以便它们使用更少的CPU?为什么?如果只是希望其他程序正常运行,请降低线程的线程优先级。这样,它们将使用所有可用的CPU,但仍允许其他程序以最小的延迟运行。几乎没有任何好的理由调用
Thread.sleep()
。如果您在实时应用程序中使用它使事情在适当的时候发生,那么最好使用标准JRE的
ScheduledThreadPoolExecutor
类或一些GUI框架的
Timer
类。如果您使用它来平衡不同线程完成的工作量,那么可能还有其他更好的方法。如果你用它来确保事情按正确的顺序发生,那么你可能犯了一个可怕的错误。