• 线程间的通信



    1.举个栗子:消费者和生产者

    代码实现如下:

     


    共享资源类:

     1 package producer_Consumer;
     2 
     3 /**
     4  * @param name    存储的姓名
     5  * @param gender  存储的性别
     6  * @param isEmpty 表示共享资源对象是否为空
     7  * @author 王彪
     8  *
     9  */
    10 public class ShareResources {
    11     private String name;
    12     private String gender;
    13     private boolean isEmpty = true;
    14 
    15     // 生产者向共享资源对象中存储数据
    16     synchronized public void push(String name, String gender) {
    17         try {
    18             // 共享资源对象不为空时 生产者线程等待
    19             while (!isEmpty) {
    20                 this.wait();
    21             }
    22 
    23             this.name = name;
    24             Thread.sleep(10);
    25             this.gender = gender;
    26             isEmpty = false;
    27             this.notify();
    28         } catch (InterruptedException e) {
    29             e.printStackTrace();
    30         }
    31     }
    32 
    33     // 消费者从共享资源对象中取出数据
    34     synchronized public void popup() {
    35         try {
    36             //当前isEmpty为true的时候,为空,等着消费者来生产。
    37             while(isEmpty) {
    38                 //使用同步锁对象来调用,表示当前线程释放同步锁,进入等待池,只能被其他线程所唤醒。
    39                 this.wait();
    40             }
    41             Thread.sleep(10);
    42             System.out.println(this.name + "-" + this.gender);
    43             isEmpty = true;
    44             this.notify();
    45         } catch (InterruptedException e) {
    46             e.printStackTrace();
    47         }
    48     }
    49 }

    消费者类:

     1 package producer_Consumer;
     2 
     3 public class Consumer implements Runnable {
     4     private ShareResources resource = null;
     5 
     6     public Consumer(ShareResources resource) {
     7         this.resource = resource;
     8     }
     9 
    10     @Override
    11     public void run() {
    12         for (int i = 0; i < 50; i++) {
    13              resource.popup();
    14         }
    15     }
    16 
    17 }

    生产者类:

     1 package producer_Consumer_ByLock;
     2 
     3 public class Producer implements Runnable{
     4     ShareResources resources = null;
     5     public Producer(ShareResources resources) {
     6         this.resources = resources;
     7     }
     8     @Override
     9     public void run() {
    10         for(int i = 0; i < 50; i++) {
    11             if(i % 2 == 0) {
    12                 resources.push("李白", "男");
    13             }else {
    14                 resources.push("王昭君", "女");
    15             }
    16         }
    17     }
    18 }

    测试类:

    1 package producer_Consumer_ByLock;
    2 
    3 public class Test {
    4     public static void main(String[] args) {
    5         ShareResources resources = new ShareResources();
    6         new Thread(new Consumer(resources)).start();
    7         new Thread(new Producer(resources)).start();
    8     }
    9 }

    2.使用了Lock和Condition接口:

     1 package producer_Consumer_ByLock;
     2 
     3 import java.util.concurrent.locks.Condition;
     4 import java.util.concurrent.locks.Lock;
     5 import java.util.concurrent.locks.ReentrantLock;
     6 
     7 public class ShareResources {
     8     private String name;
     9     private String gender;
    10     private boolean isEmpty = true;
    11     private final Lock lock = new ReentrantLock();
    12     private Condition condition = lock.newCondition();
    13     public void push(String name, String gender) {
    14         lock.lock();
    15         try {
    16             while(!isEmpty) {
    17                 condition.await();
    18             }
    19             this.name = name;
    20             Thread.sleep(10);
    21             this.gender = gender;
    22             condition.signal();
    23             isEmpty = false;
    24         } catch (InterruptedException e) {
    25             e.printStackTrace();
    26         }finally {
    27             lock.unlock();
    28         }
    29     }
    30 
    31     public void popup() {
    32         lock.lock();
    33         try {
    34             while(isEmpty) {
    35                 condition.await();
    36                 Thread.sleep(10);
    37                 System.out.println(this.name + "-" + this.gender);
    38                 condition.signal();
    39                 isEmpty = true;
    40             }
    41         } catch (InterruptedException e) {
    42             e.printStackTrace();
    43         }finally {
    44             lock.unlock();
    45         }
    46     }
    47 }

    3.关于死锁


     4.线程的生命周期:


    5. 线程控制操作:


    6.定时器和线程组:

  • 相关阅读:
    win7常用快捷键
    java中构造代码块、方法调用顺序问题
    eclipse项目改为maven项目导致svn无法比较历史数据的解决办法
    linux配置Anaconda python集成环境
    DataFrame对行列的基本操作实战
    驱动:电阻屏触摸芯片NS2009
    读书笔记:代码大全(第二版)
    资料:磁角度传感器芯片
    经验:FatFs文件系统实时写入
    笔记:CAN收发器-TJA1051T与TJA1051T/3调试总结
  • 原文地址:https://www.cnblogs.com/AmosWong/p/9538091.html
Copyright © 2020-2023  润新知