高中生,可以选择更好的学校
热门专业

课程预订

姓名:
年龄:
学历:
电话:

QQ:

校验码:

点图更换

 

IT培训:Java多线程并发管理实例

来源:武汉北大青鸟鲁广校区   发表时间:2014-12-16 09:25

与在线专业老师直接对话交流

  【IT培训:Java多线程并发管理实例】IT培训小知识分享,在软件开发学习中,java方向一直是大家比较关注的知识面,在一技术的运用上也是为广泛之一的知识点。武汉北大青鸟鲁广校区为你介绍关于Java多线程并发管理实例。在书上看到了一个好方法,当多个线程并发时,可以用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次---

  C:\java>java TestGreenhouseScheduler

  Bing!执行了0

  Thermostat to night setting执行了0

  Turning on lights执行了0

  Bing!执行了1

  Bing!执行了2

  Thermostat to night setting执行了1

  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

  Bing!执行了7

  Terminating

  nowww

  更多精彩知识内容等你来分享,武汉北大青鸟鲁广校区的联系电话:027-87808818

  • 武汉北大青鸟鲁广校区-安卓课程介绍
  • 武汉北大青鸟鲁广校区-来校路线
  • 武汉北大青鸟鲁广校区-在线咨询

作者: 武汉北大青鸟 原载: 武汉北大青鸟鲁广校区www.027hpit.com
(版权所有,转载时必须以链接形式注明作者和原始出处及本声明)

相关文章

新闻图片

Copyright 2006-2020 版权所有 武汉宏鹏职业培训学校 鄂ICP备11007753号

鄂公网安备 42011102000420号

CacheTime:2017-06-27 09:40:31 CacheID:6432