forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into idiomaticscala
- Loading branch information
Showing
22 changed files
with
741 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=0.13.3-SNAPSHOT | ||
version=0.13.5-SNAPSHOT |
21 changes: 21 additions & 0 deletions
21
language-adaptors/rxjava-groovy/src/test/groovy/rx/lang/groovy/TestParallel.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package rx.lang.groovy | ||
|
||
import org.junit.Test | ||
|
||
import rx.Observable | ||
import rx.Scheduler | ||
import rx.concurrency.Schedulers | ||
import rx.util.functions.Func1 | ||
|
||
class TestParallel { | ||
|
||
@Test | ||
public void testParallelOperator() { | ||
Observable.range(0, 100) | ||
.parallel({ | ||
it.map({ return it; }) | ||
}) | ||
.toBlockingObservable() | ||
.forEach({ println("T: " + it + " Thread: " + Thread.currentThread()); }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
rxjava-core/src/main/java/rx/operators/OperationParallel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* Copyright 2013 Netflix, Inc. | ||
* | ||
* 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 rx.operators; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.junit.Test; | ||
|
||
import rx.Observable; | ||
import rx.Scheduler; | ||
import rx.concurrency.Schedulers; | ||
import rx.observables.GroupedObservable; | ||
import rx.util.functions.Action1; | ||
import rx.util.functions.Func0; | ||
import rx.util.functions.Func1; | ||
|
||
/** | ||
* Identifies unit of work that can be executed in parallel on a given Scheduler. | ||
*/ | ||
public final class OperationParallel<T> { | ||
|
||
public static <T, R> Observable<R> parallel(Observable<T> source, Func1<Observable<T>, Observable<R>> f) { | ||
return parallel(source, f, Schedulers.threadPoolForComputation()); | ||
} | ||
|
||
public static <T, R> Observable<R> parallel(final Observable<T> source, final Func1<Observable<T>, Observable<R>> f, final Scheduler s) { | ||
return Observable.defer(new Func0<Observable<R>>() { | ||
|
||
@Override | ||
public Observable<R> call() { | ||
final AtomicInteger i = new AtomicInteger(0); | ||
return source.groupBy(new Func1<T, Integer>() { | ||
|
||
@Override | ||
public Integer call(T t) { | ||
return i.incrementAndGet() % s.degreeOfParallelism(); | ||
} | ||
|
||
}).flatMap(new Func1<GroupedObservable<Integer, T>, Observable<R>>() { | ||
|
||
@Override | ||
public Observable<R> call(GroupedObservable<Integer, T> group) { | ||
return f.call(group.observeOn(s)); | ||
} | ||
}).synchronize(); | ||
} | ||
}); | ||
} | ||
|
||
public static class UnitTest { | ||
|
||
@Test | ||
public void testParallel() { | ||
int NUM = 1000; | ||
final AtomicInteger count = new AtomicInteger(); | ||
Observable.range(1, NUM).parallel( | ||
new Func1<Observable<Integer>, Observable<Integer[]>>() { | ||
|
||
@Override | ||
public Observable<Integer[]> call(Observable<Integer> o) { | ||
return o.map(new Func1<Integer, Integer[]>() { | ||
|
||
@Override | ||
public Integer[] call(Integer t) { | ||
return new Integer[] { t, t * 99 }; | ||
} | ||
|
||
}); | ||
} | ||
}).toBlockingObservable().forEach(new Action1<Integer[]>() { | ||
|
||
@Override | ||
public void call(Integer[] v) { | ||
count.incrementAndGet(); | ||
System.out.println("V: " + v[0] + " R: " + v[1] + " Thread: " + Thread.currentThread()); | ||
} | ||
|
||
}); | ||
|
||
// just making sure we finish and get the number we expect | ||
assertEquals(NUM, count.get()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.