我正在寻找与 ruby 中相同的 java 功能:
SystemTimer.timeout_after(30.seconds) 做 做一点事 结束
我可以通过 fork 一个线程然后在一段时间后杀死它来实现这一点,但是有没有更简单的方法?
最佳答案
你不能只使用 Java Timer ?
A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.
关于java - 如何在主线程上围绕 java 中的某些代码设置超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/819103/