gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:30:04 24 4
gpt4 key购买 nike

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

更新于 2016/06/06:每个线程都专注于使用Executors从Internet获取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() 将处理器释放给另一个可运行的线程或进程,并将当前线程标记为不可运行,直到 sleep 时间到期。

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

关于java - Java Thread.sleep() 是否释放处理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37644053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com