我有6个函数需要在特定的时间间隔内运行:
public void runFirst() {
System.out.println("First method");
}
public void runSecond() {
System.out.println("Second method");
}
public void runThird() {
System.out.println("Third method");
}
public void runFourth() {
System.out.println("Fourth method");
}
public void runFifth() {
System.out.println("Fifth method");
}
public void runSixth() {
System.out.println("Sixth method");
}
我需要先跑#34;首先"一旦我点击按钮,第二个必须在65秒之后,第二个必须在第二个之后,第三个15秒之后第三个,依此类推,当时我使用Thread.sleep
但我将需要让它没有睡觉。
最好的方法是什么,有人可以根据我的方法向我展示一些例子。
答案 0 :(得分:0)
你可以使用Quartz。单击该按钮可以获得系统当前时间并添加新的计划作业以在特定日期和时间运行。所以Quartz会处理它以便在当时运行。
另一种方法是创建一个新的Thread并在其上定义睡眠时间段并让它运行。
答案 1 :(得分:0)
为什么你不能使用
Thread.sleep
并在不同的线程中运行此代码(然后,整个应用程序不会停止睡眠)
答案 2 :(得分:0)
答案 3 :(得分:0)
要在一段时间后运行任务,您应该使用Timer,这是“线程的工具,可以安排任务以便将来在后台线程中执行。”。< / p>
它使您无需创建自己的线程:更不容易出错且更容易。它可以管理多个计划任务。但要注意,这些任务应该是短暂的(其他它们会阻止用于计时器调度的线程)..
答案 4 :(得分:0)
使用ExecutorService
。基本的想法是这样的:
// "Global" resource
final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(5); // Adjust threadpool size
...
// inside onclick() processing
ScheduledFuture<Void> future1 = executorService.schedule(new Callable<Void>() {
@Override Void call() {
try {
runFirst();
} finally {
scheduleFuture2();
}
}
},
65,
TimeUnit.SECONDS);
...
void scheduleFuture2() {
ScheduledFuture<Void> future2 = executorService.schedule(new Callable<Void>() {
@Override Void call() {
try {
runSecond();
} finally {
scheduleFuture3();
}
}
},
20,
TimeUnit.SECONDS);
}
...
void scheduleFuture3() {
ScheduledFuture<Void> future3 = executorService.schedule(new Callable<Void>() {
@Override Void call() {
try {
runThird();
} finally {
scheduleFuture4();
}
}
},
15,
TimeUnit.SECONDS);
}
...
// And so on
一些注意事项:
runFirst()
,runSecond()
,...执行Swing / AWT UI操作,您应该在这些方法中合理地使用SwingUtilities.invokeLater executorService.shutdown()
。如果你忘了它,你会“泄漏”线程。Void
的引用(大写“V”)是有意的:我们使用的是Void
类,而不是void
类型答案 5 :(得分:0)
{
ScheduledExecutorService worker = Executors.newScheduledThreadPool(1);
worker.execute(() -> runFirst());
worker.schedule(() -> runSecond(), 65, TimeUnit.SECONDS);
worker.schedule(() -> runThird(), 85, TimeUnit.SECONDS);
worker.schedule(() -> runFourth(), 100, TimeUnit.SECONDS);
worker.schedule(() -> runFifth(), 110, TimeUnit.SECONDS);
worker.schedule(() -> runSixth(), 115, TimeUnit.SECONDS);
worker.shutdown();
}
如果由于某种原因无法使用Java 8,请使用anonymous implementations而不是Runnable的Lambda expressions:
// worker.schedule(() -> runSixth(), 115, TimeUnit.SECONDS);
worker.schedule(new Runnable(){
@Override
public void run() {
runSixth();
}}, 115, TimeUnit.SECONDS);
答案 6 :(得分:-2)
为您的方法创建一个Thread类和另一个Class
现在:
public class ThreadTest implements Runnable {
private int functionNumber;
private int time2start;
private YourClass obj;
public ThreadTest(int functionNumber, int time2start, YourClass obj) {
this.functionNumber = functionNumber;
this.time2start = time2start;
this.obj = obj;
}
public void run() {
try {
Thread.currentThread().sleep(time2start);
} catch (Exception ex) {
}//Time Delay before executing methods
switch (functionNumber) {
case 1:
obj.runFirst();
break;
case 2:
obj.runSecond();
break;
case 3:
obj.runThird();
break;
case 4:
obj.runFourth();
break;
//keep adding
}
}
}
然后为您的方法分类:
public class YourClass {
public void runFirst() {
System.out.println("First method");
}
public void runSecond() {
System.out.println("Second method");
}
public void runThird() {
System.out.println("Third method");
}
public void runFourth() {
System.out.println("Fourth method");
}
public void runFifth() {
System.out.println("Fifth method");
}
public void runSixth() {
System.out.println("Sixth method");
}
}
现在这是按钮的onClick事件的方法:
//on button click
ThreadTest th1 = new ThreadTest(1, 0, obj);//here obj is YourClass
ThreadTest th2 = new ThreadTest(2, 65000, obj);//65 SECONDS
//... keep adding
Thread thread1 = new Thread(th1);
Thread thread2 = new Thread(th2);
//...keep adding
thread1.start();
thread2.start();
//...keep adding