Helius
2021-07-15 bea4cd8cceb220189fd1fed5c2c1ff22d3e8a9ed
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import cn.hutool.core.lang.Console;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import org.junit.Test;
 
import java.util.concurrent.*;
 
public class ThreadTest {
 
    @Test
    public  void test1() throws InterruptedException, ExecutionException, TimeoutException {
 
        CompletionService<Object> objectCompletionService = ThreadUtil.newCompletionService();
 
        CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(4);
 
        objectCompletionService.submit(new Callable<Object>() {
 
            @Override
            public Object call() throws Exception {
                System.out.println("第一个任务");
                try {
                    return 1;
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
 
        objectCompletionService.submit(new Callable<Object>() {
 
            @Override
            public Object call() throws Exception {
                System.out.println("第二个任务");
//                ThreadUtil.sleep(3000);
                try {
                    return 2;
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
 
        objectCompletionService.submit(new Callable<Object>() {
 
            @Override
            public Object call() throws Exception {
                System.out.println("第三个任务");
                try {
                    return 3;
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
 
        objectCompletionService.submit(new Callable<Object>() {
 
            @Override
            public Object call() throws Exception {
//                int a=5/0;
//                ThreadUtil.sleep(11000);
//                ThreadUtil.sleep(3000);
                System.out.println("第四个任务");
                try {
                    return 4;
                } finally {
                    countDownLatch.countDown();
                }
            }
        });
 
        long count = countDownLatch.getCount();
        Console.log("latch:{}",count);
 
        countDownLatch.await();
 
        for (int i = 0; i < 4; i++) {
            Object  o= objectCompletionService.take().get();
            System.out.println(o);
        }
 
        ThreadLocal<Object> threadLocal = ThreadUtil.createThreadLocal(false);
 
        threadLocal.set("123");
 
        Object o = threadLocal.get();
 
        System.out.println(o);
 
 
    }
 
    class A implements  Runnable{
 
        @Override
        public void run() {
            System.out.println("开始");
        }
    }
}