-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
2.x: internal API to get distinct Workers from some Schedulers #5741
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.schedulers; | ||
|
||
import io.reactivex.Scheduler; | ||
import io.reactivex.annotations.*; | ||
|
||
/** | ||
* Allows retrieving multiple workers from the implementing | ||
* {@link io.reactivex.Scheduler} in a way that when asking for | ||
* at most the parallelism level of the Scheduler, those | ||
* {@link io.reactivex.Scheduler.Worker} instances will be running | ||
* with different backing threads. | ||
* | ||
* @since 2.1.7 - experimental | ||
*/ | ||
@Experimental | ||
public interface SchedulerMultiWorkerSupport { | ||
|
||
/** | ||
* Creates the given number of {@link io.reactivex.Scheduler.Worker} instances | ||
* that are possibly backed by distinct threads | ||
* and calls the specified {@code Consumer} with them. | ||
* @param number the number of workers to create, positive | ||
* @param callback the callback to send worker instances to | ||
*/ | ||
void createWorkers(int number, @NonNull WorkerCallback callback); | ||
|
||
/** | ||
* The callback interface for the {@link SchedulerMultiWorkerSupport#createWorkers(int, WorkerCallback)} | ||
* method. | ||
*/ | ||
interface WorkerCallback { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While still it is an internal API, I don't think we need a more complicated naming. |
||
/** | ||
* Called with the Worker index and instance. | ||
* @param index the worker index, zero-based | ||
* @param worker the worker instance | ||
*/ | ||
void onWorker(int index, @NonNull Scheduler.Worker worker); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the use case for the index? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, potentially reduces allocations for the user, thanks |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.schedulers; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.*; | ||
|
||
import org.junit.Test; | ||
|
||
import io.reactivex.Scheduler.Worker; | ||
import io.reactivex.TestHelper; | ||
import io.reactivex.disposables.CompositeDisposable; | ||
import io.reactivex.internal.schedulers.SchedulerMultiWorkerSupport.WorkerCallback; | ||
import io.reactivex.schedulers.Schedulers; | ||
|
||
public class SchedulerMultiWorkerSupportTest { | ||
|
||
final int max = ComputationScheduler.MAX_THREADS; | ||
|
||
@Test | ||
public void moreThanMaxWorkers() { | ||
final List<Worker> list = new ArrayList<Worker>(); | ||
|
||
SchedulerMultiWorkerSupport mws = (SchedulerMultiWorkerSupport)Schedulers.computation(); | ||
|
||
mws.createWorkers(max * 2, new WorkerCallback() { | ||
@Override | ||
public void onWorker(int i, Worker w) { | ||
list.add(w); | ||
} | ||
}); | ||
|
||
assertEquals(max * 2, list.size()); | ||
} | ||
|
||
@Test | ||
public void getShutdownWorkers() { | ||
final List<Worker> list = new ArrayList<Worker>(); | ||
|
||
ComputationScheduler.NONE.createWorkers(max * 2, new WorkerCallback() { | ||
@Override | ||
public void onWorker(int i, Worker w) { | ||
list.add(w); | ||
} | ||
}); | ||
|
||
assertEquals(max * 2, list.size()); | ||
|
||
for (Worker w : list) { | ||
assertEquals(ComputationScheduler.SHUTDOWN_WORKER, w); | ||
} | ||
} | ||
|
||
@Test | ||
public void distinctThreads() throws Exception { | ||
for (int i = 0; i < 1000; i++) { | ||
|
||
final CompositeDisposable composite = new CompositeDisposable(); | ||
|
||
try { | ||
final CountDownLatch cdl = new CountDownLatch(max * 2); | ||
|
||
final Set<String> threads1 = Collections.synchronizedSet(new HashSet<String>()); | ||
|
||
final Set<String> threads2 = Collections.synchronizedSet(new HashSet<String>()); | ||
|
||
Runnable parallel1 = new Runnable() { | ||
@Override | ||
public void run() { | ||
final List<Worker> list1 = new ArrayList<Worker>(); | ||
|
||
SchedulerMultiWorkerSupport mws = (SchedulerMultiWorkerSupport)Schedulers.computation(); | ||
|
||
mws.createWorkers(max, new WorkerCallback() { | ||
@Override | ||
public void onWorker(int i, Worker w) { | ||
list1.add(w); | ||
composite.add(w); | ||
} | ||
}); | ||
|
||
Runnable run = new Runnable() { | ||
@Override | ||
public void run() { | ||
threads1.add(Thread.currentThread().getName()); | ||
cdl.countDown(); | ||
} | ||
}; | ||
|
||
for (Worker w : list1) { | ||
w.schedule(run); | ||
} | ||
} | ||
}; | ||
|
||
Runnable parallel2 = new Runnable() { | ||
@Override | ||
public void run() { | ||
final List<Worker> list2 = new ArrayList<Worker>(); | ||
|
||
SchedulerMultiWorkerSupport mws = (SchedulerMultiWorkerSupport)Schedulers.computation(); | ||
|
||
mws.createWorkers(max, new WorkerCallback() { | ||
@Override | ||
public void onWorker(int i, Worker w) { | ||
list2.add(w); | ||
composite.add(w); | ||
} | ||
}); | ||
|
||
Runnable run = new Runnable() { | ||
@Override | ||
public void run() { | ||
threads2.add(Thread.currentThread().getName()); | ||
cdl.countDown(); | ||
} | ||
}; | ||
|
||
for (Worker w : list2) { | ||
w.schedule(run); | ||
} | ||
} | ||
}; | ||
|
||
TestHelper.race(parallel1, parallel2); | ||
|
||
assertTrue(cdl.await(5, TimeUnit.SECONDS)); | ||
|
||
assertEquals(threads1.toString(), max, threads1.size()); | ||
assertEquals(threads2.toString(), max, threads2.size()); | ||
} finally { | ||
composite.dispose(); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cores
isfinal
in this context, why copy it to local var?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With potential volatiles around, this won't re-read the field every time it is needed in the loop below.