Update MessageTask.java

* Optimize string concatenation.
* Ensure thread safety in multi-threaded contexts.
* Handle exceptions robustly.
* Evaluate whether Task interface usage is necessary or if method references could suffice.
* Ensure onMessage() processing is efficient and scalable.
This commit is contained in:
cwd.systems | 0KN 2024-11-27 17:08:32 +06:00 committed by GitHub
parent 8ffb0625a1
commit a35e1dfe2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,21 +8,30 @@ import org.qortal.utils.ExecuteProduceConsume.Task;
public class MessageTask implements Task {
private final Peer peer;
private final Message nextMessage;
private final String name;
private String name; // Lazy initialization
public MessageTask(Peer peer, Message nextMessage) {
this.peer = peer;
this.nextMessage = nextMessage;
this.name = "MessageTask::" + peer + "::" + nextMessage.getType();
}
@Override
public String getName() {
if (name == null) {
name = "MessageTask::" + peer + "::" + nextMessage.getType();
}
return name;
}
@Override
public void perform() throws InterruptedException {
try {
Network.getInstance().onMessage(peer, nextMessage);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw e;
} catch (Exception e) {
System.err.println("Error processing message task: " + e.getMessage());
}
}
}