• java 使用多线程模拟 大气测试数据传感器 和 计算机显示数据


    1. package week8.day2;
    2. class AirData{
    3. private int temperature;//温度
    4. private int humidity;//湿度
    5. private int windSpeed;//风速
    6. AirData(){temperature=0;humidity=0;windSpeed=0;}
    7. public void setTemperature(int temperature) {
    8. this.temperature = temperature;
    9. }
    10. public void setHumidity(int humidity) {
    11. this.humidity = humidity;
    12. }
    13. public void setWindSpeed(int windSpeed) {
    14. this.windSpeed = windSpeed;
    15. }
    16. public int getTemperature() {
    17. return temperature;
    18. }
    19. public int getHumidity() {
    20. return humidity;
    21. }
    22. public int getWindSpeed() {
    23. return windSpeed;
    24. }
    25. @Override
    26. public String toString() {
    27. return "温度:"+ this.getTemperature()+",湿度:" + this.getHumidity()+",风速:"+ this.getWindSpeed();
    28. }
    29. }
    30. class TestingSensor extends Thread{
    31. static int getValue(){
    32. return (int)(Math.random()*100);
    33. }
    34. private AirData airData ;
    35. public TestingSensor (AirData airData){
    36. this.airData=airData;
    37. }
    38. public void run() {
    39. while(true){
    40. synchronized (airData){
    41. //产生三个数据
    42. airData.setTemperature(getValue());
    43. airData.setHumidity(getValue());
    44. airData.setWindSpeed(getValue());
    45. System.out.println("传感器测试到的数据是 "+ airData);
    46. airData.notify();
    47. }
    48. try {
    49. Thread.sleep(5000);
    50. } catch (InterruptedException e) {
    51. throw new RuntimeException(e);
    52. }
    53. }
    54. }
    55. }
    56. class Computer extends Thread{
    57. private AirData airData ;
    58. public Computer (AirData airData){
    59. this.airData=airData;
    60. }
    61. public void run() {
    62. for(int i=1;i<=2;i++){
    63. synchronized (airData){
    64. System.out.println("计算机显示第"+i+"次读取: "+ airData);
    65. try {
    66. airData.wait();
    67. } catch (InterruptedException e) {
    68. throw new RuntimeException(e);
    69. }
    70. }
    71. try {
    72. //获取三个数据,并显示
    73. sleep(100);
    74. } catch (InterruptedException e) {
    75. throw new RuntimeException(e);
    76. }
    77. }
    78. System.out.println("计算机显示次数已经结束");
    79. }
    80. }
    81. public class Test {
    82. public static void main(String[] args) {
    83. AirData airData = new AirData();
    84. Thread t1 = new TestingSensor(airData);
    85. Thread t2 = new Computer(airData);
    86. t1.setDaemon(true);//设置为影子线程 目的,让所有前台线程结束时,t1也跟着结束。
    87. t1.start();
    88. t2.start();
    89. System.out.println(Thread.currentThread().getName()+" 线程负责开始 t1 t2 然后结束");
    90. System.out.println(Thread.currentThread().getName()+" 线程负责开始 t1 t2 然后结束");
    91. }
    92. }

  • 相关阅读:
    网络原理 --- 传输层Ⅰ UDP协议
    原生JS实现无缝轮播图
    实现按键单击、双击和长按事件的FreeRTOS任务
    【爬虫】7.1. JavaScript动态渲染界面爬取-Selenium
    Python三目运算符(三元运算符)用法详解(含Python代码)
    当我们在控制台运行`npm run xxx`时,到底发生了什么?
    T31开发笔记:OTA升级
    Linux 负载均衡介绍
    学习Java的第二十三天。。。(集合)
    LoadRunner常用函数和参数化
  • 原文地址:https://blog.csdn.net/laocooon/article/details/133748777