From 7d369725709350a7278bee5d119375c5fba9e2dd Mon Sep 17 00:00:00 2001 From: Jesus Boadas Date: Thu, 20 Jul 2017 10:13:42 -0400 Subject: [PATCH] changed thread for ScheduledExecutorService (#2297) --- .../java/com/baeldung/vaadin/VaadinUI.java | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java b/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java index 8343d38f6a..68e2ca7944 100644 --- a/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java +++ b/libraries/src/main/java/com/baeldung/vaadin/VaadinUI.java @@ -4,6 +4,9 @@ import java.time.Instant; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import javax.servlet.annotation.WebServlet; @@ -216,7 +219,11 @@ public class VaadinUI extends UI { timeLayout.addComponent(currentTime); serverPushPanel.setContent(timeLayout); serverPushPanel.setSizeUndefined(); - new ServerPushThread().start(); + ScheduledExecutorService scheduleExecutor = Executors.newScheduledThreadPool(1); + Runnable task = () -> { + currentTime.setValue("Current Time : " + Instant.now()); + }; + scheduleExecutor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS); FormLayout dataBindingLayout = new FormLayout(); dataBindingLayout.setSpacing(true); @@ -271,24 +278,4 @@ public class VaadinUI extends UI { @VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false) public static class MyUIServlet extends VaadinServlet { } - - class ServerPushThread extends Thread { - @Override - public void run() { - try { - while (true) { - Thread.sleep(1000); - access(new Runnable() { - @Override - public void run() { - currentTime.setValue("Current Time : " + Instant.now()); - } - }); - } - - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } } \ No newline at end of file