0

I am writing a simple application for practice that I want to show a message every N minutes and then sleep for a while.

My question here: what are the most efficient ways of designing applications that have this functionality: sleep for a while and then wake up and do something. I am a student and at school we write applications with threads and after writing a basic kernel I understand some basics of scheduling and time slicing.

My goal is to better understand how to write a small program that has a very small footprint (and how to reach that small footprint in this small example), but can run for weeks.

  • I can use Thread.sleep(N) in Java or equivalent. But that has does not guarantee precision of sleep and in fact (from what I read), might not sleep nowhere near N seconds. Articles like this seem to discourage sleep().
  • I thought about creating a thread that I can signal to wake up every N seconds. But then the main() thread will be constantly working and counting time. That's also very wasteful since I'd be wasting cycles on counting time.

I guess ideally I want the process to not run for N minutes and then wake up (by OS) after some number of time slices, but I am not sure there is another way of doing it besides sleep(). I am writing it in Java, but I could go with C or Python if those can do better (I feel that shouldn't matter). I am still researching, but I thought I'd get a hint from the community as well.

The program is a simple reminder that tells me to stretch and use a standing desk. :) Understanding threading a bit better is the actual goal.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Serge Poele
  • 117
  • 11
  • If you want/need a good response at the end of your sleep, Thread.setPriority(Thread.MAX_PRIORITY) your thread so that it stands a good chance of being made running when it becomes ready after the Sleep interval. – Martin James Feb 16 '16 at 21:06
  • ' (from what I read), might not sleep nowhere near N seconds' - in most cases, this is FUD. That will only happen on machines that are grossly overloaded with far more ready threads than cores. I'm afraid that the amount of misinformation re. threads on the net is pretty awesome:( – Martin James Feb 16 '16 at 21:09

4 Answers4

1

The comments around Thread.sleep are really about more precision than your usecase entails, IMO. Thread.sleep should be precise enough for your needs. You could look at a ScheduledThreadPoolExecutor which gets you out of the some of the nitty gritty thread management, which is an easy thing to get wrong as a program grows in complexity.

Taylor
  • 3,942
  • 2
  • 20
  • 33
0

Check out the java Timer and TimerTask classes in the docs

Emzor
  • 1,380
  • 17
  • 28
heniv181
  • 108
  • 1
  • 6
0
 Well, From application perspective as you mentioned you can use the Thread.sleep(N). 
 However the accuracy totally relied on the underlying machine. 
 (Read the docs on http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#currentTimeMillis())
  You  can also use this SOF to get more options : http://stackoverflow.com/questions/824110/accurate-sleep-for-java-on-windows
 on the under relying machine. 
Yohannes Gebremariam
  • 2,225
  • 3
  • 17
  • 23
0

Sleeping a thread is most of the cases a bad way to desing an application, in java there are Timers that you can define to execute a taks periodically. look like:

Timer timer = new Timer();
    timer.schedule(new TimerTask() {
       @Override
       public void run() {
             doStuff();
        }
    }, <delay>, <period>);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97