44import java .util .ArrayList ;
55import java .util .Date ;
66import java .util .List ;
7+ import java .util .concurrent .Executors ;
8+ import java .util .concurrent .ScheduledExecutorService ;
9+ import java .util .concurrent .TimeUnit ;
710
811import javax .servlet .annotation .WebServlet ;
912
@@ -216,7 +219,11 @@ protected void init(VaadinRequest vaadinRequest) {
216219 timeLayout .addComponent (currentTime );
217220 serverPushPanel .setContent (timeLayout );
218221 serverPushPanel .setSizeUndefined ();
219- new ServerPushThread ().start ();
222+ ScheduledExecutorService scheduleExecutor = Executors .newScheduledThreadPool (1 );
223+ Runnable task = () -> {
224+ currentTime .setValue ("Current Time : " + Instant .now ());
225+ };
226+ scheduleExecutor .scheduleWithFixedDelay (task , 0 , 1 , TimeUnit .SECONDS );
220227
221228 FormLayout dataBindingLayout = new FormLayout ();
222229 dataBindingLayout .setSpacing (true );
@@ -271,24 +278,4 @@ protected void init(VaadinRequest vaadinRequest) {
271278 @ VaadinServletConfiguration (ui = VaadinUI .class , productionMode = false )
272279 public static class MyUIServlet extends VaadinServlet {
273280 }
274-
275- class ServerPushThread extends Thread {
276- @ Override
277- public void run () {
278- try {
279- while (true ) {
280- Thread .sleep (1000 );
281- access (new Runnable () {
282- @ Override
283- public void run () {
284- currentTime .setValue ("Current Time : " + Instant .now ());
285- }
286- });
287- }
288-
289- } catch (InterruptedException e ) {
290- e .printStackTrace ();
291- }
292- }
293- }
294281}
0 commit comments