Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protected methods to customize client thread names. #1422

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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