-
Notifications
You must be signed in to change notification settings - Fork 81
Description
youtube-projects/java-projects/src/main/java/logger/LogClient.java
Lines 63 to 64 in 7e2275a
| public void end(String processId) { //1 | |
| taskScheduler[processId.hashCode() % taskScheduler.length].execute(() -> { |
youtube-projects/java-projects/src/main/java/logger/LogClient.java
Lines 54 to 55 in 7e2275a
| public void start(String processId, long timestamp) { //1 | |
| taskScheduler[processId.hashCode() % taskScheduler.length].execute(() -> { |
We are trying to schedule the start method before the end method by using Single Thread Executor.
Let's say we scheduled two threads first thread is calling start method and second thread is calling end method.
Now, OS may schedule the end thread to execute first (which we don't want).
Now, with the above approach the problem is still there because, we can't ensure which thread hits execute method
of Executor Service first.
My approach of solving the problem is,
In the end method start a new Thread which polls the ConcurrentMap if the process Id is added via the start method.
@Override
public void end(final String processId) {
long now = System.currentTimeMillis();
new Thread(() -> {
Process process;
while ((process = processes.get(processId)) == null);
process.setEndTime(now);
processes.remove(processId);
}).start();
}