Skip to content

Commit 1514719

Browse files
author
YangSen-qn
committed
change func runInMain
1 parent aef5a0a commit 1514719

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

library/src/androidTest/java/com/qiniu/android/AsynTest.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.qiniu.android;
22

3+
import android.os.Looper;
4+
35
import com.qiniu.android.utils.AsyncRun;
46
import com.qiniu.android.utils.LogUtil;
57

@@ -11,7 +13,7 @@ private class TestParam {
1113
int completeCount;
1214
int successCount;
1315
}
14-
public void testAsyncMain(){
16+
public void testAsyncMainOnMainThread(){
1517

1618
final TestParam testParam = new TestParam();
1719
testParam.maxCount = 100;
@@ -22,11 +24,14 @@ public void testAsyncMain(){
2224
AsyncRun.runInMain(new Runnable() {
2325
@Override
2426
public void run() {
25-
String threadName = Thread.currentThread().getName();
26-
if (threadName.equals("main")){
27+
28+
if (Thread.currentThread() == Looper.getMainLooper().getThread()){
2729
testParam.successCount += 1;
2830
}
2931

32+
String threadName = Thread.currentThread().getName();
33+
LogUtil.d("thread name:" + threadName);
34+
3035
testParam.completeCount += 1;
3136
}
3237
});
@@ -49,6 +54,53 @@ public boolean shouldWait() {
4954
assertTrue((testParam.successCount == testParam.maxCount));
5055
}
5156

57+
public void testAsyncMainOnOtherThread(){
58+
59+
final TestParam testParam = new TestParam();
60+
testParam.maxCount = 10;
61+
testParam.completeCount = 0;
62+
testParam.successCount = 0;
63+
for (int i = 0; i < testParam.maxCount; i++) {
64+
final int i_p = i;
65+
66+
new Thread(new Runnable() {
67+
@Override
68+
public void run() {
69+
AsyncRun.runInMain(new Runnable() {
70+
@Override
71+
public void run() {
72+
73+
if (Thread.currentThread().getName().equals("main")){
74+
testParam.successCount += 1;
75+
}
76+
77+
String threadName = Thread.currentThread().getName();
78+
LogUtil.d("thread name:" + threadName);
79+
80+
testParam.completeCount += 1;
81+
}
82+
});
83+
}
84+
}).start();
85+
}
86+
87+
WaitConditional waitConditional = new WaitConditional() {
88+
@Override
89+
public boolean shouldWait() {
90+
if (testParam.completeCount == testParam.maxCount){
91+
return false;
92+
} else {
93+
return true;
94+
}
95+
}
96+
};
97+
98+
wait(waitConditional, 5);
99+
100+
LogUtil.i(String.format("success count: %d", testParam.successCount));
101+
assertTrue((testParam.successCount == testParam.maxCount));
102+
}
103+
52104
public void testAsyncBg(){
53105
final TestParam testParam = new TestParam();
54106
testParam.maxCount = 100;

library/src/main/java/com/qiniu/android/utils/AsyncRun.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
*/
1515
public final class AsyncRun {
1616

17+
private static final Handler mainThreadHandler = new Handler(Looper.getMainLooper());
1718
private static final ExecutorService executorService = Executors.newFixedThreadPool(3);
1819

1920
public static void runInMain(Runnable r) {
20-
Handler h = new Handler(Looper.getMainLooper());
21-
h.post(r);
21+
if (Looper.getMainLooper() == Looper.myLooper()){
22+
r.run();
23+
} else {
24+
mainThreadHandler.post(r);
25+
}
2226
}
2327

2428
/**
@@ -30,8 +34,7 @@ public static void runInMain(int delay,
3034
delayTimerTask(delay, new TimerTask() {
3135
@Override
3236
public void run() {
33-
Handler h = new Handler(Looper.getMainLooper());
34-
h.post(r);
37+
mainThreadHandler.post(r);
3538
this.cancel();
3639
}
3740
});

0 commit comments

Comments
 (0)