Commit 1fa6ae3
committed
fix concurrency bug in ScheduledObserver
- found a concurrency bug while working on Netflix/Hystrix#123
- the following code would lock up occasionally due to onCompleted not being delivered:
```java
public class RunTest {
public static void main(String[] args) {
System.out.println("Starting test...");
final ArrayList<String> strings = new ArrayList<String>(200000);
int num = 10000;
while (true) {
long start = System.currentTimeMillis();
final AtomicInteger count = new AtomicInteger();
for (int i = 0; i < num; i++) {
new TestService1(2, 5).toObservable().forEach(new Action1<Integer>() {
@OverRide
public void call(Integer v) {
count.addAndGet(v);
}
});
new TestService2("hello").toObservable().forEach(new Action1<String>() {
@OverRide
public void call(String v) {
strings.add(v);
}
});
}
long time = (System.currentTimeMillis() - start);
long executions = num * 2;
System.out.println("Time: " + time + "ms for " + executions + " executions (" + (time * 1000) / executions + " microseconds)");
System.out.println(" Count: " + count);
System.out.println(" Strings: " + strings.size());
strings.clear();
}
}
}
```
- Also made OperationObserveOn not use ScheduledObserver if the `ImmediateScheduler` is chosen to allow an optimization. I believe this optimization is safe because ScheduledObserver does not require knowledge of a Scheduler (such as for now()) and all we do is emit data to the Observer on a scheduler and if we know it's Immediate we can go direct and skip the enqueuing step. This allows shaving off a noticable number of microseconds per execution in the loop above.1 parent 18b1362 commit 1fa6ae3
File tree
2 files changed
+37
-45
lines changed- rxjava-core/src/main/java/rx/operators
2 files changed
+37
-45
lines changedLines changed: 7 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
53 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
54 | 60 | | |
55 | 61 | | |
56 | 62 | | |
| |||
Lines changed: 30 additions & 44 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | 24 | | |
27 | 25 | | |
28 | 26 | | |
29 | 27 | | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | 28 | | |
34 | 29 | | |
35 | 30 | | |
| |||
46 | 41 | | |
47 | 42 | | |
48 | 43 | | |
49 | | - | |
50 | | - | |
| 44 | + | |
| 45 | + | |
51 | 46 | | |
52 | 47 | | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
| 48 | + | |
57 | 49 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 50 | + | |
65 | 51 | | |
66 | 52 | | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
89 | 73 | | |
| 74 | + | |
90 | 75 | | |
91 | | - | |
| 76 | + | |
| 77 | + | |
92 | 78 | | |
0 commit comments