Skip to content

Commit

Permalink
完善代码案例
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchong211 committed Dec 21, 2021
1 parent 9838dcd commit e5ae801
Show file tree
Hide file tree
Showing 42 changed files with 723 additions and 175 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
30 changes: 30 additions & 0 deletions ThreadPoolLib/build.gradle
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.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package cn.ycbjie.ycthreadpoollib;


import android.support.annotation.NonNull;

import androidx.annotation.NonNull;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.ycbjie.ycthreadpoollib;

import android.support.annotation.NonNull;

import androidx.annotation.NonNull;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import androidx.annotation.Nullable;

import java.util.concurrent.Executor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package cn.ycbjie.ycthreadpoollib.deliver;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import androidx.annotation.Nullable;

import java.util.concurrent.Executor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package cn.ycbjie.ycthreadpoollib.factory;

import android.support.annotation.NonNull;

import androidx.annotation.NonNull;

import java.util.concurrent.ThreadFactory;

Expand Down
1 change: 1 addition & 0 deletions ThreadTaskLib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
25 changes: 25 additions & 0 deletions ThreadTaskLib/build.gradle
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'
}
21 changes: 21 additions & 0 deletions ThreadTaskLib/proguard-rules.pro
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
2 changes: 2 additions & 0 deletions ThreadTaskLib/src/main/AndroidManifest.xml
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 ThreadTaskLib/src/main/java/com/yc/taskscheduler/AbsTaskRunnable.java
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);
}
}
});
}
}

}
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);
}
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 ThreadTaskLib/src/main/java/com/yc/taskscheduler/SafeHandler.java
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);
}
}
}
Loading

0 comments on commit e5ae801

Please sign in to comment.