Make sure the stream is blocked for input.
A thread takes up 100% of the processor time if it is not blocked. Blocking will cause the thread to start to sleep until some external stimulus appears to wake it up. Whenever you have a dedicated thread for processing input, you need to make sure that it blocks this input so that it sleeps in such a way as to avoid a thread that takes up 100% of the processor time in its cycle.
In this particular case, if Line is javax.sound.sampled.TargetDataLine , then the read() method is where you should do your lock. According to the API, "This method is blocked until the requested amount of data has been read," but "if the data line is closed, stopped, discharged, or reset before the requested amount is read, this method is no longer blocked, but returns the amount bytes read. "
So there are a couple of possibilities. I donβt see your definition of Line , so itβs possible that you are clearing or stripping the line in some code that you did not include. If you have done this, you should reorganize the solution so as not to use these methods.
Another possibility is that since you always read only two bytes at a time, you do not read enough to block it. Basically, there are always two bytes, because it takes too much time to process the two bytes you read, which is always two more bytes. Try increasing the buffer size to read as much as 4kb at a time. See If this makes the loop work less often and block more often.
source share