mirror of
https://github.com/correl/mage.git
synced 2024-12-26 11:09:27 +00:00
Merge pull request #2029 from draxdyn/audio_threadpool
Use a dedicated thread pool for audio
This commit is contained in:
commit
0733729382
1 changed files with 18 additions and 3 deletions
|
@ -5,6 +5,10 @@ import java.util.LinkedList;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
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.AudioFormat;
|
||||||
import javax.sound.sampled.AudioSystem;
|
import javax.sound.sampled.AudioSystem;
|
||||||
|
@ -19,8 +23,6 @@ import javax.sound.sampled.SourceDataLine;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import mage.utils.ThreadUtils;
|
|
||||||
|
|
||||||
public class LinePool {
|
public class LinePool {
|
||||||
|
|
||||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
@ -41,12 +43,25 @@ public class LinePool {
|
||||||
|
|
||||||
private Mixer mixer;
|
private Mixer mixer;
|
||||||
private int alwaysActive;
|
private int alwaysActive;
|
||||||
|
private ThreadPoolExecutor threadPool;
|
||||||
|
private int threadCount;
|
||||||
|
|
||||||
public LinePool() {
|
public LinePool() {
|
||||||
this(new AudioFormat(22050, 16, 1, true, false), 4, 1);
|
this(new AudioFormat(22050, 16, 1, true, false), 4, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinePool(AudioFormat audioFormat, int size, int alwaysActive) {
|
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;
|
format = audioFormat;
|
||||||
this.alwaysActive = alwaysActive;
|
this.alwaysActive = alwaysActive;
|
||||||
mixer = AudioSystem.getMixer(null);
|
mixer = AudioSystem.getMixer(null);
|
||||||
|
@ -95,7 +110,7 @@ public class LinePool {
|
||||||
busyLines.add(line);
|
busyLines.add(line);
|
||||||
logLineStats();
|
logLineStats();
|
||||||
}
|
}
|
||||||
ThreadUtils.threadPool.submit(new Runnable() {
|
threadPool.submit(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
Loading…
Reference in a new issue