changed thread for ScheduledExecutorService (#2297)

This commit is contained in:
Jesus Boadas 2017-07-20 10:13:42 -04:00 committed by maibin
parent b558adec06
commit 7d36972570
1 changed files with 8 additions and 21 deletions

View File

@ -4,6 +4,9 @@ import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
@ -216,7 +219,11 @@ public class VaadinUI extends UI {
timeLayout.addComponent(currentTime); timeLayout.addComponent(currentTime);
serverPushPanel.setContent(timeLayout); serverPushPanel.setContent(timeLayout);
serverPushPanel.setSizeUndefined(); 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(); FormLayout dataBindingLayout = new FormLayout();
dataBindingLayout.setSpacing(true); dataBindingLayout.setSpacing(true);
@ -271,24 +278,4 @@ public class VaadinUI extends UI {
@VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false) @VaadinServletConfiguration(ui = VaadinUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet { 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();
}
}
}
} }