Skip to content

Commit

Permalink
Added protected methods to customize client thread names.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Jun 16, 2024
1 parent cd08f83 commit 5cac132
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected void startConnectionLostTimer() {
private void restartConnectionLostTimer() {
cancelConnectionLostTimer();
connectionLostCheckerService = Executors
.newSingleThreadScheduledExecutor(new NamedThreadFactory("connectionLostChecker", daemon));
.newSingleThreadScheduledExecutor(new NamedThreadFactory(connectionLostThreadPrefix(), daemon));
Runnable connectionLostChecker = new Runnable() {

/**
Expand Down Expand Up @@ -233,6 +233,10 @@ public void run() {
TimeUnit.NANOSECONDS);
}

protected String connectionLostThreadPrefix () {
return "connectionLostChecker";
}

/**
* Send a ping to the endpoint or close the connection since the other endpoint did not respond
* with a ping
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,18 @@ public void connect() {
}
connectReadThread = new Thread(this);
connectReadThread.setDaemon(isDaemon());
connectReadThread.setName("WebSocketConnectReadThread-" + connectReadThread.getId());
connectReadThread.setName(connectReadThreadName(connectReadThread.getId()));
connectReadThread.start();
}

protected String connectReadThreadName (long threadId) {
return "WebSocketConnectReadThread-" + threadId;
}

protected String writeThreadName (long threadId) {
return "WebSocketWriteThread-" + threadId;
}

/**
* Same as <code>connect</code> but blocks until the websocket connected or failed to do so.<br>
*
Expand Down Expand Up @@ -819,7 +827,7 @@ private class WebsocketWriteThread implements Runnable {

@Override
public void run() {
Thread.currentThread().setName("WebSocketWriteThread-" + Thread.currentThread().getId());
Thread.currentThread().setName(writeThreadName(Thread.currentThread().getId()));
try {
runWriteData();
} catch (IOException e) {
Expand Down

0 comments on commit 5cac132

Please sign in to comment.