【问题标题】:Javafx: Multi Threading a reminder notificationJavafx:多线程提醒通知
【发布时间】: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


【解决方案1】:

所以我遵循了 Zephyr 在评论中给出的逻辑,它对我有用。

我假设约会在某个时候被添加到数据库中?如果是这样,只需创建一个后台线程以定期轮询数据库。当约会临近时,显示提醒。

代码:

private void displayAppointmentNotification() {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

    Timer timer = new Timer();

    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            try {
                ps = HospitalDB.getCon().prepareStatement("Select * From Notification");
                rs = ps.executeQuery();

                while (rs.next()) {

                    if (rs.getString("Reminder").contentEquals(dateFormat.format(new Date()))) {
                        Notifications noti = Notifications.create();
                        noti.text("Doctor " + rs.getString("Doctor") + " has an Appointment with Patient " + rs.getString("Patient"));
                        noti.title("Appointment");
                        noti.hideAfter(Duration.seconds(30));
                        noti.position(Pos.BOTTOM_RIGHT);

                        Platform.runLater(() -> {
                            noti.show();
                        });

                        //Change The Appointment Status to Done
                        ps = HospitalDB.getCon().prepareStatement("Update Appointment Set Status=? Where Patient=? and Doctor=?");
                        ps.setString(1, "Done");
                        ps.setString(2, rs.getString("Patient"));
                        ps.setString(3, rs.getString("Doctor"));
                        ps.executeUpdate();

                        populateTable();
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    };
    timer.schedule(task, 0, 1 * 1000);
}

后台线程将始终查看数据库中的日期,如果它与系统日期相同,则显示通知

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多