Created
          July 27, 2019 19:50 
        
      - 
      
- 
        Save rzwitserloot/9def245029193eb7b580f650ac69145a to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import java.util.concurrent.TimeUnit | |
| class DivLoader { | |
| private final Object locker = new Object[0]; | |
| private final AtomicBoolean running = new AtomicBoolean(); | |
| private static final long WAIT_TIME = TimeUnit.SECONDS.toMillis(60); | |
| private Thread thread; | |
| private final AtomicReference<String> content = new AtomicReference<>(); | |
| private Runnable refresher() { | |
| return () -> { | |
| running.set(true); | |
| synchronized (locker) { | |
| while (!Thread.interrupted()) { | |
| try { | |
| reloadDivContent(); | |
| Thread.sleep(WAIT_TIME); | |
| } catch (InterruptedException e) { | |
| break; | |
| } | |
| } | |
| running.set(false); | |
| thread = null; | |
| } | |
| } | |
| } | |
| public boolean isRefreshing() { | |
| return running.get(); | |
| } | |
| public void startRefresher() { | |
| if (isRefreshing()) return; | |
| synchronized (locker) { | |
| thread = new Thread(refresher(), "Web Div reloader"); | |
| thread.start(); | |
| } | |
| } | |
| public void stopRefresher() { | |
| synchronized (locker) { | |
| if (thread != null) thread.interrupt(); | |
| } | |
| } | |
| private void reloadDivContent() throws IOException { | |
| // Use tools to fetch the content | |
| content.set("Content of div here"); | |
| } | |
| public String getContent() { | |
| return content.get(); | |
| } | |
| public static void main(String[] args) { | |
| System.out.println("Hello World!"); | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment