mirror of
https://github.com/Qortal/qortal.git
synced 2025-04-01 17:55:54 +00:00
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:
parent
8ffb0625a1
commit
a35e1dfe2a
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user