1212
1313import java .util .concurrent .CountDownLatch ;
1414import java .util .concurrent .Executor ;
15- import java .util .concurrent .TimeUnit ;
1615
1716/**
1817 * <pre>
@@ -39,65 +38,39 @@ public void execute(@NonNull Runnable command) {
3938
4039 @ Test
4140 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-
6741 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+ }
10174 countDownLatch .countDown ();
10275 countDownLatch .await ();
10376
@@ -228,8 +201,8 @@ public void onFail(Throwable t) {
228201 static class Person implements Comparable <Person > {
229202
230203 String name ;
231- int age ;
232- int time ;
204+ int age ;
205+ int time ;
233206
234207 public Person (String name ) {
235208 this .name = name ;
0 commit comments