如何销毁线程、暂停/挂起线程、恢复/重新运行线程?
嘿伙计们 我在 android 应用程序中的 oncreate 之外使用 runnable,其中我使用线程来设置 ProgressBar 的进度。我不知道的是,当按下停止按钮时如何停止/销毁线程,因为 thread.stop 不是一种方法,以及如何从中恢复,甚至如何销毁线程。
我知道我必须在可运行中创建一些方法和成员,但我不完全知道是什么?
Hey guys
I am using runnable outside the oncreate in my android application where i have used thread to setprogress of ProgressBar. What i dont know is how to stop/destry the thread when stop button is pressed since thread.stop is not a method and how to resume from that , how to even destroy the thread.
I know i have to make some methods and members in runnable but i dont exactly know what??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Thread.stop()
不再使用,因为它被认为是危险的:http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html。您必须让线程因变量更改而自然结束。该链接还提供了一些有关如何实现这一目标的建议。
警告
:请确保您在循环代码、阻塞自身的方法或 Thread.sleep(..)。如果您不了解受保护的块,Thread.sleep 是其中最原始的,但它会起作用。您还可以永远等待,并使用中断机制来取消线程,当您使用等待或睡眠时,该线程会在 try-catch 块中以
InterruptedException
的形式抛出。为此,请使用!Thread.currentThread().isInterrupted()
作为循环保护条件,然后使用 Thread 对象并调用thread.interrupt()
。Thread.stop()
is no longer used since it was considered dangerous: http://download.oracle.com/javase/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html.You must let the thread come naturally to an end as a result of a variable change. The link also gives some advice about how to achieve this.
}
Warning: make sure you either use a guarded block in the looping code, a method that blocks itself, or a
Thread.sleep(..)
. Thread.sleep is the most primitive of these if you don't understand guarded blocks, but it will work. You could also wait forever and use the interrupt mechanism to cancel the thread which is thrown asInterruptedException
in the try-catch block when you use a wait or sleep. For this, use!Thread.currentThread().isInterrupted()
as the loop guard condition, then use your Thread object and callthread.interrupt()
.要控制 Java 线程,您应该向对象添加可由其他对象调用的方法,这些对象设置
run()
方法读取的变量。您没有提供太多关于您正在做什么的信息,但这里有一个可能的模式:您可能希望在控制变量周围使用更强大的同步,并且,正如评论中提到的,等待/通知而不是忙碌等待。
像这样使用:
To control a Java thread, you should add methods to the object that can be called by other objects which set variables read by your
run()
method. You don't give much information on exactly what you're doing, but here's a possible pattern:You will probably want to use more robust synchronisation around the control variables, and, as mentioned in the comments, wait/notify rather than a busy wait.
Use as so:
您有几个选项,它们取决于您如何定义线程的各种状态。
当线程退出 run() 方法时,该线程实际上已停止。
要“暂停”和“恢复”线程的执行,您可以使用 wait() 和 notification()。
为了说明这一点,这里有一个简单的例子:
You have a few options and they depend on how you define the various states of your thread.
A thread is effectively stoped when it exits the run() method.
To "pause" and "resume" a thread's execution you can can use wait() and notify().
To illustrate this, here's a quick example:
让另一个线程定期检查布尔标志(isCancelled 或类似的东西)。最初是假的。
在停止按钮代码中,将此值设置为 true。
当您的线程接下来检查该标志并发现它为真时,该线程应该杀死自己。
Have the other thread check a boolean flag (isCancelled, or something like that) periodically. Initially is is false.
From your stop button code, set this value to true.
When your thread next checks the flag and finds it to be true, the thread should kill itself.