Skip to content

Commit 76b6ddc

Browse files
Faqiang LvFaqiang Lv
authored andcommitted
updateCoe
1 parent 67851e6 commit 76b6ddc

File tree

6 files changed

+209
-1
lines changed

6 files changed

+209
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
用于Android 日常代码测试,效果整理
33

44
### Update Log:
5+
- 2018年08月27日
6+
7+
[Kotlin 协程+Channel 控制线程切换](https://github.com/lvfaqiang/AndroidTestCode/blob/master/app/src/main/java/com/lvfq/code/coroutines/thread_toggle/CoroutineToggleActivity.kt)
58
- 2018年08月24日
69

710
[Kotlin Channel](https://github.com/lvfaqiang/AndroidTestCode/blob/master/app/src/main/java/com/lvfq/code/coroutines/eventbus)

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
android:supportsRtl="true"
2020
android:theme="@style/AppTheme">
2121

22-
<activity android:name=".coroutines.eventbus.CoroutinesMainActivity">
22+
<activity android:name=".coroutines.thread_toggle.CoroutineToggleActivity">
2323
<intent-filter>
2424
<action android:name="android.intent.action.MAIN" />
2525

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.lvfq.code.coroutines.thread_toggle;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
import kotlin.Unit;
6+
import kotlin.coroutines.experimental.Continuation;
7+
import kotlin.coroutines.experimental.CoroutineContext;
8+
9+
/**
10+
* CoroutineJava
11+
*
12+
* @author FaQiang on 2018/8/27 上午9:57
13+
* @Github: <a href="https://github.com/lvfaqiang"/>
14+
* @Blog: <a href="http://blog.csdn.net/lv_fq"/>
15+
* @desc :
16+
*/
17+
public class CoroutineJava {
18+
19+
public static void main(String[] args) {
20+
test();
21+
CoroutineUtils.start();
22+
System.out.println("main Function");
23+
}
24+
25+
26+
public static void test() {
27+
async(new Task() {
28+
@Override
29+
public void run() {
30+
try {
31+
Thread.sleep(1000);
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
int i = "string".hashCode();
36+
ui(new UI() {
37+
@Override
38+
public void run() {
39+
System.out.println("------- UI - " + Thread.currentThread().getName());
40+
}
41+
});
42+
}
43+
});
44+
}
45+
46+
public static void async(Task task) {
47+
CoroutineUtils.async(task, new Continuation<Unit>() {
48+
@NotNull
49+
@Override
50+
public CoroutineContext getContext() {
51+
return null;
52+
}
53+
54+
@Override
55+
public void resume(Unit unit) {
56+
57+
}
58+
59+
@Override
60+
public void resumeWithException(Throwable throwable) {
61+
62+
}
63+
});
64+
}
65+
66+
public static void ui(UI task) {
67+
CoroutineUtils.ui(task, new Continuation<Unit>() {
68+
@NotNull
69+
@Override
70+
public CoroutineContext getContext() {
71+
return null;
72+
}
73+
74+
@Override
75+
public void resume(Unit unit) {
76+
77+
}
78+
79+
@Override
80+
public void resumeWithException(Throwable throwable) {
81+
82+
}
83+
});
84+
}
85+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.lvfq.code.coroutines.thread_toggle
2+
3+
import android.os.Bundle
4+
import android.support.v7.app.AppCompatActivity
5+
import android.view.View
6+
import com.lvfq.code.R
7+
import com.lvfq.library.utils.LvLog
8+
9+
/**
10+
* CoroutineToggleActivity
11+
* @author FaQiang on 2018/8/27 上午10:18
12+
* @Github: <a href="https://github.com/lvfaqiang"/>
13+
* @Blog: <a href="http://blog.csdn.net/lv_fq"/>
14+
* @desc :
15+
*
16+
*/
17+
class CoroutineToggleActivity : AppCompatActivity() {
18+
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState)
21+
setContentView(R.layout.activity_coroutine_toggle)
22+
start()
23+
LvLog.i("lfq", "onCreate====================")
24+
}
25+
26+
fun button1(v: View) {
27+
CoroutineJava.async(object : Task() {
28+
override fun run() {
29+
Thread.sleep(1000)
30+
LvLog.i("lfq", "task==========${Thread.currentThread().name}")
31+
}
32+
})
33+
}
34+
35+
fun button2(v: View) {
36+
CoroutineJava.ui(object : UI() {
37+
override fun run() {
38+
LvLog.i("lfq", "UI==========${Thread.currentThread().name}")
39+
}
40+
})
41+
}
42+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
@file:JvmName("CoroutineUtils")
2+
3+
package com.lvfq.code.coroutines.thread_toggle
4+
5+
import android.os.AsyncTask
6+
import kotlinx.coroutines.experimental.CoroutineDispatcher
7+
import kotlinx.coroutines.experimental.android.UI
8+
import kotlinx.coroutines.experimental.channels.Channel
9+
import kotlinx.coroutines.experimental.launch
10+
import kotlin.coroutines.experimental.CoroutineContext
11+
12+
/**
13+
* CoroutineUtils
14+
* @author FaQiang on 2018/8/27 上午9:12
15+
* @Github: <a href="https://github.com/lvfaqiang"/>
16+
* @Blog: <a href="http://blog.csdn.net/lv_fq"/>
17+
* @desc :
18+
*
19+
*/
20+
val channel = Channel<Runnable>(2)
21+
22+
abstract class Task : Runnable
23+
24+
abstract class UI : Runnable
25+
26+
suspend fun async(task: Task) {
27+
channel.send(task)
28+
}
29+
30+
suspend fun ui(task: UI) {
31+
channel.send(task)
32+
}
33+
34+
fun start() {
35+
launch(AndroidCommonPool) {
36+
while (true) {
37+
val t = channel.receive()
38+
when (t) {
39+
is UI -> {
40+
launch(UI) {
41+
t.run()
42+
}
43+
}
44+
is Task -> {
45+
t.run()
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
object AndroidCommonPool : CoroutineDispatcher() {
53+
override fun dispatch(context: CoroutineContext, block: Runnable) {
54+
AsyncTask.THREAD_POOL_EXECUTOR.execute(block)
55+
}
56+
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@+id/root_layout"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:orientation="vertical">
7+
8+
<Button
9+
android:id="@+id/button"
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
12+
android:onClick="button1"
13+
android:text="Async" />
14+
15+
<Button
16+
android:id="@+id/button2"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:onClick="button2"
20+
android:text="UI" />
21+
</LinearLayout>

0 commit comments

Comments
 (0)