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.