-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9838dcd
commit e5ae801
Showing
42 changed files
with
723 additions
and
175 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'com.github.dcendents.android-maven' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
buildToolsVersion '29.0.0' | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
targetSdkVersion 29 | ||
versionCode 13 | ||
versionName "1.3.3" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
} | ||
|
||
dependencies { | ||
implementation fileTree(include: ['*.jar'], dir: 'libs') | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'androidx.annotation:annotation:1.1.0' | ||
} | ||
|
||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
.../ycbjie/ycthreadpoollib/ScheduleTask.java → .../ycbjie/ycthreadpoollib/ScheduleTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
apply plugin: 'com.android.library' | ||
apply plugin: 'com.github.dcendents.android-maven' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
|
||
defaultConfig { | ||
minSdkVersion 16 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(include: ['*.jar'], dir: 'libs') | ||
implementation 'androidx.appcompat:appcompat:1.2.0' | ||
implementation 'androidx.annotation:annotation:1.1.0' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.yc.taskscheduler"/> |
111 changes: 111 additions & 0 deletions
111
ThreadTaskLib/src/main/java/com/yc/taskscheduler/AbsTaskRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.yc.taskscheduler; | ||
|
||
import android.util.Log; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
|
||
public abstract class AbsTaskRunnable<R> implements Runnable { | ||
|
||
private static final String TAG = "Task"; | ||
private AtomicBoolean mCanceledAtomic = new AtomicBoolean(false); | ||
private AtomicReference<Thread> mTaskThread = new AtomicReference<>(); | ||
|
||
|
||
/** | ||
* 异步线程处理任务,在非主线程执行 | ||
* @return 处理后的结果 | ||
* @throws InterruptedException 获取InterruptedException异常,来判断任务是否被取消 | ||
*/ | ||
public abstract R doInBackground() throws InterruptedException; | ||
|
||
/** | ||
* 异步线程处理后返回的结果,在主线程执行 | ||
* @param result 结果 | ||
*/ | ||
public abstract void onSuccess(R result); | ||
|
||
/** | ||
* 异步线程处理出现异常的回调,按需处理,未置成抽象,主线程执行 | ||
* @param throwable 异常 | ||
*/ | ||
public void onFail(Throwable throwable){ | ||
|
||
} | ||
|
||
/** | ||
* 任务被取消的回调,主线程执行 | ||
* | ||
*/ | ||
public void onCancel(){ | ||
|
||
} | ||
|
||
/** | ||
* 将任务标记为取消,没法真正取消正在执行的任务,只是结果不在onSuccess里回调 | ||
* cancel 不一定能让任务停止,和AsyncTask同样道理,可参考 | ||
* {#link http://silencedut.com/2016/07/08/%E5%9F%BA%E4%BA%8E%E6%9C%80%E6%96%B0%E7%89%88%E6%9C%AC%E7%9A%84AsyncTask%E6%BA%90%E7%A0%81%E8%A7%A3%E8%AF%BB%E5%8F%8AAsyncTask%E7%9A%84%E9%BB%91%E6%9A%97%E9%9D%A2/} | ||
**/ | ||
|
||
void cancel() { | ||
|
||
this.mCanceledAtomic.set(true); | ||
|
||
Thread t = mTaskThread.get(); | ||
if(t!=null) { | ||
Log.d(TAG,"Task cancel: "+t.getName()); | ||
t.interrupt(); | ||
} | ||
|
||
TaskScheduler.runOnUIThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
onCancel(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* 任务是已取消 | ||
* @return 任务是否已被取消 | ||
*/ | ||
public boolean isCanceled() { | ||
|
||
return mCanceledAtomic.get(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
|
||
Log.d(TAG,"Task : "+Thread.currentThread().getName()); | ||
mTaskThread.compareAndSet(null,Thread.currentThread()); | ||
|
||
mCanceledAtomic.set(false); | ||
|
||
final R result = doInBackground(); | ||
|
||
TaskScheduler.runOnUIThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
if(!isCanceled()){ | ||
onSuccess(result); | ||
} | ||
} | ||
}); | ||
} catch (final Throwable throwable) { | ||
|
||
Log.e(TAG,"handle background Task error " +throwable); | ||
TaskScheduler.runOnUIThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
if(!isCanceled()){ | ||
onFail(throwable); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
ThreadTaskLib/src/main/java/com/yc/taskscheduler/InterLog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.yc.taskscheduler; | ||
|
||
|
||
public interface InterLog { | ||
|
||
void info(String info); | ||
void error(String error); | ||
} |
60 changes: 60 additions & 0 deletions
60
ThreadTaskLib/src/main/java/com/yc/taskscheduler/LifecycleRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.yc.taskscheduler; | ||
|
||
|
||
import android.os.Handler; | ||
|
||
import androidx.lifecycle.GenericLifecycleObserver; | ||
import androidx.lifecycle.Lifecycle; | ||
import androidx.lifecycle.LifecycleOwner; | ||
|
||
|
||
public class LifecycleRunnable implements Runnable { | ||
|
||
private Runnable mOriginRunnable; | ||
private LifecycleOwner mLifecycleOwner; | ||
private GenericLifecycleObserver mLifecycleObserver; | ||
|
||
|
||
LifecycleRunnable(LifecycleOwner lifecycleOwner, final Handler handler, | ||
final Lifecycle.Event targetEvent, | ||
final Runnable originRunnable) { | ||
if(originRunnable == null || lifecycleOwner == null) { | ||
return; | ||
} | ||
this.mLifecycleOwner = lifecycleOwner; | ||
this.mOriginRunnable = originRunnable; | ||
mLifecycleObserver = new GenericLifecycleObserver() { | ||
@Override | ||
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { | ||
|
||
if(event == targetEvent) { | ||
if(mLifecycleOwner!=null ) { | ||
mLifecycleOwner.getLifecycle().removeObserver(this); | ||
} | ||
handler.removeCallbacks(LifecycleRunnable.this); | ||
} | ||
} | ||
}; | ||
if(TaskScheduler.isMainThread()) { | ||
mLifecycleOwner.getLifecycle().addObserver(mLifecycleObserver); | ||
}else { | ||
TaskScheduler.runOnUIThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
mLifecycleOwner.getLifecycle().addObserver(mLifecycleObserver); | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
|
||
@Override | ||
public void run() { | ||
if(mOriginRunnable!=null && mLifecycleOwner!=null) { | ||
mOriginRunnable.run(); | ||
mLifecycleOwner.getLifecycle().removeObserver(mLifecycleObserver); | ||
} | ||
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
ThreadTaskLib/src/main/java/com/yc/taskscheduler/SafeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.yc.taskscheduler; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
import android.os.Message; | ||
import android.util.Log; | ||
|
||
|
||
public class SafeHandler extends Handler { | ||
|
||
private static final String TAG = "SafeSchedulerHandler"; | ||
|
||
SafeHandler(Looper looper) { | ||
super(looper); | ||
} | ||
|
||
SafeHandler() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public void dispatchMessage(Message msg) { | ||
if (msg == null){ | ||
Log.d(TAG, "msg is null , return"); | ||
return; | ||
} | ||
try { | ||
super.dispatchMessage(msg); | ||
} catch (Exception e) { | ||
Log.d(TAG, "dispatchMessage Exception " + msg + " , " + e); | ||
} catch (Error error) { | ||
Log.d(TAG, "dispatchMessage error " + msg + " , " + error); | ||
} | ||
} | ||
} |
Oops, something went wrong.