Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8085763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:13:56+00:00 2026-06-05T18:13:56+00:00

I would like to create a class that runs something (a runnable) at regular

  • 0

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:

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() { }

}

Somehow I’ve gotten this far. But I’m not sure if this is the correct approach.

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();

  }

}

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 thread.isInterrupted(). Does this seem like the right approach? Should I translate this to executors? How can I force a run on a scheduled executor?

EDIT

After experimenting with the executor, it seems that this approach seems reasonable. What do you think?

public class SomeExecutorService implements Runnable {

  private static final SomeExecutorService runner 
    = new SomeExecutorService();

  private static final ScheduledExecutorService executor 
    = Executors.newSingleThreadScheduledExecutor();

  // properties

  ScheduledFuture<?> 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();
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:13:57+00:00Added an answer on June 5, 2026 at 6:13 pm

    For starters – you probably don’t want to implement Runnable yourself; you should take in a Runnable. You should only implement Runnable if you expect your class to be passed to others to execute.

    Why not just wrap a ScheduledExecutorService? Here’s a quick (very poor, but ought to be functional) implementation.

    public class PokeableService {
    
      private ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
      private final Runnable codeToRun;
    
      public PokeableService (Runnable toRun, long delay, long interval, TimeUnit units) {
        codeToRun = toRun;
        service.scheduleAtFixedRate(toRun, delay, interval, units);
      }
    
      public void poke () {
        service.execute(codeToRun);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a class that behaves as a python variable but
I would like to create a LoadingDialog class that could be used by ANY
I would like to create a class that will take a list of strings
motivation: I would like to create a utility class so that instead of having
I would like to create a class whose methods can be called from multiple
I would like to create a class that effectively does this (mixing a little
I would like to create a singleton class that is instantiated once in each
I would like to create a class that creates and manages log files. I
I would like to create a class that describes a file resource and then
I would like to modify class completion so that every method that is created

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.