当它合理使用Thread.sleep()方法? [英] When is it sensible to use Thread.Sleep()?

查看:105
本文介绍了当它合理使用Thread.sleep()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是看到人们使用 Thread.sleep代码()创建加工或类似的东西,人们延迟总是嘲笑了使用这种方式。

I always see people using Thread.Sleep() for creating delays in processing or something similar and people are always derided for using it this way.

当是明智的/使用所需 Thread.sleep代码()

When is it sensible/required to use Thread.Sleep()?

推荐答案

您应该叫视频下载()当你真正需要在后台线程的延迟。

You should call Thread.sleep() when you actually need a delay in a background thread.

别叫它来帮助同步(也不会),不要把它在一个循环中等待的东西(它会慢),从来没有把它在UI线程(它会冻结)

Do not call it to help synchronization (it won't), don't call it in a loop to wait for something (it'll be slow) and never call it on a UI thread (it'll freeze).

这篇关于当它合理使用Thread.sleep()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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