中断或停止睡眠线程 [英] Interrupting or stopping a sleeping thread
本文介绍了中断或停止睡眠线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在java中停止或中断睡眠线程?我有一个同步数据并在run()方法中休眠10分钟的线程,如果我想通过在线程休眠时停止线程来停止同步。如何实现?
How to stop or interrupt a sleeping thread in java.? I have a thread that syncs data and sleeps for 10 minutes in run() method, if i want to stop the sync by stopping the thread when it is sleeping.? How can this be achieved?
推荐答案
在线程上调用interrupt()方法。这将导致取消睡眠并抛出InterruptedException。
Call the interrupt() method on your thread. This will cause the sleep to be cancelled and an InterruptedException will be thrown.
这篇关于中断或停止睡眠线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文