黑马程序员_学习笔记第12天——多线程2

---------------------- ASP.Net+Android+IOS开发、 href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------

1、wait,notify(),notifyAll()都使用在同步中,因为要对持有监视器(锁)的线程操作,所以要使用在同步中,因为只有同步才具有锁。

2、为什么这些操作线程的方法要定义Object类中呢?

因为这些方法在操作同步线程时,都必须要标识他们所操作线程持有的锁,只有同一个锁上的被等待线程,可以被同一个锁上notify唤醒,不可以对不同锁中的线程进行唤醒。也就是说,等待和唤醒必须是同一个锁。而锁可以是任意对象,所以可以被任意对象调用的方法定义在Object类中。

  1. public class InputOutDemo {  
  2.   
  3.     public static void main(String[] args) {  
  4.         Res r = new Res();  
  5.           
  6.         /*Input in = new Input(r); 
  7.         Output out = new Output(r); 
  8.          
  9.         Thread t1 = new Thread(in); 
  10.         Thread t2 = new Thread(out); 
  11.          
  12.         t1.start(); 
  13.         t2.start(); 
  14. <span style="white-space:pre">      </span>*/  
  15.         new Thread(new Input(r)).start();  
  16.         new Thread(new Output(r)).start();  
  17.     }  
  18.   
  19. }  
  20. class Res {   
  21.     private String name;  
  22.     private String sex;  
  23.     private boolean flag = false;  
  24.       
  25.     public synchronized void set(String name,String sex) {  
  26.         if(flag)  
  27.             try {this.wait();}catch (Exception e) {}  
  28.         this.name = name;  
  29.         this.sex = sex;  
  30.         flag = true;  
  31.         this.notify();  
  32.     }  
  33.     public synchronized void out(){  
  34.         if(!flag)  
  35.             try {this.wait();}catch (Exception e) {}  
  36.         System.out.println(name+"........"+sex);  
  37.         flag = false;  
  38.         this.notify();  
  39.     }  
  40. }  
  41.   
  42. class Input implements Runnable {  
  43.     private Res r;  
  44.     Input(Res r) {  
  45.         this.r = r;  
  46.     }  
  47.     int x ;  
  48.       
  49.     public void run() {  
  50.         while(true) {  
  51.             /*synchronized(r){ 
  52.                 if(r.flag) 
  53.                     try {r.wait();}catch (Exception e) {}*/  
  54.                 if(x==0){  
  55.                     r.set("mike","man");  
  56.                 }  
  57.                 else{  
  58.                     r.set("丽丽","女女女女女");  
  59.                 }  
  60.                 x= (x+1)%2;  
  61. /*              r.flag = true; 
  62.                 r.notify();*/  
  63.         //  }     
  64.         }  
  65.     }  
  66. }  
  67.   
  68. class Output implements Runnable {  
  69.     private Res r;  
  70.     Output(Res r) {  
  71.         this.r = r ;  
  72.     }  
  73.     public void run() {  
  74.         while(true) {     
  75.             /*synchronized(r){ 
  76.                 if(!r.flag) 
  77.                     try {r.wait();}catch (Exception e) {} 
  78.                 System.out.println(r.name+"...."+r.sex); 
  79.                 r.flag = false; 
  80.                 r.notify(); 
  81.             } 
  82.             */  
  83.               
  84.             r.out();  
  85.         }  
  86.           
  87.     }  
  88. }  

3、对于多个生产者和消费者,为什么要定义while判断标记:因为让被唤醒的线程再一次判断标记。

      为什么要定义notifyAll:因为需要唤醒对方线程,因为只用notify,容易出现只唤醒本方线程的情况,导致程序中的所有线程都等待。

  1. public class ProducerConsumerDemo {  
  2.   
  3.     public static void main(String[] args) {  
  4.         // TODO Auto-generated method stub  
  5.   
  6.         Resource r = new Resource();  
  7.         Producer pro = new Producer(r);  
  8.         Consumer con = new Consumer(r);  
  9.         Thread t1 = new Thread(pro);  
  10.         Thread t2 = new Thread(con);  
  11.         Thread t3 = new Thread(pro);  
  12.         Thread t4 = new Thread(con);  
  13.         t1.start();  
  14.         t2.start();  
  15.         t3.start();  
  16.         t4.start();  
  17.     }  
  18.   
  19. }  
  20. class Resource {  
  21.     private String name ;  
  22.     private int count = 1 ;  
  23.     private boolean flag = false;  
  24.       
  25.     public synchronized void set(String name ){  
  26.         while(flag)  
  27.             try {wait();}catch (Exception e) {}  
  28.         this.name = name+"--"+count++;  
  29.           
  30.         System.out.println(Thread.currentThread().getName()+"........生产者........."+this.name);  
  31.         flag = true;  
  32.         this.notifyAll();  
  33.     }  
  34.     public synchronized void out() {  
  35.         while(!flag)  
  36.             try {wait();}catch (Exception e) {}  
  37.         System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);  
  38.         flag = false;  
  39.         this.notifyAll();  
  40.     }  
  41.       
  42. }  
  43.   
  44. class Producer implements Runnable {  
  45.     private Resource res;  
  46.     Producer(Resource res) {  
  47.         this.res = res;  
  48.     }  
  49.     public void run() {  
  50.         while(true) {  
  51.             res.set("+商品+");  
  52.         }  
  53.     }  
  54. }  
  55.   
  56. class Consumer implements Runnable {  
  57.     private Resource res;  
  58.     Consumer(Resource res) {  
  59.         this.res=res;  
  60.     }  
  61.     public void run() {  
  62.         while (true) {  
  63.             res.out();  
  64.         }  
  65.     }  
  66. }  


4、jdk1.5中提供了多线程升级解决方法,将同步Synchronized替换成了显示的Lock操作,将Object 中的wait,notify,notifyAll,替换成了Condition对象,该对象可以Lock锁,进行获取。该示例中,实现了本方只唤醒对方的操作。

没有Lock之前,一个锁对应一个wait、notify,有了之后,可以对应多个wait、notify

  1. import java.util.concurrent.locks.Condition;  
  2. import java.util.concurrent.locks.Lock;  
  3. import java.util.concurrent.locks.ReentrantLock;  
  4.   
  5. public class ProducerConsumerDemo2 {  
  6.   
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.         Resource2 r = new Resource2();  
  10.         Producer2 pro = new Producer2(r);  
  11.         Consumer2 con = new Consumer2(r);  
  12.         Thread t1 = new Thread(pro);  
  13.         Thread t2 = new Thread(con);  
  14.         Thread t3 = new Thread(pro);  
  15.         Thread t4 = new Thread(con);  
  16.         t1.start();  
  17.         t2.start();  
  18.         t3.start();  
  19.         t4.start();  
  20.     }  
  21.   
  22. }  
  23. class Resource2 {  
  24.     private String name ;  
  25.     private int count = 1 ;  
  26.     private boolean flag = false;  
  27.       
  28.     private Lock lock = new ReentrantLock();  
  29.     private Condition condition_pro = lock.newCondition();  
  30.     private Condition condition_con = lock.newCondition();  
  31.       
  32.     public synchronized void set(String name )throws InterruptedException{  
  33.         lock.lock();  
  34.         try{  
  35.             while(flag)  
  36.                 condition_pro.await();  
  37.             this.name = name+"--"+count++;  
  38.               
  39.             System.out.println(Thread.currentThread().getName()+"........生产者........."+this.name);  
  40.             flag = true;  
  41.             condition_con.signal();  
  42.         }  
  43.         finally{  
  44.             lock.unlock();//释放锁的动作一定要执行  
  45.         }  
  46.           
  47.     }  
  48.     public void out() throws InterruptedException{  
  49.         lock.lock();  
  50.         try{  
  51.             while(!flag)  
  52.                 condition_con.await();  
  53.             System.out.println(Thread.currentThread().getName()+"...消费者..."+this.name);  
  54.             flag = false;  
  55.             condition_pro.signal();  
  56.         }  
  57.         finally {  
  58.             lock.unlock();  
  59.         }  
  60.     }  
  61.       
  62. }  
  63.   
  64. class Producer2 implements Runnable {  
  65.     private Resource2 res;  
  66.     Producer2(Resource2 res) {  
  67.         this.res = res;  
  68.     }  
  69.     public void run() {  
  70.         while(true) {  
  71.             try{  
  72.                 res.set("+商品+");  
  73.             } catch(InterruptedException e) {  
  74.                   
  75.             }  
  76.               
  77.         }  
  78.     }  
  79. }  
  80.   
  81. class Consumer2 implements Runnable {  
  82.     private Resource2 res;  
  83.     Consumer2(Resource2 res) {  
  84.         this.res=res;  
  85.     }  
  86.     public void run() {  
  87.         while (true) {  
  88.             try {  
  89.                 res.out();  
  90.             }catch(InterruptedException e) {  
  91.                   
  92.             }  
  93.               
  94.         }  
  95.     }  
  96. }  

5、stop方法已经过时,如何停止线程?

只有一种方法,run方法结束。

开启多线程运行,运行代码通常是循环结构,只要控制住循环,就可以让run方法结束,也就是线程结束。

特殊情况:当线程处于了冻结状态,就不会读取到标记,那么线程就不会结束。

当没有指定的方式让冻结的线程恢复到运行状态时,这时需要对冻结进行清除。强制让线程恢复到运行状态中来,这样就可以操作标记让线程结束。

Thread类提供该方法interrupt();

  1. public class StopThreadDemo {  
  2.   
  3.     public static void main(String[] args) {  
  4.         // TODO Auto-generated method stub  
  5.   
  6.         StopThread st = new StopThread();  
  7.           
  8.         Thread t1 = new Thread(st);  
  9.         Thread t2 = new Thread(st);  
  10.           
  11.         t1.start();  
  12.         t2.start();  
  13.           
  14.         int num = 0 ;  
  15.         while(true) {  
  16.             if(num++==60) {  
  17.                 //st.changeFlag();  
  18.                 t1.interrupt();  
  19.                 t2.interrupt();  
  20.                 break;  
  21.             }  
  22.             System.out.println(Thread.currentThread().getName()+"----main-----");  
  23.         }  
  24.     }  
  25.   
  26. }  
  27.   
  28. class StopThread implements Runnable{  
  29.     private boolean flag = true;  
  30.     public synchronized void run() {  
  31.         while(flag) {  
  32.             try {  
  33.                 wait();  
  34.             } catch(InterruptedException e) {  
  35.                 System.out.println(Thread.currentThread().getName()+"-----Exception");  
  36.                 flag = false;  
  37.             }  
  38.             System.out.println(Thread.currentThread().getName()+"------run------");  
  39.         }  
  40.     }  
  41.     public void changeFlag() {  
  42.         flag = false;  
  43.     }  
  44.       
  45. }  

6、守护线程:setDaemon(true);该方法必须放在线程开启(start)之前,当正在运行的线程都是守护线程时,java虚拟机退出。

7、join特点:当A线程执行到了B线程的join()方法时,A就会等待,等B线程都执行完,A才会执行。join可以用来临时加入线程执行。

  1. public class JoinDemo {  
  2.   
  3.     public static void main(String[] args) throws Exception{  
  4.         // TODO Auto-generated method stub  
  5.   
  6.         Demo d = new Demo();  
  7.           
  8.         Thread t1 = new Thread(d);  
  9.         Thread t2 = new Thread(d);  
  10.           
  11.         t1.start();  
  12.         t1.join();  
  13.         t2.start();  
  14.           
  15.         for(int x=0; x<80 ; x++) {  
  16.             System.out.println("main...."+x);  
  17.         }  
  18.         System.out.println("over");  
  19.     }  
  20.   
  21. }  
  22. class Demo implements Runnable {  
  23.     public void run() {  
  24.         for ( int x = 0; x<70; x++) {  
  25.             System.out.println(Thread.currentThread().getName()+"..."+x);  
  26.         }  
  27.     }  
  28. }  

8、优先级:

设置优先级: t1.setPriority(Thread.MAX_PRIORITY),或MIN_PRIORITY或NORM_PRIORITY,(优先级1-10),正常的都为5级

9、yield()暂停当前正在执行的线程对象,并执行其它线程。Thread.yield()

  1. public class ThreadTest {  
  2.   
  3.     public static void main(String[] args) {  
  4.         // TODO Auto-generated method stub  
  5.         new Thread() {  
  6.             public void run() {  
  7.                 for(int x=0; x<100; x++) {  
  8.                     System.out.println(Thread.currentThread().getName()+"---1");  
  9.                 }  
  10.             }  
  11.         }.start();  
  12.           
  13.         for(int x=0; x<100; x++) {  
  14.             System.out.println(Thread.currentThread()+"----2");  
  15.         }  
  16.           
  17.         Runnable r = new Runnable() {  
  18.             public void run() {  
  19.                 for(int x=0; x<100; x++) {  
  20.                     System.out.println(Thread.currentThread()+"----3");  
  21.                 }  
  22.             }  
  23.         };  
  24.         new Thread(r).start();  
  25.   
  26.     }  
  27.   
  28. }  


---------------------- ASP.Net+Android+IOS开发、 href="http://edu.csdn.net"target="blank">.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数字乡村和智慧农业的数字化转型是当前农业发展的新趋势,旨在通过应用数字技术,实现农业全流程的再造和全生命周期的管理服务。中国政府高度重视这一领域的发展,提出“数字中国”和“乡村振兴”战略,以提升国家治理能力,推动城乡融合发展。 数字乡村的建设面临乡村治理、基础设施、产业链条和公共服务等方面的问题,需要分阶段实施《数字乡村发展战略纲要》来解决。农业数字化转型的需求包括满足市民对优质农产品的需求、解决产销对接问题、形成优质优价机制、提高农业劳动力素质、打破信息孤岛、提高农业政策服务的精准度和有效性,以及解决农业融资难的问题。 数字乡村建设的关键在于构建“1+3+4+1”工程,即以新技术、新要素、新商业、新农民、新文化、新农村为核心,推进数据融合,强化农业大数据的汇集功能。数字农业大数据解决方案以农业数字底图和数据资源为基础,通过可视化监管,实现区域农业的全面数字化管理。 数字农业大数据架构基于大数据、区块链、GIS和物联网技术,构建农业大数据中心、农业物联网平台和农村综合服务指挥决策平台三大基础平台。农业大数据中心汇聚各类涉农信息资源和业务数据,支持大数据应用。信息采集系统覆盖市、县、乡、村多级,形成高效的农业大数据信息采集体系。 农业物联网平台包括环境监测系统、视频监控系统、预警预报系统和智能控制系统,通过收集和监测数据,实现对农业环境和生产过程的智能化管理。综合服务指挥决策平台利用数据分析和GIS技术,为农业决策提供支持。 数字乡村建设包括三大服务平台:治理服务平台、民生服务平台和产业服务平台。治理服务平台通过大数据和AI技术,实现乡村治理的数字化;民生服务平台利用互联网技术,提供各类民生服务;产业服务平台融合政企关系,支持农业产业发展。 数字乡村的应用场景广泛,包括农业生产过程、农产品流通、农业管理和农村社会服务。农业生产管理系统利用AIoT技术,实现农业生产的标准化和智能化。农产品智慧流通管理系统和溯源管理系统提高流通效率和产品追溯能力。智慧农业管理通过互联网+农业,提升农业管理的科学性和效率。农村社会服务则通过数字化手段,提高农村地区的公共服务水平。 总体而言,数字乡村和智慧农业的建设,不仅能够提升农业生产效率和管理水平,还能够促进农村地区的社会经济发展,实现城乡融合发展,是推动中国农业现代化的重要途径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值