将当前线程睡眠 [英] Putting the current thread to sleep

查看:98
本文介绍了将当前线程睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我做在一个线程(不是主线程)的工作单元。在某些情况下,我想将这个线程睡眠10秒。是Thread.sleep代码(10000)资源最有效的方式做到这一点?


解决方案

是线程。睡眠(10000)资源最有效的方式做到这一点?




是在这个意义上,它不是忙等待,但给人了CPU。



但它浪费线程。你不应该扩展这许多休眠的线程。


I have a unit of work I'm doing in a thread (not the main thread). Under certain circumstances I would like to put this thread to sleep for 10 seconds. Is Thread.Sleep(10000) the most resource efficient way to do this?

解决方案

Is Thread.Sleep(10000) the most resource efficient way to do this?

Yes in the sense that it is not busy-waiting but giving up the CPU.

But it is wasting a Thread. You shouldn't scale this to many sleeping threads.

这篇关于将当前线程睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆