forked from OnlyTerminator/GeekThread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeekTask.java
More file actions
71 lines (58 loc) · 1.96 KB
/
Copy pathGeekTask.java
File metadata and controls
71 lines (58 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.geek.thread.task;
import android.util.Log;
import com.geek.thread.GeekThreadManager;
import com.geek.thread.ThreadPriority;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Created by aotuman on 2017/11/23.
* 用来执行和存储线程的Task
*/
public class GeekTask<T> extends FutureTask<T> implements GeekPriorityComparable{
private static final String TAG = "GeekTask";
private ThreadPriority mPriority;
private int mKey = -1;
public GeekTask(Runnable r, T result, ThreadPriority priority) {
super(r, result);
this.mPriority = priority;
}
public void setKey(int key) {
if (key >= 0) {
this.mKey = key;
}
}
@Override
public void setGeekPriority(ThreadPriority priority) {
this.mPriority = priority;
}
@Override
public ThreadPriority getGeekPriority() {
return mPriority;
}
@Override
public int compareTo(GeekPriorityComparable geekPriorityComparable) {
if (null == geekPriorityComparable) {
return 1;
}
return getGeekPriority().getPriorityValue() - geekPriorityComparable.getGeekPriority().getPriorityValue();
}
@Override
public boolean equals(Object obj) {
return null != obj && obj instanceof GeekPriorityComparable && ((GeekPriorityComparable) obj).getGeekPriority() == getGeekPriority() && super.equals(obj);
}
@Override
protected void done() {
try {
get();
} catch (InterruptedException | CancellationException e) {
Log.e(TAG, "done: ", e);
} catch (ExecutionException e) {
throw new RuntimeException("An error occurred while executing MJFutureTask",
e.getCause());
} finally {
GeekThreadManager.getInstance().removeWork(mKey);
}
}
}