0

What is the best way to publish safely data periodically ?

First approach:

while(true){
    Thread.sleep(1000);
    //pub
}

second:

Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                //pub
                }
             }

third:

ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
        ses.scheduleAtFixedRate(new Runnable() {
            public void run() { //pub }
        }, 0, 3, TimeUnit.SECONDS);
Sadik Hasan
  • 173
  • 3
  • 18
  • Without a lot of context of exactly what you are doing where, you are asking for an opinion and Stack Overflow really isn't the place for this sort of question. – hardillb Jul 16 '16 at 16:47
  • the context i think is directly available periodic publications in java / paho lib which approach is preferable and best suited for ciriticall apps – Sadik Hasan Jul 16 '16 at 17:26
  • All 3 methods do the same thing, it totally depends on the architecture of the rest of your application as to which fits best – hardillb Jul 17 '16 at 09:06

0 Answers0