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

1.x: fix ExecutorScheduler and GenericScheduledExecutorService reorder bug #3760

Merged
merged 1 commit into from
Mar 17, 2016

Conversation

akarnokd
Copy link
Member

This PR relates to the failure of errorThrownIssue1685.

The underlying problem was with the GenericScheduledExecutorService. By being multi-threaded, tasks scheduled from the same thread one after the other may get reordered because different worker threads inside the pool could pick them up at the same time. In this case, there is no guarantee they keep their FIFO order.

(I currently have no idea how one can use trampolining for this case; subsequent tasks may have any relative delays in respect to each other.)

The solution creates N single threaded ScheduledExecutorServices and getInstance() hands one of them out. In turn ExecutorService takes one in its worker upfront.

However, there is still the problem when the programmer uses a multi-threaded ScheduledExecutorService with Schedulers.from() when the same issue comes back. A solution to that problem would be to always use the new GenericScheduledExecutorService for delaying timed tasks.

NONE = Executors.newScheduledThreadPool(0);
NONE.shutdownNow();
SHUTDOWN = Executors.newScheduledThreadPool(0);
SHUTDOWN.shutdown();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ew

I definitely don't like that library will create executor when this class will be loaded. Even without threads used it looks strange.

Maybe return new each time getInstance()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executors don't start their thread until the first task is submitted.

@stevegury
Copy link
Member

I have trouble understanding how this solve the problem, could you please elaborate?
The only thing I see that we could do to force the FIFO order is to cap the thread number to 1, right?

@akarnokd
Copy link
Member Author

Yes, ExecutorScheduler's worker needs a helper ScheduledExecutorService with a single thread only. But we don't want all ExecutorSchedulers to wait in a single thread for their time to run. This change, similar to how computation scheduler works, hands out single hreaded ScheduledExecutorServices on demand.

@zsxwing
Copy link
Member

zsxwing commented Mar 16, 2016

Nice catch.

However, there is still the problem when the programmer uses a multi-threaded ScheduledExecutorService with Schedulers.from() when the same issue comes back. A solution to that problem would be to always use the new GenericScheduledExecutorService for delaying timed tasks.

I vote for always use the new GenericScheduledExecutorService for delaying timed tasks.. Correctness is more important than performance. Moreover, when people use schedule(action, time), they usually don't want the codes to run as fast as possible, so the performance lost doesn't really matter.

@stevegury
Copy link
Member

Agree with @zsxwing that correctness is more important than performance.


ScheduledExecutorService[] execs = new ScheduledExecutorService[count];
for (int i = 0; i < count; i++) {
execs[i] = Executors.newScheduledThreadPool(count, THREAD_FACTORY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count should be replaced with 1 to guarantee FIFO. Right?

@akarnokd
Copy link
Member Author

Right, I'll fix them shortly.

@akarnokd akarnokd force-pushed the ErrorThrownIssue1685Again2 branch from 3252de0 to c36456a Compare March 16, 2016 21:42
@akarnokd
Copy link
Member Author

Updated.

@zsxwing
Copy link
Member

zsxwing commented Mar 16, 2016

👍

1 similar comment
@stevegury
Copy link
Member

👍

stevegury added a commit that referenced this pull request Mar 17, 2016
1.x: fix ExecutorScheduler and GenericScheduledExecutorService reorder bug
@stevegury stevegury merged commit a92a077 into ReactiveX:1.x Mar 17, 2016
@akarnokd akarnokd deleted the ErrorThrownIssue1685Again2 branch May 18, 2016 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants