Skip to content

Commit

Permalink
add onStart method on Callback
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Apr 20, 2017
1 parent 633002e commit 550840b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# EasyThread
<a href="http://www.methodscount.com/?lib=com.github.yjfnypeu%3AEasyThread%3A0.1"><img src="https://img.shields.io/badge/Methods count-61-e91e63.svg"/></a>

一款简单易用的线程池管理器
一款简单超轻量级的线程池管理器

- 为什么会需要此


### 依赖

Expand Down Expand Up @@ -49,5 +52,10 @@ ThreadCallback implements Callback {
public void onCompleted(Thread thread) {
// 当使用EasyThread启动后台任务后,若子线程运行完毕。将会回调到此方法中通知用户
}
@Override
public void onStart(Thread thread) {
// 当子线程启动运行时,回调到此方法通知用户。
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ final class CallableWrapper<T> implements Callable<T> {

@Override
public T call() throws Exception {
if (callback != null) {
callback.onStart(Thread.currentThread());
}
Tools.resetThread(Thread.currentThread(),name,callback);
T t = proxy.call();
if (callback != null) {
Expand Down
6 changes: 6 additions & 0 deletions easythread/src/main/java/com/lzh/easythread/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ public interface Callback {
* @param thread The running thread
*/
void onCompleted (Thread thread);

/**
* notify user that task start running
* @param thread The running thread
*/
void onStart (Thread thread);
}
12 changes: 12 additions & 0 deletions easythread/src/main/java/com/lzh/easythread/EasyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public void run() {
}
});
}

@Override
public void onStart(final Thread thread) {
main.post(new Runnable() {
@Override
public void run() {
if (delegate != null) {
delegate.onStart(thread);
}
}
});
}
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ final class RunnableWrapper implements Runnable {

@Override
public void run() {
if (callback != null) {
callback.onStart(Thread.currentThread());
}
Tools.resetThread(Thread.currentThread(),name,callback);
proxy.run();
if (callback != null) {
Expand Down
6 changes: 6 additions & 0 deletions javatest/src/main/java/com/example/EasyThreadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public void onCompleted(Thread thread) {
System.out.println("thread:" + Thread.currentThread());
System.out.println("线程完成");
}

@Override
public void onStart(Thread thread) {
System.out.println("thread:" + Thread.currentThread());
System.out.println("线程启动");
}
}).build();
easyThread.execute(new Runnable() {
@Override
Expand Down

0 comments on commit 550840b

Please sign in to comment.