java中延时后调用particluar函数
在java中,如何在延迟后调用特定函数java中延时后调用particluar函数,java,multithreading,timer,Java,Multithreading,Timer,在java中,如何在延迟后调用特定函数 { // Do something callmeafterevery10sec () // call this function in every 10 sec while continue the whole thing // Do something } 选项1: Timer t = new Timer(); t.schedule(new TimerTask() { @Override public void ru
{
// Do something
callmeafterevery10sec () // call this function in every 10 sec while continue the whole thing
// Do something
}
选项1:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hello World");
}
}, 0, 10000);
这将每隔10秒打印一次Hello World
。您可以在一个方法中使用它,也可以使用它调用另一个方法
选项2:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hello World");
}
}, 0, 10000);
从Brian Goetz等人的Java并发实践来看,ScheduledExecutorService
更好:
Runnable helloRunnable = new Runnable() {
public void run() {
System.out.println("Hello world");
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 10, TimeUnit.SECONDS);
有关ScheduledExecutorService.scheduleAtFixedRate的详细信息。选项1:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hello World");
}
}, 0, 10000);
这将每隔10秒打印一次Hello World
。您可以在一个方法中使用它,也可以使用它调用另一个方法
选项2:
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hello World");
}
}, 0, 10000);
从Brian Goetz等人的Java并发实践来看,ScheduledExecutorService
更好:
Runnable helloRunnable = new Runnable() {
public void run() {
System.out.println("Hello world");
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(helloRunnable, 0, 10, TimeUnit.SECONDS);
更多关于ScheduledExecutorService.scheduleAtFixedRate的信息。只要调用Thread.sleep(),它就会工作:
public class Test {
public static void main(String args[]) throws InterruptedException {
useTimer();
}
private static void useTimer() throws InterruptedException {
System.out.println("**********");
Thread.sleep(10000);
System.out.println("----------");
}
}
只需调用Thread.sleep(),它就可以工作:
public class Test {
public static void main(String args[]) throws InterruptedException {
useTimer();
}
private static void useTimer() throws InterruptedException {
System.out.println("**********");
Thread.sleep(10000);
System.out.println("----------");
}
}
听起来像是ScheduledExecutorService的作业。您应该看看这个类的JavaDoc。这里有很多很好的例子。听起来像是ScheduledExecutorService的作业。您应该看看这个类的JavaDoc。有很多很好的例子。@parikshitparmar,如果答案解决了你的问题,别忘了接受它。第一个解决方案不起作用,它在run方法中被卡住了。无法同时执行其他方法。是否可以编辑并输入刚才尝试的内容?我会试着看一看。因为它适用于me.import java.util.Timer;导入java.util.TimerTask;类TestThread{public static void main(final String[]参数)抛出中断异常{Timer t=new Timer();System.out.println(“第一次打印”);t.schedule(new TimerTask(){@Override public void run(){printSomething();}},0,100);System.out.Print(“最后一次打印”)}public static void printSomething(){System.out.print(“Inside”);}我试图编辑我的帖子,但现在似乎无法编辑,我还没有添加代码,它显示按ctrl+k和4空格indentetion@parikshitparmar,如果答案解决了你的问题,不要忘记接受它。第一个解决方案不起作用,它在run方法中被卡住了。无法同时执行其他方法。是否可以编辑并输入刚才尝试的内容?我会试着看一看。因为它适用于me.import java.util.Timer;导入java.util.TimerTask;类TestThread{public static void main(final String[]参数)抛出中断异常{Timer t=new Timer();System.out.println(“第一次打印”);t.schedule(new TimerTask(){@Override public void run(){printSomething();}},0,100);System.out.Print(“最后一次打印”)}public static void printSomething(){System.out.print(“Inside”);}我试图编辑我的帖子,但现在似乎无法编辑,我还没有添加代码,它显示按ctrl+k和4空格indentetion@parikshitparmar如果这有效,请接受答案,否则,如果仍然面临问题,请告诉我OP示例中的注释是:“每10秒调用一次此函数,然后继续整个过程。”这听起来很像是他们想在后台线程中执行定期任务。@parikshitparmar如果这样行得通,请接受答案,否则,请让我知道如果仍然面临问题,OP示例中的注释说,“每10秒调用一次此函数,然后继续整个过程。”这听起来很像他们希望在后台线程中执行定期任务。