0

I'm working on real-time system. Which requires balancing load per second. So when computing is done I've to put a thread to sleep state till next second right now I'm doing this by:

Thread.sleep(((1000 - (Calendar.getInstance().getTimeInMillis() % 1000))));

but it looks really ugly for me.
Do you have any tip to improve this?

1
  • 1
    Calendar.getInstance() will take some time to set up. Instead you could use System.currentTimeMillis()
    – Pshemo
    Sep 29, 2013 at 22:05

1 Answer 1

2

Instead of having the threads all sleep some amount of time, have the threads wait on a signal from a manager thread.

This way, only one thread needs to keep track of ticks, and the others just do work, then wait for a signal to do more work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.