让线程在运行之前先休眠
如何让我的线程在运行之前先休眠?我知道如何让睡眠工作,但是,每当我的程序运行时,线程就会立即运行。我希望它在首次创建后等待开始运行。 (我正在使用处理程序)
How can I make my thread sleep first before it runs? I know how to get sleep to work, however, whenever my program is run, the thread immediately runs. I want it to WAIT once it is first created to start running. (I am using handlers)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法控制线程的调度时间。如果您希望它进入睡眠状态,请让线程子例程中的第一个语句等待某个条件或类似的操作,当您准备好时,您可以广播该条件。在伪代码中:
我想你可以让父级在创建子级时保持锁定,然后他们所要做的就是:
并避免条件问题。
You cannot control when threads are scheduled. If you want it to go to sleep, have the first statement in the thread subroutine do a wait on a condition or something like that and when you are ready you can broadcast to that condition. In pseudo-code:
I suppose you could have the parent hold the lock while creating the children and then all they have to do is:
and avoid the condition thing.
什么操作系统? Windoze 允许您创建处于挂起状态的线程。当你加载了ctor中的线程字段后,你就可以恢复线程了。如果失败,请在线程启动参数中传递一些同步对象,以便新线程等待。
平均值,
马丁.
What OS? Windoze allows you to create threads in a suspended state. When you have loaded up the thread fields in the ctor, you can resume the thread. Failing that, pass some synchro object in the thread start parameter for the new thread to wait on.
Rgds,
Martin.