Skip to content

Instantly share code, notes, and snippets.

@rzwitserloot
Created July 27, 2019 19:50
Show Gist options
  • Save rzwitserloot/9def245029193eb7b580f650ac69145a to your computer and use it in GitHub Desktop.
Save rzwitserloot/9def245029193eb7b580f650ac69145a to your computer and use it in GitHub Desktop.
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