Java中javascript setTimeout的等价物是什么?
我需要实现一个功能,在点击按钮60秒后运行。请帮助,我使用了Timer类,但我认为这不是最好的方法。您应该使用Java中javascript setTimeout的等价物是什么?,java,javascript,timer,settimeout,setinterval,Java,Javascript,Timer,Settimeout,Setinterval,我需要实现一个功能,在点击按钮60秒后运行。请帮助,我使用了Timer类,但我认为这不是最好的方法。您应该使用Thread.sleep()方法 try { Thread.sleep(60000); callTheFunctionYouWantTo(); } catch(InterruptedException ex) { } 这将等待60000毫秒(60秒),然后执行代码中的下一个语句 您只需使用Thread.sleep()即可。但是,如果您在具有用户界面的多线程环境中工作
Thread.sleep()
方法
try {
Thread.sleep(60000);
callTheFunctionYouWantTo();
} catch(InterruptedException ex) {
}
这将等待60000毫秒(60秒),然后执行代码中的下一个语句 您只需使用
Thread.sleep()
即可。但是,如果您在具有用户界面的多线程环境中工作,您可能希望在单独的线程中执行此操作,以避免睡眠阻塞用户界面
try{
Thread.sleep(60000);
// Then do something meaningful...
}catch(InterruptedException e){
e.printStackTrace();
}
“我使用了Timer类,但我认为这不是最好的方法。”
其他答案假设您的用户界面没有使用Swing(按钮)
如果您正在使用Swing,请不要使用Thread.sleep()
,因为它会冻结您的Swing应用程序
相反,您应该使用javax.swing.Timer
有关更多信息和示例,请参阅Java教程和。库中有setTimeout()
方法
代码示例:
import com.github.underscore.U;
import com.github.underscore.Function;
public class Main {
public static void main(String[] args) {
final Integer[] counter = new Integer[] {0};
Function<Void> incr = new Function<Void>() { public Void apply() {
counter[0]++; return null; } };
U.setTimeout(incr, 100);
}
}
import com.github.underline.U;
导入com.github.underline.Function;
公共班机{
公共静态void main(字符串[]args){
最终整数[]计数器=新整数[]{0};
函数incr=新函数(){public Void apply()){
计数器[0]++;返回null;}};
U.setTimeout(增量,100);
}
}
该函数将在100ms内使用新线程启动。使用JDK1.8异步实现:
public static void setTimeout(Runnable runnable, int delay){
new Thread(() -> {
try {
Thread.sleep(delay);
runnable.run();
}
catch (Exception e){
System.err.println(e);
}
}).start();
}
要使用lambda表达式调用:
setTimeout(() -> System.out.println("test"), 1000);
或使用方法参考:
setTimeout(anInstance::aMethod, 1000);
要处理当前正在运行的线程,请仅使用同步版本:
public static void setTimeoutSync(Runnable runnable, int delay) {
try {
Thread.sleep(delay);
runnable.run();
}
catch (Exception e){
System.err.println(e);
}
}
在主线程中小心使用此选项–它将在调用后挂起所有内容,直到
timeout
过期并且runnable
执行。不要使用thread.sleep
,否则它将冻结主线程,而不会从JS模拟setTimeout。您需要创建并启动一个新的后台线程来运行代码,而不停止主线程的执行。像这样:
new Thread() {
@Override
public void run() {
try {
this.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// your code here
}
}.start();
从scheduleAtFixedRate开始60秒后运行
使用Java 9 CompletableFuture,每个简单的:
CompletableFuture.delayedExecutor(5, TimeUnit.SECONDS).execute(() -> {
// Your code here executes after 5 seconds!
});
使用:
new Timer().计划(new TimerTask()){
@凌驾
公开募捐{
//这是你的延迟代码
}
},300L);//300是以毫秒为单位的延迟
你可以找到一些信息和例子。这个问题有一些很好的相关答案:使用任何类型的“睡眠”(在主线程或活动线程上)来做这类事情绝对是不合适的。(它也不需要使用线程。)正如这里所建议的,一定需要某种类型的超时。另外:在超时处理程序中,确保确认感兴趣的条件仍然存在!(即使您在条件不再存在时“删除超时”,仍然存在一个无法完全消除的微小计时漏洞。)您能否分享您的解决方案比其他解决方案更好的方式?还请解释一下你的解决方案在做什么,而不仅仅是粘贴一段代码。代码创建者不是我。是神谕。他们的解释比我强。请参考此链接并阅读文档。代码段不起作用。它显示错误:对于CompletableFuture类型,delayedExecutor(int,TimeUnit)方法未定义。我认为我犯了一个错误,它是针对Java 9的,而不是针对Java 8的。使用Java 9,一切都很好。对于任何使用Java 9及以上的人来说,这应该是公认的答案。是的,不要这样做-它会启动太多线程,我认为CompletableFuture技术更好CompletableFuture仍然会创建一个新线程。看,有没有办法告诉CompletableFuture使用线程池而不是每次创建一个新线程?或者它已经这样做了?甚至Java的内置计时器
类也会启动“太多线程”:“一种让线程安排任务以便在后台线程中执行的工具。”我认为这种方式实际上相当干净。它不处理重复执行的情况,但您可以让任务在结束时再次调用settimeout。尽管这是公认的答案,但它已经过时了,特别是在java 9问世之后。请参阅下面关于delayedExecutor
的答案,谢谢,我还需要clearTimeout()选项,因此用法是<代码>定时器=新定时器();时间表(…);timer.cancel()代码>
CompletableFuture.delayedExecutor(5, TimeUnit.SECONDS).execute(() -> {
// Your code here executes after 5 seconds!
});