Question

I have this code:

    exec = Executors.newSingleThreadScheduledExecutor();
    exec.scheduleAtFixedRate(new Runnable() {
      @Override
      public void run() {
          System.err.println("Getting messages....");
          getNewMessagesAndAddToTextArea();
      }
    }, 0, GET_MESSAGE_INTERVAL, TimeUnit.SECONDS);

exec is in my class declared as: private ScheduledExecutorService exec;

I'm getting the "Getting messages...." messages 2 times and then it stops. I can't explain why. The GUI is still working. So what happened here?

Was it helpful?

Solution

I suspect getNewMessagesAndAddToTextArea is throwing an exception.

The first way to validate that idea is to add

System.err.println("Finished getting messages");

at the end of your run method. If (as I expect) you don't see that the second time, you should consider including a try/catch block which logs exceptions. You'll need to think what you want to do with uncaught unchecked exceptions...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top