Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Object.wait而不是Thread.sleep进行时间同步_Java_Android - Fatal编程技术网

Java 使用Object.wait而不是Thread.sleep进行时间同步

Java 使用Object.wait而不是Thread.sleep进行时间同步,java,android,Java,Android,我在reliable source()中找到了这个示例: 公共类HelloIntentService扩展了IntentService{ 公共服务{ 超级(“HelloIntentService”); } @凌驾 受保护的手部内容无效(意图){ long-endTime=System.currentTimeMillis()+5*1000; while(System.currentTimeMillis()

我在reliable source()中找到了这个示例:

公共类HelloIntentService扩展了IntentService{
公共服务{
超级(“HelloIntentService”);
}
@凌驾
受保护的手部内容无效(意图){
long-endTime=System.currentTimeMillis()+5*1000;
while(System.currentTimeMillis()
问题是:为什么“睡眠”是这样实施的,而不是这样:

endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
    try {
        Thread.sleep(endTime - System.currentTimeMillis());
    } catch (Exception e) {
    }
}
endTime=System.currentTimeMillis()+5*1000;
while(System.currentTimeMillis()
建议的方法更紧凑,并且没有任何线程可以调用此对象上的“notify”。无论如何,即使有,如果时间限制没有过期,程序也会再次“进入睡眠”。那么,编写更复杂、更长的代码以获得相同结果的隐藏原因是什么呢

我见过这个问题,但它没有回答我的问题。 我也看到了这一点,但我的问题更具体

更新 真不敢相信,它为不同的语言提供了不同的代码示例(在我的例子中,为英语和俄语)。也许它只是还没有更新

请参见此屏幕截图:
Object.wait
Thread.sleep
之间存在显著差异
Object.wait
释放
线程所拥有的所有监视器和锁,而
Thread.sleep
不释放。这本身就使得每种方法都有不同的目的。通常情况下,
Thread.sleep
用于暂停执行,以便超出您控制范围的内容(如网络响应)完成某项操作,而
Object.wait
用于通过等待和通知具体事件(如消费者/生产者或发布者/订阅者模式)来同步应用程序


Object.wait
Thread.sleep
之间存在显著差异
Object.wait
释放
线程所拥有的所有监视器和锁,而
Thread.sleep
不释放。这本身就使得每种方法都有不同的目的。通常情况下,
Thread.sleep
用于暂停执行,以便超出您控制范围的内容(如网络响应)完成某项操作,而
Object.wait
用于通过等待和通知具体事件(如消费者/生产者或发布者/订阅者模式)来同步应用程序


因为他们做错了

没有理由使用
对象实现5秒睡眠。请等待
Object.wait
用于同步,而
Thread.sleep
用于睡眠

上同步此
是另一个提示,表明这一切都是糟糕的编程。除非你有一个很好的理由,在这种情况下,它应该被记录下来,但事实并非如此

此外,当我关注你的链接时,我会在他们的页面中看到:

 /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */
  @Override
  protected void onHandleIntent(Intent intent) {
      // Normally we would do some work here, like download a file.
      // For our sample, we just sleep for 5 seconds.
      try {
          Thread.sleep(5000);
      } catch (InterruptedException e) {
          // Restore interrupt status.
          Thread.currentThread().interrupt();
      }
  }
}
所以,也许他们已经意识到自己的错误并改正了


注意如何正确处理代码中断,而不是像原始问题中的两段代码那样丢弃中断。

,因为他们做得不对。

没有理由使用
对象实现5秒睡眠。请等待
Object.wait
用于同步,而
Thread.sleep
用于睡眠

上同步此
是另一个提示,表明这一切都是糟糕的编程。除非你有一个很好的理由,在这种情况下,它应该被记录下来,但事实并非如此

此外,当我关注你的链接时,我会在他们的页面中看到:

 /**
   * The IntentService calls this method from the default worker thread with
   * the intent that started the service. When this method returns, IntentService
   * stops the service, as appropriate.
   */
  @Override
  protected void onHandleIntent(Intent intent) {
      // Normally we would do some work here, like download a file.
      // For our sample, we just sleep for 5 seconds.
      try {
          Thread.sleep(5000);
      } catch (InterruptedException e) {
          // Restore interrupt status.
          Thread.currentThread().interrupt();
      }
  }
}
所以,也许他们已经意识到自己的错误并改正了


请注意,在这种情况下,代码中断是如何被正确处理的,而不是像原始问题中的两段代码那样被丢弃的。

“我在可靠的源代码中找到了这个示例”--您引用的网页上没有出现任何代码。这很好,因为这两个都是关于
IntentService
@commonware的反模式感谢您的评论,我已经更新了我的问题。它们为不同的语言提供不同的代码示例。关于服务中“睡眠”的事实——在这种情况下这并不重要,它只是使用IntentService的示例,而不是编写代码示例来完成一些理解工作。“它们为不同的语言提供不同的代码示例”-ick。我对此提出了申请,但大多数时候文档问题都被忽略了,所以我没有什么希望。今后我必须记住这一点。我为怀疑你而道歉@公用软件没关系!“我在可靠的源代码中找到了这个示例”--您引用的网页上没有出现这些代码。这很好,因为这两个都是关于
IntentService
@commonware的反模式感谢您的评论,我已经更新了我的问题。它们为不同的语言提供不同的代码示例。关于服务中“睡眠”的事实——在这种情况下这并不重要,它只是使用IntentService的示例,而不是编写代码示例来完成一些理解工作。“它们为不同的语言提供不同的代码示例”-ick。我对此提出了申请,但大多数时候文档问题都被忽略了,所以我没有什么希望。今后我必须记住这一点。我为怀疑你而道歉@公用软件没关系!您是对的,感谢您的解释和对中断处理的注意。你说得对,谢谢你的解释