refactor: removed client side threads from a server, improved test log files rotation;

This commit is contained in:
Oleg Agafonov 2023-11-27 18:13:05 +04:00
parent 0470ae9799
commit c5632f6868
6 changed files with 75 additions and 62 deletions

View file

@ -7,6 +7,10 @@ import java.util.Queue;
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;
@ -25,6 +29,31 @@ public class LinePool {
private final org.apache.log4j.Logger logger = Logger.getLogger(LinePool.class);
private static final int LINE_CLEANUP_INTERVAL = 30000;
private static final ThreadPoolExecutor threadPoolSounds;
private static int threadCount = 0;
static {
threadPoolSounds = new ThreadPoolExecutor(4, 4, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
threadCount++;
Thread thread = new Thread(runnable, "SOUND-" + threadCount);
thread.setDaemon(true);
return thread;
}
}) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
t = ThreadUtils.findRealException(r, t);
if (t != null) {
// TODO: show sound errors in client logs?
//logger.error("Catch unhandled error in SOUND thread: " + t.getMessage(), t);
}
}
};
threadPoolSounds.prestartAllCoreThreads();
}
private final Queue<SourceDataLine> freeLines = new ArrayDeque<>();
private final Queue<SourceDataLine> activeLines = new ArrayDeque<>();
private final Set<SourceDataLine> busyLines = new HashSet<>();
@ -106,7 +135,7 @@ public class LinePool {
}
logLineStats();
}
ThreadUtils.threadPoolSounds.submit(() -> {
threadPoolSounds.submit(() -> {
synchronized (LinePool.this) {
try {
if (!line.isOpen()) {