liurunyu
2024-04-15 55d5046ae0a9da1109e402d7d719f765a4bccdae
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.dy.common.threadPool;
 
 
public class Test {
 
//
//    public static void main(String args[])throws Exception {
////        Test t = new Test();
////        t.testShort1();
////        t.testShort2();
////        t.testLong();
//    }
 
    /**
     * 测试增加线程线程  
     * @throws Exception 
     *
     */
    @SuppressWarnings("unused")
    private void testShort1() throws Exception {
        TreadPoolFactory.initThreadPoolShort("测试线程组",    2, 1, 10, 20);
        ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolShort() ;
        try {
            int n = 1 ;
            Thread.sleep(1000);
            while (n < 4) {
                System.out.println("加入短工作任务(" + n + ")");
                pool.putJob(new ThreadPool.Job() {
                    public void execute() {
                        System.out.println("!!!!!开始短工作" + this.hashCode());
                        try {
                            Thread.sleep(2000);
                        } catch (Exception e) {
                            ;
                        }
                        System.out.println("!!!!!结束短工作" + this.hashCode());
                    }
                    @Override
                    public void destroy(){
                    }
                    @Override
                    public boolean isDestroy(){
                        return false ;
                    }
                });
                n++ ;
                Thread.sleep(100);
            }
        } catch (Exception e) {
 
        }
    }
    /**
     * 测试增加线程线程  
     * @throws Exception 
     *
     */
    @SuppressWarnings("unused")
    private void testShort2() throws Exception {
        TreadPoolFactory.initThreadPoolShort("测试短工作线程组", 2, 1, 5, 6);
        ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolShort() ;
        try {
            int n = 1 ;
            Thread.sleep(1000);
            while (n < 2) {
                System.out.println("加入短工作任务(" + n + ")");
                pool.putJob(new ThreadPool.Job() {
                    @Override
                    public void execute() {
                        Long times = 0L ;
                        System.out.println("!!!!!开始短工作" + this.hashCode());
                        while(true){
                            if(this.isDestroy == null || !this.isDestroy.booleanValue()){
                                times++ ;
                                System.out.println("!!!!!执行短工作" + this.hashCode() + " 次数:" + times);
                                try {
                                    Thread.sleep(2000);
                                } catch (Exception e) {
                                    ;
                                }
//                                System.out.println("!!!!!结束长工作" + this.hashCode());
                            }
                        }
                    }
                    @Override
                    public void destroy(){
                        this.isDestroy = true ;
                    }
                    @Override
                    public boolean isDestroy(){
                        if(this.isDestroy == null){
                            return false ;
                        }
                        return this.isDestroy.booleanValue() ;
                    }
                    private Boolean isDestroy;
                });
                n++ ;
                Thread.sleep(100);
            }
        } catch (Exception e) {
 
        }
    }/**
     * 测试增加线程线程  
     * @throws Exception 
     *
     */
    @SuppressWarnings("unused")
    private void testLong() throws Exception {
        TreadPoolFactory.initThreadPoolLong("测试长工作线程组", -1, 1, 5, -1);
        ThreadPool.Pool pool = TreadPoolFactory.getThreadPoolLong() ;
        try {
            int n = 1 ;
            Thread.sleep(1000);
            while (n < 2) {
                System.out.println("加入长工作任务(" + n + ")");
                pool.putJob(new ThreadPool.Job() {
                    @Override
                    public void execute() {
                        Long times = 0L ;
                        System.out.println("!!!!!开始长工作" + this.hashCode());
                        while(true){
                            if(this.isDestroy == null || !this.isDestroy.booleanValue()){
                                times++ ;
                                System.out.println("!!!!!执行长工作" + this.hashCode() + " 次数:" + times);
                                try {
                                    Thread.sleep(2000);
                                } catch (Exception e) {
                                    ;
                                }
//                                System.out.println("!!!!!结束长工作" + this.hashCode());
                            }
                        }
                    }
                    @Override
                    public void destroy(){
                        this.isDestroy = true ;
                    }
                    @Override
                    public boolean isDestroy(){
                        if(this.isDestroy == null){
                            return false ;
                        }
                        return this.isDestroy.booleanValue() ;
                    }
                    private Boolean isDestroy;
                });
                n++ ;
                Thread.sleep(100);
            }
        } catch (Exception e) {
 
        }
    }
 
}