我想爲報警應用程序創建多個Runnable
對象,它將在用戶指定的時間內執行任務。Java - 實例化Runnable Inside Loop
我嘗試做內環路這樣的:
ScheduledExecutorService wait;
List<Runnable> listens = new ArrayList<>();
int i;
private void playAlarmOnInit(){
wait = Executors.newScheduledThreadPool(3);
// loop through the tasks to get times
int counts = getDays().size();
for(i = 0; i < counts; i++){
if(!getDays().get(i).isEmpty()) {
Runnable listen = new Runnable() {
@Override
public void run() {
if(!getDays().get(i).equals("Everyday")) {
System.out.println(getDays().get(i) + " " + getTimes().get(i));
} else {
DateFormat format = new SimpleDateFormat("d-M-yyyy");
Date date = new Date();
String time = format.format(date);
System.out.println(time + " " + getTimes().get(i));
}
// System.out.println(" " + getTimes().get(i));
}
};
wait.scheduleAtFixedRate(listen, 0, 1, TimeUnit.SECONDS);
}
}
}
它什麼都不做。 爲什麼上面的代碼不起作用?
你是什麼意思「它什麼都不做」?例外?沒有一個'Runnable'正在執行? – JohnnyAW
沒什麼。甚至沒有執行任何懷疑或Runnable。 – KeepMove
你確定'getDays()'實際上是返回一些東西,像@John Tribe在他的答案中提到的? – JohnnyAW