/* LineDrainer class. This class handles draining and closing an audioline in a separate thread. */ import java.lang.Thread; import javax.sound.sampled.SourceDataLine; class BackgroundCloser extends Thread { SourceDataLine line; public BackgroundCloser(SourceDataLine newLine) {this.line = newLine;} public void run() { /* Wait until all data are played. This is only necessary because of the bug noted below. (If we do not wait, we would interrupt the playback by prematurely closing the line and exiting the VM.) Thanks to Margie Fitch for bringing me on the right path to this solution. */ line.drain(); /* All data are played. We can close the shop. */ line.close(); } }