0

I have program with javafx and want to refresh list's with new data when other user's insert data. So program to have real-time data. Problems is with thread i created. Every time i open a view it's create a new thread and have multiple notifications instead of one. I tried to extend class with Thread and with implementing Runnable but i had no success.

On method initialize i have code where i create a runnable and set it to thread.

int i = 0;

Runnable br = new Runnable() {
  @Override
  public void run() {
    while (i < i + 1) {
      try {
        Thread.sleep(1000);
        if (count_pacient_number != pjc.getPacientForDoctor(doctor_login).size()) {
          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              emf.getCache().evictAll();
              pacientList.clear();
              patientList();
              count_pacient_number = pjc.getPacientForDoctor(doctor_login).size();
            }
          });
        }
      } catch (InterruptedException ex) {
        ex.printStackTrace();
      }
    }
  }
};

// private Thread thread = null; is created on start of class
thread = new Thread(br);
thread.setDaemon(true);
thread.start();
F0XS
  • 1,271
  • 3
  • 15
  • 19
  • Where did you add this code? I suspect that part is being executed multiple times. – Dakshinamurthy Karra Jan 23 '18 at 12:28
  • @KDM at `@Override public void initialize(URL url, ResourceBundle rb)` – Bardh Krasniqi Jan 23 '18 at 12:43
  • All the problem is at `thread = new Thread(br);` because it's has to be inside `initialize` method. If isn't inside when program runs throws exception for thread is null. – Bardh Krasniqi Jan 23 '18 at 12:48
  • Please edit your question to include a [mcve]. – Itai Jan 23 '18 at 12:53
  • Also - assuming the code in `runLater` may take more than 1 second to execute you may be submitting the same "change" more than once. You should probably set the new value for `count_pacient_number` *before* the call to `runLater`. – Itai Jan 23 '18 at 12:55
  • count_pacient_number is seted couple lines above but i didn't show it in code @sillyfly – Bardh Krasniqi Jan 23 '18 at 13:02
  • You set it *inside* the call to `Platform::runLater`, but you test for its value outside. This means that by the time you do the next check it still has the old value, and so you run another update when you don't need to. This is even before considering volatility/locality (see [this question](https://stackoverflow.com/questions/106591/do-you-ever-use-the-volatile-keyword-in-java) ). There is no real reason to only set it inside the `runLater` call, so you are better off setting it to the new value as soon as you first get it (first line in the `if`) – Itai Jan 23 '18 at 13:11
  • @sillyfly thank you for your suggestion. – Bardh Krasniqi Jan 23 '18 at 13:17
  • 1
    The controller instance is specific to the UI loaded from the FXML; each time you load the FXML, a new controller is created and `initialize()` is called on that controller; consequently you create a new thread each time you load the FXML. If you want a single thread that is common to all instances of the UI you load (i.e. one thread, but you load the FXML many times), then the controller is not the correct place to keep this thread. (OTOH if the thread is specific to the UI you create, you probably just need a mechanism for it to exit when you close out that UI.) – James_D Jan 23 '18 at 13:59
  • @James_D any suggestion how to load one thread and fxml many times? I tried to avoid creating new thread's by extending class with thread and implements runnables but i had same problem. – Bardh Krasniqi Jan 24 '18 at 14:37
  • No, not without you clarifying the question considerably. It looks like the thread you defined here is specifically updating elements of this particular UI (i.e. it is specific to this controller instance). So I don't really understand how you would create just one thread that was going to do this if you are going to load the FXML multiple times. (What would the code in that thread look like - what should it be updating?) Note that if the only thing you are using the thread for is to repeatedly pause, you should [use a timeline](https://stackoverflow.com/questions/9966136) (not threads at all) – James_D Jan 24 '18 at 14:46

0 Answers0