Answer the question
In order to leave comments, you need to log in
What are the alternatives to Thread.sleep / Timer in Java?
Good day.
I recently started learning Java. Now I am writing a synthesizer for Android, I wanted to implement an arpeggiator in it. The arpeggiator works in a separate thread as follows:
1. Scan an array of notes.
2. If the note is not equal to zero, turn it on.
3. We fall asleep for its duration.
4. Turn off the note.
5. We fall asleep for the interval between notes.
6. Move on to the next note.
In general, everything works, however, when using Sleep(), Timer() or nanoTime(), the duration of notes / intervals is not constant and changes within +/- 20 ms, which, of course, is noticeable by ear.
while(hasTouch)
{
for (int note : sequence)
{
if (note != 0)
{
playNote(note);
Thread.sleep(duration);
stopNote(note);
Thread.sleep(duration);
}
}
}
Answer the question
In order to leave comments, you need to log in
Have you tried using scheduler? And do you need to do sleep at all?
developer.android.com/reference/java/util/concurre...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question