【发布时间】:2018-10-05 22:24:13
【问题描述】:
我正在创建一个通知用户约会时间的系统。我的问题:当用户添加一个新约会时创建一个线程是否很好,该约会将在约会日期到来时收听并在右下角显示通知
我的代码
DateFormat inFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
DateFormat outFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Timer timer = new Timer();
private void appointmentNotification() throws ParseException {
//Convert 12hour time to 24hour
String dateValues = date + " " + time;
Date dateParse = inFormat.parse(dateValues);
timer.schedule(new TimerTask() {
@Override
public void run() {
Notifications noti = Notifications.create();
noti.text("Doctor "+doc+" has an Appointment with Patient "+patient);
noti.title("Appointment");
noti.hideAfter(Duration.seconds(10));
noti.position(Pos.BOTTOM_RIGHT);
Platform.runLater(new Runnable() {
@Override
public void run() {
noti.show();
}
});
}
}, outFormat.parse(outFormat.format(dateParse)));
}
我想如果用户添加了 50 个约会,将会有 50 个线程在运行
【问题讨论】:
-
我假设约会在某个时候被添加到数据库中?如果是这样,只需创建一个后台线程以定期轮询数据库。当约会临近时,显示提醒。
-
请注意
java.util.Timer只使用一个后台线程。 -
让一个线程检查所有相关约会不是更好吗?
-
@c0der 这就是我试图实现的方式,但我无法弄清楚
-
@Slaw 是的,我知道。但是如何使用单个线程来处理多个提醒通知
标签: java multithreading javafx