forked from yjfnypeu/EasyThread
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
11 changed files
with
343 additions
and
119 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
easythread/src/main/java/com/lzh/easythread/AndroidMainExecutor.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,31 @@ | ||
package com.lzh.easythread; | ||
|
||
import android.os.Handler; | ||
import android.os.Looper; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
final class AndroidMainExecutor implements Executor { | ||
|
||
private static AndroidMainExecutor instance = new AndroidMainExecutor(); | ||
private Handler main = new Handler(Looper.getMainLooper()); | ||
|
||
static AndroidMainExecutor getInstance() { | ||
return instance; | ||
} | ||
|
||
@Override | ||
public void execute(final Runnable runnable) { | ||
if (Looper.myLooper() == Looper.getMainLooper()) { | ||
runnable.run(); | ||
return; | ||
} | ||
|
||
main.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
runnable.run(); | ||
} | ||
}); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
easythread/src/main/java/com/lzh/easythread/AsyncCallback.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,10 @@ | ||
package com.lzh.easythread; | ||
|
||
/** | ||
* Async callback class. | ||
* @author haoge on 2018/2/9. | ||
*/ | ||
public interface AsyncCallback<T> { | ||
void onSuccess(T t); | ||
void onFailed(Throwable t); | ||
} |
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
83 changes: 83 additions & 0 deletions
83
easythread/src/main/java/com/lzh/easythread/CallbackDelegate.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,83 @@ | ||
package com.lzh.easythread; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* The callback delegate class. | ||
* | ||
* @author haoge on 2018/2/9. | ||
*/ | ||
final class CallbackDelegate implements Callback, AsyncCallback { | ||
|
||
private Callback callback; | ||
private AsyncCallback async; | ||
private Executor deliver; | ||
|
||
CallbackDelegate(Callback callback, Executor deliver, AsyncCallback async) { | ||
this.callback = callback; | ||
this.deliver = deliver; | ||
this.async = async; | ||
} | ||
|
||
@Override | ||
public void onSuccess(final Object o) { | ||
if (async == null) return; | ||
deliver.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
//noinspection unchecked | ||
async.onSuccess(o); | ||
} catch (Throwable t) { | ||
onFailed(t); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onFailed(final Throwable t) { | ||
if (async == null) return; | ||
deliver.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
async.onFailed(t); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onError(final Thread thread, final Throwable t) { | ||
onFailed(t); | ||
|
||
if (callback == null) return; | ||
deliver.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
callback.onError(thread, t); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onCompleted(final Thread thread) { | ||
if (callback == null) return; | ||
deliver.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
callback.onCompleted(thread); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onStart(final Thread thread) { | ||
if (callback == null) return; | ||
deliver.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
callback.onStart(thread); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.