Skip to content

Commit 00747f3

Browse files
committedJul 20, 2020
Merge branch 'master' of https://github.com/Justson/Queue
2 parents f42aa54 + fd75352 commit 00747f3

File tree

11 files changed

+2198
-16
lines changed

11 files changed

+2198
-16
lines changed
 

‎.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
.mtj.tmp/
1212

1313
# Package Files #
14-
*.jar
1514
*.war
1615
*.nar
1716
*.ear
@@ -35,4 +34,5 @@ hs_err_pid*
3534
/captures
3635
.externalNativeBuild
3736
.cxx
38-
package.json
37+
package.json
38+
node_modules

‎.idea/inspectionProfiles/Project_Default.xml

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md

+85-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,85 @@
1-
# Queue
1+
# Dispatch-Queue
2+
3+
### 拓展了Android OS MessageQueue
4+
5+
6+
## 引入
7+
8+
* Gradle
9+
10+
11+
```gradle
12+
implementation 'com.github.Justson:dispatch-queue:v1.0.3'
13+
```
14+
15+
16+
## 特性
17+
18+
* 线程可以配对交换元素在同步点
19+
* 线程空闲后执行task
20+
* 阻塞执行
21+
* 线程之间交叉执行
22+
23+
24+
## 控制线程执行顺序
25+
26+
```
27+
final StringBuffer output = new StringBuffer();
28+
AsyncTask.execute(() -> {
29+
output.append(1).append(", ");
30+
GlobalQueue.getMainQueue().postRunnableBlocking(() -> {
31+
// do you work , in the main-Thread
32+
output.append(2).append(", ");
33+
34+
});
35+
output.append(3).append(", ");
36+
37+
// invoke in the main-Thread and return a string data
38+
String message = GlobalQueue.getMainQueue().call(() -> {
39+
output.append(4).append(", ");
40+
return "hello world";
41+
});
42+
output.append(5).append(" data ").append(message);
43+
System.out.println(output.toString());
44+
// output the order "1 2 3 4 5"
45+
});
46+
```
47+
48+
```java
49+
output the order :1, 2, 3, 4, 5
50+
```
51+
52+
53+
54+
## 创建 Dispatch Thread
55+
56+
```
57+
Dispatch messageDispatch = DispatchThread.create("message");
58+
59+
messageDispatch.postRunnable(() -> {
60+
// do you work , work in message thread
61+
});
62+
63+
System.out.println("1");
64+
messageDispatch.postRunnableScissors(() -> {
65+
System.out.println("2");
66+
});
67+
System.out.println("3");
68+
// output 1 2 3
69+
70+
// from message thread get a number, it will blocking until working finish.
71+
int i = messageDispatch.call(() -> 1);
72+
```
73+
74+
75+
76+
## 线程空闲执行
77+
78+
```
79+
messageDispatch.postRunnableInIdleRunning(() -> {
80+
// do your work , when the message thread idle will callback this runable
81+
});
82+
```
83+
84+
85+

‎app/build.gradle

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ android {
2020
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
2121
}
2222
}
23+
compileOptions {
24+
sourceCompatibility = 1.8
25+
targetCompatibility = 1.8
26+
}
2327

2428
}
2529

@@ -31,4 +35,6 @@ dependencies {
3135
testImplementation 'junit:junit:4.12'
3236
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3337
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
38+
// implementation project(path: ':library')
39+
implementation 'com.github.Justson:dispatch-queue:v1.0.3'
3440
}

‎app/src/main/java/com/queue/sample/MainActivity.java

+73
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import android.os.Bundle;
44

5+
import com.queue.library.Dispatch;
6+
import com.queue.library.DispatchThread;
7+
58
import androidx.appcompat.app.AppCompatActivity;
69

710
public class MainActivity extends AppCompatActivity {
@@ -10,5 +13,75 @@ public class MainActivity extends AppCompatActivity {
1013
protected void onCreate(Bundle savedInstanceState) {
1114
super.onCreate(savedInstanceState);
1215
setContentView(R.layout.activity_main);
16+
17+
18+
// /**
19+
// * use a
20+
// */
21+
// final StringBuffer output = new StringBuffer();
22+
// AsyncTask.execute(() -> {
23+
// output.append(1).append("-").append(Thread.currentThread().getName()).append(", ");
24+
// GlobalQueue.getMainQueue().postRunnableBlocking(() -> {
25+
// // do you work , in the main-Thread
26+
// output.append(2).append("-").append(Thread.currentThread().getName()).append(", ");
27+
//
28+
// });
29+
// output.append(3).append("-").append(Thread.currentThread().getName()).append(", ");
30+
//
31+
// // invoke in the main-Thread and return a string data
32+
// String message = GlobalQueue.getMainQueue().call(() -> {
33+
// output.append(4).append("-").append(Thread.currentThread().getName()).append(", ");
34+
// return "hello world";
35+
// });
36+
// output.append(5).append("-").append(Thread.currentThread().getName()).append(" data ").append(message);
37+
// System.out.println(output.toString());
38+
// // output the order "1 2 3 4"
39+
// });
40+
41+
// final StringBuffer output = new StringBuffer();
42+
// AsyncTask.execute(() -> {
43+
// output.append(1).append(", ");
44+
// GlobalQueue.getMainQueue().postRunnableBlocking(() -> {
45+
// // do you work , in the main-Thread
46+
// output.append(2).append(", ");
47+
//
48+
// });
49+
// output.append(3).append(", ");
50+
//
51+
// // invoke in the main-Thread and return a string data
52+
// String message = GlobalQueue.getMainQueue().call(() -> {
53+
// output.append(4).append(", ");
54+
// return "hello world";
55+
// });
56+
// output.append(5).append(" data ").append(message);
57+
// System.out.println(output.toString());
58+
// // output the order "1 2 3 4"
59+
// });
60+
61+
/**
62+
* use b
63+
*/
64+
Dispatch messageDispatch = DispatchThread.create("message");
65+
66+
messageDispatch.postRunnable(() -> {
67+
// do you work , work in message thread
68+
});
69+
70+
System.out.println("1");
71+
messageDispatch.postRunnableScissors(() -> {
72+
System.out.println("2");
73+
});
74+
System.out.println("3");
75+
// output 1 2 3
76+
77+
// from message thread get a number, it will blocking until working finish.
78+
int i = messageDispatch.call(() -> 1);
79+
80+
81+
messageDispatch.postRunnableInIdleRunning(() -> {
82+
// do your work , when the message thread idle will callback this runable
83+
});
1384
}
85+
86+
1487
}

‎build.gradle

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
buildscript {
44

55
repositories {
6+
maven { url 'https://jitpack.io' }
67
google()
78
jcenter()
8-
99
}
1010
dependencies {
11-
classpath 'com.android.tools.build:gradle:3.6.1'
12-
11+
classpath 'com.android.tools.build:gradle:3.6.3'
1312

1413
// NOTE: Do not place your application dependencies here; they belong
1514
// in the individual module build.gradle files
@@ -18,9 +17,9 @@ buildscript {
1817

1918
allprojects {
2019
repositories {
20+
maven { url 'https://jitpack.io' }
2121
google()
2222
jcenter()
23-
2423
}
2524
}
2625

‎gradle/wrapper/gradle-wrapper.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Thu Jul 09 09:51:20 CST 2020
1+
#Sat Jul 11 22:33:09 CST 2020
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME

‎library/src/main/java/com/queue/library/Dispatch.java

-7
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,6 @@ public void postRunnableImmediately(Runnable runnable) {
194194
postAtFont(runnable);
195195
}
196196

197-
public void postRunnableQuick(Runnable runnable) {
198-
if (Looper.myLooper() == getLooper()) {
199-
runnable.run();
200-
return;
201-
}
202-
postRunnable(runnable);
203-
}
204197

205198
public void post(Runnable runnable) {
206199
if (Looper.myLooper() == getLooper()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.queue.library;
2+
3+
import android.os.HandlerThread;
4+
import android.os.Looper;
5+
import android.os.Process;
6+
7+
/**
8+
* @author cenxiaozhong
9+
* @date 2020/7/11
10+
* @since 1.0.0
11+
*/
12+
public final class DispatchThread {
13+
14+
public static Dispatch create(String name) {
15+
return create(name, Process.THREAD_PRIORITY_DEFAULT);
16+
}
17+
18+
public static Dispatch create(String name, int priority) {
19+
HandlerThread handlerThread = new HandlerThread(name, priority);
20+
handlerThread.start();
21+
Looper looper = handlerThread.getLooper();
22+
return new Dispatch(looper);
23+
}
24+
}

‎package-lock.json

+1,967
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.