Reputation: 4207
I have the below code for which Sonar Qube is raising a critical error.
public void onMessageReceived(Message message) {
if((message.getTargetInstanceId() != Message.ANY_TARGET) && (message.getTargetInstanceId() != this.instanceId)) {
//Invalid message ignore
return;
} else {
int randTime = ran.nextInt(2000);
FutureTask<Message> workerTask = new FutureTask<>(new DummyCommandHandler(randTime, message,messageBus,instanceId));
workerTask.run();
}
}
Below is the issue I am getting.
Description Assignee Resource New issue
Thread.run() and Runnable.run() should not be called directly : Call the method Thread.start() to execute the content of the run() method in a dedicated thread.
To call a start method in FutureTask, it doesn't have a start method in the first place. It only has a run method. How can I overcome this issue? Are there any Java solutions or Sonarqube solutions? Please advice.
Upvotes: 2
Views: 991
Reputation: 14661
SonarQube is right, you should not directly call futureTask.run()
. Use an ExecutorService instead:
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(futureTask1);
This is a short and good tutorial on the topic.
Upvotes: 5