23
23
* author: Blankj
24
24
* blog : http://blankj.com
25
25
* time : 2018/05/08
26
- * desc :
26
+ * desc : utils about thread
27
27
* </pre>
28
28
*/
29
29
public final class ThreadUtils {
@@ -280,11 +280,14 @@ public static <T> void executeByCustomAtFixRate(final ExecutorService pool,
280
280
executeAtFixedRate (pool , task , initialDelay , delay , unit );
281
281
}
282
282
283
-
284
283
public static void cancel (final Task task ) {
285
284
task .cancel ();
286
285
}
287
286
287
+ public static boolean isMainThread () {
288
+ return Looper .myLooper () == Looper .getMainLooper ();
289
+ }
290
+
288
291
private static <T > void execute (final ExecutorService pool , final Task <T > task ) {
289
292
executeWithDelay (pool , task , 0 , TimeUnit .MILLISECONDS );
290
293
}
@@ -424,9 +427,10 @@ public abstract static class Task<T> implements Runnable {
424
427
private boolean isSchedule ;
425
428
426
429
private volatile int state ;
427
- private static final int NEW = 0 ;
428
- private static final int COMPLETING = 1 ;
429
- private static final int CANCELLED = 2 ;
430
+ private static final int NEW = 0 ;
431
+ private static final int COMPLETING = 1 ;
432
+ private static final int CANCELLED = 2 ;
433
+ private static final int EXCEPTIONAL = 3 ;
430
434
431
435
public Task () {
432
436
state = NEW ;
@@ -445,7 +449,6 @@ public Task() {
445
449
public void run () {
446
450
try {
447
451
final T result = doInBackground ();
448
-
449
452
if (state != NEW ) return ;
450
453
451
454
if (isSchedule ) {
@@ -468,6 +471,7 @@ public void run() {
468
471
} catch (final Throwable throwable ) {
469
472
if (state != NEW ) return ;
470
473
474
+ state = EXCEPTIONAL ;
471
475
Deliver .post (new Runnable () {
472
476
@ Override
473
477
public void run () {
0 commit comments