Skip to content

Commit ba1772a

Browse files
committed
feat(dispatch): provide dispatch thread to create new thread
1 parent f23ef5b commit ba1772a

File tree

7,292 files changed

+290929
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

7,292 files changed

+290929
-9
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 5 additions & 0 deletions
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,5 @@ 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')
3439
}

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

Lines changed: 73 additions & 0 deletions
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
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,6 @@ public void postRunnableImmediately(Runnable runnable) {
193193
postAtFont(runnable);
194194
}
195195

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

204197
public void post(Runnable runnable) {
205198
if (Looper.myLooper() == getLooper()) {
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
package com.queue.library;
22

3+
import android.os.HandlerThread;
4+
import android.os.Looper;
5+
import android.os.Process;
6+
37
/**
48
* @author cenxiaozhong
59
* @date 2020/7/11
610
* @since 1.0.0
711
*/
8-
public class DispatchThread {
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+
}
924
}

node_modules/.bin/atob

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/commitizen

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/git-cz

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/which

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@babel/code-frame/LICENSE

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@babel/code-frame/README.md

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@babel/code-frame/lib/index.js

Lines changed: 167 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)