java多线程编程练习_这是JAVA多线程编程练习的两道题目!!!求高手解决!!!拜托了!!!...

展开全部

package com.kevin2014.thread;

import java.util.Random;

public class AtmosphereTest {

public static void main(String[] args) {

636f70793231313335323631343130323136353331333335313262Atmosphere atmosphere = new Atmosphere();

AtmosphereSensor atmosphereSensor = new AtmosphereSensor(atmosphere);

Computer computer = new Computer(atmosphereSensor);

new Thread(atmosphereSensor).start();

new Thread(computer).start();

}

}

/**

* 传感器 1.5秒/次 读取大气信息

*

* @author 795829

*

*/

class AtmosphereSensor implements Runnable {

private Atmosphere atmosphere;

public AtmosphereSensor(Atmosphere atmosphere) {

this.atmosphere = atmosphere;

}

public Atmosphere getAtmosphere() {

return atmosphere;

}

public void setAtmosphere(Atmosphere atmosphere) {

this.atmosphere = atmosphere;

}

@Override

public void run() {

try {

int i = 1;

while (true) {

Thread.sleep(500);

atmosphere.setHumidity(new Random(System.currentTimeMillis()).nextFloat());

Thread.sleep(500);

atmosphere.setTemperature(new Random(System.currentTimeMillis()).nextFloat());

Thread.sleep(500);

atmosphere.setWind(new Random(System.currentTimeMillis()).nextFloat());

System.out.println("----- " + i++ + " 传感器数据    温度 = " + atmosphere.getTemperature() + ", 湿度 = "

+ atmosphere.getHumidity() + ", 风速 = " + atmosphere.getWind());

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

* 计算机 2秒/次 读取传感器数据

*

* @author 795829

*

*/

class Computer implements Runnable {

private AtmosphereSensor atmosphereSensor;

public Computer(AtmosphereSensor atmosphereSensor) {

this.atmosphereSensor = atmosphereSensor;

}

public AtmosphereSensor getAtmosphereSensor() {

return atmosphereSensor;

}

public void setAtmosphereSensor(AtmosphereSensor atmosphereSensor) {

this.atmosphereSensor = atmosphereSensor;

}

@Override

public void run() {

try {

for (int i = 1; i <= 50; i++) {

Thread.sleep(2000);

System.out.println(i + " 计算机数据    温度 = " + atmosphereSensor.getAtmosphere().getTemperature()

+ ", 湿度 = " + atmosphereSensor.getAtmosphere().getHumidity() + ", 风速 = "

+ atmosphereSensor.getAtmosphere().getWind());

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

class Atmosphere {

private float temperature;

private float humidity;

private float wind;

public float getTemperature() {

return temperature;

}

public void setTemperature(float temperature) {

this.temperature = temperature;

}

public float getHumidity() {

return humidity;

}

public void setHumidity(float humidity) {

this.humidity = humidity;

}

public float getWind() {

return wind;

}

public void setWind(float wind) {

this.wind = wind;

}

}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI前线

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值