Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
0mega28 opened this issue Aug 6, 2024 · 0 comments

Comments

@0mega28
Copy link

0mega28 commented Aug 6, 2024

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();
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant