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

Does Java Thread.sleep() release the processor?

我在服务器中有 CPUx2,我有一个包含许多线程的程序,如果所有线程都需要很长时间来做某事,是否可以使用 Thread.Sleep(10)为了让 CPU 将作业释放到另一个线程中?我可以只使用 thread.sleep 并让 CPU 自动切换另一个线程以改善或增强性能吗?

更新于 2016/06/06: 每个线程都专注于使用Executors从互联网上获取HTTP内容,而我想给它延迟更多的时间来做,但是我不确定我在代码中添加TimeUnit.MILLISECONDS.sleep(10)时是否使用任何一个MILLISECONDS 或 NANOSECOND 以及有多少时隙让 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() 将处理器释放给另一个可运行的线程或进程,并将当前线程标记为不可运行,直到睡眠时间到期。

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