Skip to content
Advertisement

Executor service returning incorrect response

I am creating future list from a list of calls to executor service submit method based on student ID. The response from service is not returning for all studentId’s. It runs for the right number of times but the studentId getting used in service call is either first or last. It is ignoring the middle ones. Please check the code below

JavaScript

In the below code the service is getting called 10 times but for only student Id 1 and 10. Not able to get result of 2 to 9 which is resulting in an inaccurate result. Need help in understanding if i am missing anything here.

Advertisement

Answer

Your code is faulty to the point of not compiling, as noted in my Comment. And you’ve omitted some possibly important code. So I cannot diagnose exactly what problems you have. So I will revamp your code in my own style, and get it working.

Rather than trying to fill a list of Future objects, I would fill a list of tasks, either Runnable or Callable objects. You can then call ExecutorService#invokeAll to submit all the tasks. You get back a list of Future objects to track the completion of your submitted tasks.

First, let’s define the Student class as a record.

JavaScript

It seems to me you have mixed two different responsibilities into the StudentService class. That class should focus only on holding the students’s data. That class should not be the Callable. Define the Callable separately, and pass the StudentService object to its constructor.

Notice that we return an Optional. If the calling programmer provides an invalid student ID, we return an empty Optional rather than a null pointer.

JavaScript

Notice in code above that the fetchStudentById is marked synchronized. We know this method will be invoked across threads. The current implementation here may be thread-safe, by streaming over a non-modifiable List. But in real work, this look-up may not be thread-safe. So we mark it synchronized for thread-safety.

If you are not comfortable with streams as seen in that code above, know that you could accomplish the same effect with a conventional loop. Using streams makes for briefer code, but the use of streams here is not important.

Define our task, a Callable that looks up a student by ID, and returns a Optional < Student >. We pass to its constructor the StudentService object to be used to actually find the student. And we pass the id of the desired student.

JavaScript

Now we are ready to try this out.

Instantiate a StudentService object to be used by all our tasks.

JavaScript

Establish a list of task objects. Pass the service and id to each.

JavaScript

Prepare the executor service, and the list of Future objects we expect it to fill.

JavaScript

Submit all those tasks to the executor service.

JavaScript

Shutdown the executor service, and wait for results. The following is boilerplate code taken from Javadoc, and slightly modified.

JavaScript

Lastly, report the results of our tasks by examining each Future object. In real work, you would likely interrogate the future for its completion status, but I’ll leave that as an exercise for the reader.

JavaScript

When run.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement