12
12
13
13
import java .util .concurrent .CountDownLatch ;
14
14
import java .util .concurrent .Executor ;
15
- import java .util .concurrent .TimeUnit ;
16
15
17
16
/**
18
17
* <pre>
@@ -39,65 +38,39 @@ public void execute(@NonNull Runnable command) {
39
38
40
39
@ Test
41
40
public void test () throws Exception {
42
- ThreadUtils .Task <Void > task = new ThreadUtils .Task <Void >() {
43
- @ Override
44
- public Void doInBackground () throws Throwable {
45
- System .out .println ("haha" );
46
- return null ;
47
- }
48
-
49
- @ Override
50
- public void onSuccess (Void result ) {
51
- System .out .println ("onSuccess" );
52
- }
53
-
54
- @ Override
55
- public void onCancel () {
56
- System .out .println ("onCancel" );
57
- }
58
-
59
- @ Override
60
- public void onFail (Throwable t ) {
61
- System .out .println (t .toString ());
62
- }
63
- };
64
- ThreadUtils .executeBySingleAtFixRate (task , 1 , 1 , TimeUnit .SECONDS );
65
-
66
-
67
41
final CountDownLatch countDownLatch = new CountDownLatch (1 );
68
- //// for (int i = 0; i < 100; i++) {
69
- //// final int finalI = i;
70
- // ThreadUtils.Task<Void> task = new ThreadUtils.Task<Void>() {
71
- //
72
- // @Override
73
- // public Void doInBackground() throws Throwable {
74
- // for (int j = 0; j < 10000; j++) {
75
- // Thread.sleep(100);
76
- // System.out.println(j);
77
- // }
78
- // return null;
79
- // }
80
- //
81
- // @Override
82
- // public void onSuccess(@Nullable Void result) {
83
- // System.out.println(Thread.currentThread().getName());
84
- // }
85
- //
86
- // @Override
87
- // public void onCancel() {
88
- // System.out.println("onCancel");
89
- // }
90
- //
91
- // @Override
92
- // public void onFail(Throwable t) {
93
- // System.out.println(t + "onFail");
94
- // }
95
- // };
96
- // ThreadUtils.executeBySingle(task);
97
- //
98
- //// }
99
- Thread .sleep (100 );
100
- task .cancel ();
42
+ for (int i = 0 ; i < 100 ; i ++) {
43
+ final int finalI = i ;
44
+ ThreadUtils .Task <Void > task = new ThreadUtils .Task <Void >() {
45
+
46
+ @ Override
47
+ public Void doInBackground () throws Throwable {
48
+ for (int j = 0 ; j < 10000 ; j ++) {
49
+ Thread .sleep (100 );
50
+ System .out .println (j );
51
+ }
52
+ return null ;
53
+ }
54
+
55
+ @ Override
56
+ public void onSuccess (Void result ) {
57
+ System .out .println (finalI + Thread .currentThread ().getName ());
58
+ }
59
+
60
+ @ Override
61
+ public void onCancel () {
62
+ System .out .println (finalI + "onCancel" );
63
+ }
64
+
65
+ @ Override
66
+ public void onFail (Throwable t ) {
67
+ System .out .println (finalI + "onFail" + t );
68
+ }
69
+ };
70
+ ThreadUtils .executeBySingle (task );
71
+ Thread .sleep (100 );
72
+ task .cancel ();
73
+ }
101
74
countDownLatch .countDown ();
102
75
countDownLatch .await ();
103
76
@@ -228,8 +201,8 @@ public void onFail(Throwable t) {
228
201
static class Person implements Comparable <Person > {
229
202
230
203
String name ;
231
- int age ;
232
- int time ;
204
+ int age ;
205
+ int time ;
233
206
234
207
public Person (String name ) {
235
208
this .name = name ;
0 commit comments