文章详情页
如何设置一个无限循环并打破它。(Java线程)
如何解决如何设置一个无限循环并打破它。(Java线程)?
假设您在JDK 1.5或更高版本(澄清和改进了内存模型)上运行,则可以使用
public class MyRunnable extends Runnable{ private volatile boolean cancelled; public void run() { while (!cancelled) { doStuff(); } } public void cancel() { cancelled = true; } public boolean isCancelled() { return cancelled; }}j
或者,使用java.util.concurrent.Future和FutureTask,它们支持立即取消。
解决方法我已经设置了一个线程,我想使用循环来运行它。因此,该线程应在循环中运行并在一定时间内中断,然后再次运行循环。
请我不知道该怎么做。有人可以指导我。
标签:
java
相关文章:
1. 为什么HTML5规范中会包含一些和html无关的东西?如本地存储等。2. chrome浏览器怎么使用3. css - 关于图标字体4. javascript - Tomcat突然就报错了,解析不了java文件,试过几个百度的解决方法,没解决。5. python - 《flask web开发》中为什么发不出确认邮件?6. mysql中的全文索引支持词根检索吗?7. javascript - 百度echarts图表如何修改8. javascript - 百度地图一开始设置了中心点,怎么通过按钮在调转到对应的中心点????9. html5 - echarts折线图 markline 数值怎么设置 不想用默认的average10. objective-c - 类似微信单聊的功能,想把聊天记录直接存入Sqlite,怎么建表.
排行榜