Skip to content

Is it incorrect to schedule the start method before the end method this way? #4

@0mega28

Description

@0mega28

public void end(String processId) { //1
taskScheduler[processId.hashCode() % taskScheduler.length].execute(() -> {

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();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions