From d8b791a506dfd472dbf66027a4c0f1aa9fd71383 Mon Sep 17 00:00:00 2001 From: draxdyn Date: Thu, 23 Jun 2016 16:16:31 +0200 Subject: [PATCH] Use a dedicated thread pool for audio Audio threads are busy while the sound plays, which could be several seconds, potentially stalling other tasks competing for the thread pool. So use a dedicated thread pool, which also allows to have it sized based on the number of the audio lines. --- .../java/mage/client/util/audio/LinePool.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/util/audio/LinePool.java b/Mage.Client/src/main/java/mage/client/util/audio/LinePool.java index 8145f756bbf..6f52fdae2d5 100644 --- a/Mage.Client/src/main/java/mage/client/util/audio/LinePool.java +++ b/Mage.Client/src/main/java/mage/client/util/audio/LinePool.java @@ -5,6 +5,10 @@ import java.util.LinkedList; import java.util.Set; import java.util.Timer; import java.util.TimerTask; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; @@ -19,8 +23,6 @@ import javax.sound.sampled.SourceDataLine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import mage.utils.ThreadUtils; - public class LinePool { private final Logger log = LoggerFactory.getLogger(getClass()); @@ -41,12 +43,25 @@ public class LinePool { private Mixer mixer; private int alwaysActive; + private ThreadPoolExecutor threadPool; + private int threadCount; public LinePool() { this(new AudioFormat(22050, 16, 1, true, false), 4, 1); } public LinePool(AudioFormat audioFormat, int size, int alwaysActive) { + threadPool = new ThreadPoolExecutor(alwaysActive, size, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue(), new ThreadFactory() { + @Override + public Thread newThread (Runnable runnable) { + threadCount++; + Thread thread = new Thread(runnable, "Audio" + threadCount); + thread.setDaemon(true); + return thread; + } + }); + threadPool.prestartAllCoreThreads(); + format = audioFormat; this.alwaysActive = alwaysActive; mixer = AudioSystem.getMixer(null); @@ -95,7 +110,7 @@ public class LinePool { busyLines.add(line); logLineStats(); } - ThreadUtils.threadPool.submit(new Runnable() { + threadPool.submit(new Runnable() { @Override public void run() {