java基础复习之二:Java实现同步机制是(生产消费者)

本文介绍了一个使用Java实现的生产者消费者同步机制,通过synchronized关键字、wait()和notify()方法来确保资源的合理分配。生产者随机生成产品,消费者随机消费产品,系统在产品数量达到上限或空闲时进行同步操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    Java实现线程的同步,可以通过synchronized,wait(), notitfy(), notifyAll();假设一个线程(生产者)生产产品,一个线程(消费者)消费产品,其访问的资源时间都是随机的,这样就是生产者必须得产品(资源)消费完成之后才可以生产,而消费者必须在产品有的时候才可以消费,这就是必须对资源进行同步操作,对资源的使用部分的代码需要加入锁。

下列是我的实现方法:

[java]  view plain copy print ?
  1. package com.lzb.common;      
  2.       
  3. import java.util.Random;      
  4. import java.util.concurrent.TimeUnit;      
  5. /**   
  6.  *    
  7.  * 功能描述:生产者消费者   
  8.  *          注:锁synhronized是放在“资源的类的内部方法中”,而不是在线程的代码中   
  9.  */      
  10. public class ProducterCustomer {      
  11.           
  12.     private PCResource pc = new PCResource();      
  13.     // 生产者与消费者调用的时间随机      
  14.     private Random rand = new Random(50);      
  15.           
  16.     public void init() {              
  17.         // 生产者      
  18.         new Thread(new Runnable(){      
  19.       
  20.             public void run() {                   
  21.                 while(true) {      
  22.                     pc.producter();      
  23.                     try {      
  24.                         TimeUnit.MILLISECONDS.sleep(rand.nextInt(1000));      
  25.                     } catch (InterruptedException e) {      
  26.                         e.printStackTrace();      
  27.                     }      
  28.                 }      
  29.                       
  30.             }}).start();      
  31.           
  32.         // 消费者      
  33.         new Thread(new Runnable(){      
  34.       
  35.             public void run() {      
  36.                 while(true) {      
  37.                     pc.customer();      
  38.                     try {      
  39.                         TimeUnit.MILLISECONDS.sleep(rand.nextInt(1000));      
  40.                     } catch (InterruptedException e) {      
  41.                         e.printStackTrace();      
  42.                     }      
  43.                 }      
  44.             }}).start();      
  45.     }      
  46.           
  47.     public static void main(String[] args) {      
  48.               
  49.         ProducterCustomer startPc = new ProducterCustomer();      
  50.         startPc.init();      
  51.     }             
  52. }      
  53.       
  54. /**   
  55.  *    
  56.  * 功能描述:同步资源   
  57.  *   
  58.  */      
  59. class PCResource {      
  60.           
  61.     private static final Integer MAX = 1;      
  62.     private static final Integer MIN = 0;      
  63.     private int product = 0;      
  64.     // 同步互斥通信标志      
  65.     private boolean isRunning = true;      
  66.           
  67.     /**   
  68.      *    
  69.      * 功能描述:生产产品,当生产一个商品后挂起,等待消费者消费完成   
  70.      */      
  71.     public synchronized void producter() {      
  72.               
  73.         while(isRunning) {      
  74.             try {      
  75.                 wait();      
  76.             } catch (InterruptedException e) {      
  77.                 e.printStackTrace();      
  78.             }      
  79.             product++;      
  80.             System.out.println("-------->Product " + product + " good");      
  81.             if(product >= MAX)      
  82.                 break;      
  83.         }      
  84.         isRunning = false;      
  85.         notify();      
  86.     }      
  87.           
  88.     /**   
  89.      *    
  90.      * 功能描述:消费者,消费产品,当产品为0时,等待生产者生产产品   
  91.      */      
  92.     public synchronized void customer() {      
  93.               
  94.         while(!isRunning) {      
  95.             try {      
  96.                 wait();      
  97.             } catch (InterruptedException e) {      
  98.                 e.printStackTrace();      
  99.             }      
  100.             product--;      
  101.             System.out.println("Limit " + product + " goods<----------");      
  102.             if(product <= MIN) {      
  103.                 break;      
  104.             }      
  105.         }      
  106.         isRunning = true;      
  107.         notify();      
  108.     }      
  109. }      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值