Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a java thread that runs on a timer but can be awaken at any time
    text
    copied!<p>I would like to create a class that runs something (a runnable) at regular intervals but that can be awaken when needed. If I could encapsulate the whole thing I would like to expose the following methods:</p> <pre><code>public class SomeService implements Runnable { public run() { // the code to run at every interval } public static void start() { } public static void wakeup() { } public static void shutdown() { } } </code></pre> <p>Somehow I've gotten this far. But I'm not sure if this is the correct approach.</p> <pre><code>public class SomeService implements Runnable { private static SomeService service; private static Thread thread; static { start(); } private boolean running = true; private SomeService() { } public void run() { while (running) { try { // do what needs to be done // perhaps peeking at a blocking queue // or checking for records in a database // trying to be independent of the communication System.out.println("what needs to be done"); // wait for 15 seconds or until notify synchronized (thread) { try { thread.wait(15000); } catch (InterruptedException e) { System.out.println("interrupted"); } } } catch (Exception e) { e.printStackTrace(); } } } private static void start() { System.out.println("start"); service = new SomeService(); thread = new Thread(service); thread.setDaemon(true); thread.start(); } public static void wakeup() { synchronized (thread) { thread.notify(); } } public static void shutdown() { synchronized (thread) { service.running = false; thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("shutdown"); } public static void main(String[] args) throws IOException { SomeService.wakeup(); System.in.read(); SomeService.wakeup(); System.in.read(); SomeService.shutdown(); } } </code></pre> <p>I'm concerned that the variables should be declared volatile. And also concerned that I should check in the "what needs to be done part" for <code>thread.isInterrupted()</code>. Does this seem like the right approach? Should I translate this to executors? How can I force a run on a scheduled executor?</p> <p>EDIT</p> <p>After experimenting with the executor, it seems that this approach seems reasonable. What do you think?</p> <pre><code>public class SomeExecutorService implements Runnable { private static final SomeExecutorService runner = new SomeExecutorService(); private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); // properties ScheduledFuture&lt;?&gt; scheduled = null; // constructors private SomeExecutorService() { } // methods public void schedule(int seconds) { scheduled = executor.schedule(runner, seconds, TimeUnit.SECONDS); } public void force() { if (scheduled.cancel(false)) { schedule(0); } } public void run() { try { _logger.trace("doing what is needed"); } catch (Exception e) { _logger.error("unexpected exception", e); } finally { schedule(DELAY_SECONDS); } } // static methods public static void initialize() { runner.schedule(0); } public static void wakeup() { runner.force(); } public static void destroy() { executor.shutdownNow(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload