Skip to content

Commit 90deba9

Browse files
committed
Named all threads
1 parent 0b5b81a commit 90deba9

9 files changed

Lines changed: 48 additions & 11 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package xyz.gianlu.librespot.common;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import java.util.concurrent.ThreadFactory;
6+
7+
/**
8+
* @author Gianlu
9+
*/
10+
public final class NameThreadFactory implements ThreadFactory {
11+
private final ThreadGroup group;
12+
private final NameProvider nameProvider;
13+
14+
public NameThreadFactory(@NotNull NameProvider nameProvider) {
15+
this.nameProvider = nameProvider;
16+
SecurityManager s = System.getSecurityManager();
17+
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
18+
}
19+
20+
@Override
21+
public Thread newThread(@NotNull Runnable r) {
22+
Thread t = new Thread(group, r, nameProvider.getName(r), 0);
23+
if (t.isDaemon()) t.setDaemon(false);
24+
if (t.getPriority() != Thread.NORM_PRIORITY) t.setPriority(Thread.NORM_PRIORITY);
25+
return t;
26+
}
27+
28+
public interface NameProvider {
29+
@NotNull
30+
String getName(@NotNull Runnable r);
31+
}
32+
}

core/src/main/java/xyz/gianlu/librespot/core/FacebookAuthenticator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import com.google.protobuf.ByteString;
66
import org.apache.log4j.Logger;
77
import org.jetbrains.annotations.NotNull;
8-
import xyz.gianlu.librespot.common.Utils;
98
import xyz.gianlu.librespot.Version;
9+
import xyz.gianlu.librespot.common.Utils;
1010
import xyz.gianlu.librespot.common.proto.Authentication;
1111

1212
import javax.net.ssl.SSLSocketFactory;
@@ -55,7 +55,7 @@ public class FacebookAuthenticator implements Closeable {
5555

5656
private void startPolling() throws IOException {
5757
polling = new HttpPolling();
58-
new Thread(polling).start();
58+
new Thread(polling, "facebook-auth-polling").start();
5959
}
6060

6161
@NotNull

core/src/main/java/xyz/gianlu/librespot/core/PacketsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public PacketsManager(@NotNull Session session) {
2222
this.executorService = session.executor();
2323
this.queue = new LinkedBlockingQueue<>();
2424
this.looper = new Looper();
25-
new Thread(looper).start();
25+
new Thread(looper, "packets-manager-" + looper.hashCode()).start();
2626
}
2727

2828
public final void dispatch(@NotNull Packet packet) {

core/src/main/java/xyz/gianlu/librespot/core/Session.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.jetbrains.annotations.NotNull;
66
import xyz.gianlu.librespot.AbsConfiguration;
77
import xyz.gianlu.librespot.Version;
8+
import xyz.gianlu.librespot.common.NameThreadFactory;
89
import xyz.gianlu.librespot.common.Utils;
910
import xyz.gianlu.librespot.common.proto.Authentication;
1011
import xyz.gianlu.librespot.common.proto.Keyexchange;
@@ -49,7 +50,7 @@ public class Session implements AutoCloseable {
4950
private final DataInputStream in;
5051
private final DataOutputStream out;
5152
private final Inner inner;
52-
private final ExecutorService executorService = Executors.newCachedThreadPool();
53+
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory(r -> "handle-packet-" + r.hashCode()));
5354
private CipherPair cipherPair;
5455
private Receiver receiver;
5556
private Authentication.APWelcome apWelcome = null;
@@ -214,7 +215,7 @@ private void authenticate(@NotNull Authentication.LoginCredentials credentials)
214215
apWelcome = Authentication.APWelcome.parseFrom(packet.payload);
215216
mercuryClient = new MercuryClient(this);
216217
receiver = new Receiver();
217-
new Thread(receiver).start();
218+
new Thread(receiver, "session-packet-receiver").start();
218219

219220
audioKeyManager = new AudioKeyManager(this);
220221
channelManager = new ChannelManager(this);

core/src/main/java/xyz/gianlu/librespot/core/ZeroconfAuthenticator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class ZeroconfAuthenticator implements Closeable {
6868
this.mDnsService = new MulticastDNSService();
6969

7070
int port = session.random.nextInt((MAX_PORT - MIN_PORT) + 1) + MIN_PORT;
71-
new Thread(this.runner = new HttpRunner(port)).start();
71+
new Thread(this.runner = new HttpRunner(port), "zeroconf-http-server").start();
7272

7373
InetAddress[] bound;
7474
if (conf.zeroconfListenAll()) {

core/src/main/java/xyz/gianlu/librespot/player/AudioFileStreaming.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.protobuf.ByteString;
44
import org.apache.log4j.Logger;
55
import org.jetbrains.annotations.NotNull;
6+
import xyz.gianlu.librespot.common.NameThreadFactory;
67
import xyz.gianlu.librespot.common.Utils;
78
import xyz.gianlu.librespot.common.proto.Metadata;
89
import xyz.gianlu.librespot.core.Session;
@@ -25,7 +26,7 @@ public class AudioFileStreaming implements AudioFile {
2526
private final ByteString fileId;
2627
private final byte[] key;
2728
private final Session session;
28-
private final ExecutorService executorService = Executors.newCachedThreadPool();
29+
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory(r -> "request-chunk-" + r.hashCode()));
2930
private int chunks = -1;
3031
private ChunksBuffer chunksBuffer;
3132

core/src/main/java/xyz/gianlu/librespot/player/CacheManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.log4j.Logger;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
7+
import xyz.gianlu.librespot.common.NameThreadFactory;
78
import xyz.gianlu.librespot.common.Utils;
89

910
import java.io.*;
@@ -27,7 +28,7 @@ public class CacheManager {
2728
private final boolean enabled;
2829
private final Map<String, Handler> loadedHandlers;
2930
private final ControlTable controlTable;
30-
private final ExecutorService executorService = Executors.newCachedThreadPool();
31+
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory(r -> "cache-io-" + r.hashCode()));
3132

3233
public CacheManager(@NotNull CacheConfiguration conf) throws IOException {
3334
this.enabled = conf.cacheEnabled();

core/src/main/java/xyz/gianlu/librespot/player/ChannelManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.protobuf.ByteString;
44
import org.apache.log4j.Logger;
55
import org.jetbrains.annotations.NotNull;
6+
import xyz.gianlu.librespot.common.NameThreadFactory;
67
import xyz.gianlu.librespot.common.Utils;
78
import xyz.gianlu.librespot.core.PacketsManager;
89
import xyz.gianlu.librespot.core.Session;
@@ -28,7 +29,7 @@ public class ChannelManager extends PacketsManager {
2829
private static final Logger LOGGER = Logger.getLogger(ChannelManager.class);
2930
private final Map<Short, Channel> channels = new HashMap<>();
3031
private final AtomicInteger seqHolder = new AtomicInteger(0);
31-
private final ExecutorService executorService = Executors.newCachedThreadPool();
32+
private final ExecutorService executorService = Executors.newCachedThreadPool(new NameThreadFactory(r -> "channel-queue-" + r.hashCode()));
3233

3334
public ChannelManager(@NotNull Session session) {
3435
super(session);

core/src/main/java/xyz/gianlu/librespot/player/TrackHandler.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class TrackHandler implements PlayerRunner.Listener, Closeable {
3737
this.listener = listener;
3838
this.feeder = new StreamFeeder(session, cacheManager);
3939

40-
new Thread(new Looper()).start();
40+
Looper looper;
41+
new Thread(looper = new Looper(), "track-handler-" + looper.hashCode()).start();
4142
}
4243

4344
private void load(@NotNull TrackId id, boolean play, int pos) throws IOException, MercuryClient.MercuryException {
@@ -51,7 +52,7 @@ private void load(@NotNull TrackId id, boolean play, int pos) throws IOException
5152
try {
5253
if (playerRunner != null) playerRunner.stop();
5354
playerRunner = new PlayerRunner(stream.in, stream.normalizationData, lines, conf, this, track.getDuration());
54-
new Thread(playerRunner).start();
55+
new Thread(playerRunner, "player-runner-" + playerRunner.hashCode()).start();
5556

5657
playerRunner.seek(pos);
5758

0 commit comments

Comments
 (0)