使用sleep方法暂停一个线程

2023-05-16

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Pausing Execution with Sleep

使用sleep方法暂停一个线程 

Thread.sleep causes the current thread to suspend execution for a specified period. This is an efficient means of making processor time available to the other threads of an application or other applications that might be running on a computer system. The sleep method can also be used for pacing, as shown in the example that follows, and waiting for another thread with duties that are understood to have time requirements, as with the SimpleThreads example in a later section.使用Thread.sleep()方法可以暂停当前线程一段时间。这是一种使处理器时间可以被其他线程或者应用程序使用的有效方式。sleep()方法还可以用于调整线程执行节奏(见下面的例子)和等待其他有执行时间需求的线程(这个例子将在下一节演示)。

Two overloaded versions of sleep are provided: one that specifies the sleep time to the millisecond and one that specifies the sleep time to the nanosecond. However, these sleep times are not guaranteed to be precise, because they are limited by the facilities provided by the underlying OS. Also, the sleep period can be terminated by interrupts, as we'll see in a later section. In any case, you cannot assume that invoking sleep will suspend the thread for precisely the time period specified.在Thread中有两个不同的sleep()方法,一个使用毫秒表示休眠的时间,而另一个使用纳秒。由于操作系统的限制休眠时间并不能保证十分精确。休眠周期可以被interrupts所中断,我们将在后面看到这样的例子。不管在任何情况下,我们都不应该假定调用了sleep()方法就可以将一个线程暂停一个十分精确的时间周期。

The SleepMessages example uses sleep to print messages at four-second intervals:

SleepMessages程序为我们展示了使用sleep()方法每四秒打印一个信息的例子 

public class SleepMessages {
    public static void main(String args[])
        throws InterruptedException {
        String importantInfo[] = {
            "Mares eat oats",
            "Does eat oats",
            "Little lambs eat ivy",
            "A kid will eat ivy too"
        };

        for (int i = 0;
             i < importantInfo.length;
             i++) {
            //Pause for 4 seconds
            Thread.sleep(4000);
            //Print a message
            System.out.println(importantInfo[i]);
        }
    }
}

Notice that main declares that it throws InterruptedException. This is an exception that sleep throws when another thread interrupts the current thread while sleep is active. Since this application has not defined another thread to cause the interrupt, it doesn't bother to catch InterruptedException.  main()方法声明了它有可能抛出InterruptedException。当其他线程中断当前线程时,sleep()方法就会抛出该异常。由于这个应用程序并没有定义其他的线程,所以并不用关心如何处理该异常。

转载于:https://my.oschina.net/u/553266/blog/295098

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用sleep方法暂停一个线程 的相关文章

  • 为什么我的所有线程都使用 sleep() 进行睡眠?

    我在网上看到了下面一段关于 Linux 线程的代码 但是当我运行它时 所有线程似乎都在睡眠 而不仅仅是主线程 为什么 另外 如果没有 sleep 5 线程创建成功 语句会运行 3 次而不是 2 次 有人可以解释一下这种行为吗 谢谢 编译使用
  • Windows 7:超出 C++ std::this_thread::sleep_for

    我们的代码是用 C 11 VS2012 Win 7 64bit 编写的 C 库提供了sleep for我们使用的函数 我们观察到 C sleep for有时会出现较大的超调 换句话说 我们请求睡眠 15 毫秒 但睡眠结果是例如 15 毫秒
  • 如何让 Pygame 停止几秒钟?

    我知道这是一个简单的问题 我可以使用time功能或其他东西 但这对我来说仍然是一个问题 所以我有这个 letter pygame image load w00 png screen blit letter letter x 625 lett
  • Windows 命令中的“2>&1”有什么作用?

    对脚本进行一些维护 我发现了这一行 ping n 40 127 0 0 1 gt NUL 2 gt 1 I know 从这个问题 https stackoverflow com questions 1672338 how to sleep
  • Thread.sleep() 永远不会返回

    我有一个奇怪的错误Thread sleep 关于Java 由于某种原因 当我在某些机器上调用 sleep 时 它永远不会返回 我无法弄清楚是什么导致了这种行为 起初 我认为错误可能在我的代码中的其他地方 所以我做了最简单的睡眠测试 publ
  • iOS的睡眠功能是什么

    我正在寻找一个函数 可以让 iOS 上的 C 代码甚至 Objective C 代码的线程进入休眠状态 我的编译器无法识别 sleep 0 Thanks 阻止 0 5 秒 NSThread sleepForTimeInterval 5
  • 我怎样才能保证 Thread.sleep 至少休眠那么多时间?

    As per 这个问题 https stackoverflow com q 45419940 1898563 Thread sleep不一定保证在您指定的时间内睡眠 它可能会更短or longer 如果您阅读了文档Thread sleep
  • 为什么我要使用无限超时的 Sleep() ?

    据 MSDN 报道 Sleep http msdn microsoft com en us library ms686298 VS 85 aspx可以提供INFINITE值并且 表明暂停不应超时 为什么我要调用 Sleep INFINITE
  • java中如何让线程休眠特定时间?

    我有一个场景 我希望线程休眠特定的时间 Code public void run try do something Thread sleep 3000 do something after waking up catch Interrupt
  • Python 杀死线程

    我正在尝试杀死 python 中的一个线程 异常将是执行此操作的首选方法 因为通过 try except 对优雅地退出线程的 run 方法将允许关闭资源 我试过 Python 有没有办法杀死一个线程 https stackoverflow
  • Bash:无限睡眠(无限阻塞)

    I use startx启动 X 它将评估我的 xinitrc In my xinitrc我使用启动窗口管理器 usr bin mywm 现在 如果我终止我的 WM 为了测试其他 WM X 也会终止 因为 xinitrc脚本到达 EOF 所
  • Android手机休眠时网络访问

    我正在使用警报组合 设置为AlarmManager 和后台服务定期同步我的应用程序中的数据 我遇到的唯一问题是 当睡眠策略终止 Wi Fi 连接时 同步将不再起作用 有没有办法 唤醒 已进入睡眠状态的 Wi Fi 连接 GMail 以某种方
  • 当 iPhone 处于睡眠模式时,iPhone 应用程序延迟 10 -15 分钟

    我创建了一个使用 NSTimer 的应用程序 它每秒都会触发一次 我的问题是 如果 Iphone 处于睡眠模式 我会延迟 10 到 15 事件触发前的分钟数 我已经 stackoverflowed 并用谷歌搜索了这个 其原因似乎是手机在某些
  • 定时器在待机模式下会发生什么情况? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 如何等到File.Exists?

    我有一个应用程序 正在侦听所选文件夹中的 log 文件 我用了FileSystemWatcher 但有一个问题 负责制作该文件的另一个应用程序采取以下步骤 制作一个 gz 文件 解压到txt文件 一些随机文件名 将 txt 名称更改为带有
  • “时钟”是否测量“睡眠”,即挂起的线程?

    我正在尝试理解clock t clock void 功能更好 有以下问题 我理解正确吗clock测量进程的滴答数 因为它是积极奔跑 and sleep挂起调用线程 在这种情况下只有一个线程 即主线程 因此挂起整个进程 意思就是clock不测
  • sleep 0 有特殊含义吗?

    我看到很多用法sleep 0在我的一个客户项目中 代码看起来像这样 while true sleep 0 end 阅读一些像这样的答案this https stackoverflow com questions 3727420 signif
  • 如何在 javascript/jquery 中进行非阻塞睡眠?

    如何在 javascript jquery 中进行非阻塞睡眠 冒着从评论者那里窃取答案的风险 请使用设置超时 https developer mozilla org en US docs Web API WindowTimers setTi
  • Java Thread.sleep 泄漏线程?

    因此 我继承了一些等待来自网络源的通信的代码 当它等待来自网络套接字的更多数据时 Thread sleep 10 叫做 这似乎导致了线程泄漏 正如 jconsole 和我的线程转储所报告的那样 Thread 68 Thread 385 等有
  • Intel 上的 gcc 中的 _mm_pause 用法

    我参考过这个网页 https software intel com en us articles benefitting power and performance sleep loops https software intel com

随机推荐