首页 / JAVA / Java多线程并发管理
Java多线程并发管理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java多线程并发管理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2985字,纯文字阅读大概需要5分钟。
内容图文
![Java多线程并发管理](/upload/InfoBanner/zyjiaocheng/1111/935853f4ad6546e9990c990ba5bd740d.jpg)
在书上看到了一个好方法,当多个线程并发时,可以用scheduleAtFixedRate来管理,scheduleAtFixedRate定时执行一次任务,是重复执行,而ScheduledThreadPoolExecutor将只执行一次任务,
如果你有多个任务,同时进行,并且,是定时的执行,那么以下的程序,完全可以满足你的要求: import java.util.concurrent.*; import java.util.*; public class TestGreenhouseScheduler { private volatile boolean light = false; private volatile boolean water = false; private String thermostat = "Day"; public synchronized String getThermostat() { return thermostat; } public synchronized void setThermostat(String value) { thermostat = value; } //运行一次任务 ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(10); public void schedule(Runnable event, long delay) { scheduler.schedule(event, delay, TimeUnit.MILLISECONDS); } public void repeat(Runnable event, long initialDelay, long period) { //scheduleAtFixedRate 定时执行一次任务,是重复执行 scheduler.scheduleAtFixedRate(event, initialDelay, period, TimeUnit.MILLISECONDS); } class LightOn implements Runnable { 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; } } class LightOff implements Runnable { public void run() { // Put hardware control code here to // physically turn off the light. System.out.println("Turning off lights"); light = false; } } class WaterOn implements Runnable { public void run() { // Put hardware control code here. System.out.println("Turning greenhouse water on"); water = true; } } class WaterOff implements Runnable { public void run() { // Put hardware control code here. System.out.println("Turning greenhouse water off"); water = false; } } class ThermostatNight implements Runnable { int j=0; public void run() { // Put hardware control code here. System.out.println("Thermostat to night setting"+"执行了"+(j++)); setThermostat("Night"); } } class ThermostatDay implements Runnable { public void run() { // Put hardware control code here. System.out.println("Thermostat to day setting"); setThermostat("Day"); } } class Bell implements Runnable { int j= 0; public void run() { System.out.println("Bing!"+"执行了"+(j++)); } } class Terminate implements Runnable { 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 void run() { //最后一次执行 System.out.println("nowww"); } }.start(); } } public static void main(String[] args) { TestGreenhouseScheduler gh = new TestGreenhouseScheduler(); gh.schedule(gh.new Terminate(), 1000*7); //7秒运行一次任务,这里只执行一次 gh.repeat(gh.new Bell(), 0, 1000);//1秒执行一次任务,是重复执行 gh.repeat(gh.new ThermostatNight(), 0, 2000); gh.repeat(gh.new LightOn(), 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 |
原文:http://blog.csdn.net/longhua2014/article/details/41799615
内容总结
以上是互联网集市为您收集整理的Java多线程并发管理全部内容,希望文章能够帮你解决Java多线程并发管理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。