首页 > 代码库 > Java多线程并发管理

Java多线程并发管理

在书上看到了一个好方法,当多个线程并发时,可以用scheduleAtFixedRate来管理,scheduleAtFixedRate定时执行一次任务,是重复执行,而ScheduledThreadPoolExecutor将只执行一次任务,
  如果你有多个任务,同时进行,并且,是定时的执行,那么以下的程序,完全可以满足你的要求:

[1].[代码] [Java]代码 跳至[1]

?
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
import java.util.concurrent.*; import java.util.*;
publicclass TestGreenhouseScheduler {
    privatevolatile booleanlight = false;
    privatevolatile booleanwater = false;
    privateString thermostat = "Day";
    publicsynchronized String getThermostat() {
        returnthermostat;
    }
    publicsynchronized voidsetThermostat(String value) {
        thermostat = value;
    }
     //运行一次任务    ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10);
    publicvoid schedule(Runnable event,long delay) {
        scheduler.schedule(event, delay, TimeUnit.MILLISECONDS);
    }
    publicvoid repeat(Runnable event,long initialDelay,long period) {
        //scheduleAtFixedRate 定时执行一次任务,是重复执行         scheduler.scheduleAtFixedRate(event, initialDelay, period,
               TimeUnit.MILLISECONDS);
    }
    classLightOn implementsRunnable {
        int j=0;
        public void run() {
            // Put hardware control code here to            // physically turn on the light.            
            System.out.println("Turning on lights"+"执行了"+(j++));
            light = true;
        }
    }
    classLightOff implementsRunnable {
        public void run() {
            // Put hardware control code here to            // physically turn off the light.             System.out.println("Turning off lights");
            light = false;
        }
    }
    classWaterOn implementsRunnable {
        public void run() {
            // Put hardware control code here.            System.out.println("Turning greenhouse water on");
            water = true;
        }
    }
    classWaterOff implementsRunnable {
        public void run() {
            // Put hardware control code here.            System.out.println("Turning greenhouse water off");
            water = false;
        }
    }
    classThermostatNight implementsRunnable {
        int j=0;
        public void run() {
        
            // Put hardware control code here.            System.out.println("Thermostat to night setting"+"执行了"+(j++));
            setThermostat("Night");
        }
    }
    classThermostatDay implementsRunnable {
        public void run() {
            // Put hardware control code here.            System.out.println("Thermostat to day setting");
            setThermostat("Day");
        }
    }
    classBell implementsRunnable {
        int j= 0;
        public void run() {
            
            System.out.println("Bing!"+"执行了"+(j++));
        }
    }
    classTerminate implementsRunnable {
        public void run() {
            System.out.println("Terminating");
            scheduler.shutdownNow(); //结束线程            // Must start a separate task to do this job,            // since the scheduler has been shut down:            new Thread() {
               public voidrun() {
                        //最后一次执行                         System.out.println("nowww");
               }
            }.start();
        }
    }
    
    
    
    
    publicstatic void main(String[] args) {
        TestGreenhouseScheduler gh = newTestGreenhouseScheduler();
        gh.schedule(gh.newTerminate(), 1000*7);//7秒运行一次任务,这里只执行一次
        gh.repeat(gh.newBell(), 0,1000);//1秒执行一次任务,是重复执行        gh.repeat(gh.newThermostatNight(), 0,2000);
        gh.repeat(gh.newLightOn(), 0,3000);
        
        
    
        
    }
 
---结果7秒之后,结束所有的线程,你看Bing执行了7次---
Bing!执行了0
Thermostat to night setting执行了0
Turning on lights执行了0
Bing!执行了1
Thermostat to night setting执行了1
Bing!执行了2
Bing!执行了3
Turning on lights执行了1
Bing!执行了4
Thermostat to night setting执行了2
Bing!执行了5
Bing!执行了6
Thermostat to night setting执行了3
Turning on lights执行了2
Terminating
Bing!执行了7
nowww

Java多线程并发管理