会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132376个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3497楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 3498楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3503楼

你好老师

问题:我用管程法实现了多线程的操作(用synchronized的方法)但是还是出现了脏数据;

package Thread_study03;
/**
  * 协作模型:生产者消费者实现方式一:管程法
  * 借助缓冲区
 * @author 陈世丰
 *
 */
 
 
public class CoTest01 {
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  SynContainer synContainer= new SynContainer();
  new Thread(new Productor("a",2,synContainer)).start();//生产2
  new Thread(new Consumer("m",4,synContainer)).start();//消费4
  new Thread(new Productor("a1",4,synContainer)).start();//生产4
  new Thread(new Consumer("m1",2,synContainer)).start();//消费2
  
 }
}




class SynContainer{
  Steamedbun[] buns = new Steamedbun[10]; //存储容器
  int count = 0; //计数器
  //生产
  public  synchronized void push(Steamedbun a) throws InterruptedException{
   if(count>=10){
    this.wait();
     }
   buns[count]=a;
   count+=1;
   this.notify();
  }
  //消费
  public  synchronized Steamedbun pop() throws InterruptedException{
   if(count<=0){
    this.wait();
     }
   Steamedbun a=buns [count];
   buns [count]=null;
   count-=1;
   this.notify();
    return a;
   
  }
 
}



//馒头类
class  Steamedbun{
 int id;
 public Steamedbun(int id) {
  this.id = id;
 }
}


//生产者
class Productor implements Runnable{
     String name;
     int count ;
     SynContainer synContainer;
 public Productor(String name, int count,SynContainer synContainer) {
  this.name = name;
  this.count = count;
  this.synContainer=synContainer;
 }
 @Override
 public void run() {
  for(int i=0;i<count;i++){
   try {
    Thread.sleep(200);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    synContainer.push(new Steamedbun(i));
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   System.out.println(name+"成产后剩余-->"+synContainer.count+"剩余个馒头");
   
  }
  
 }
 
 
}
//消费者
class Consumer implements Runnable{
     String name;
     int count;
     SynContainer synContainer;
 public Consumer(String name, int count, SynContainer synContainer) {
  this.name = name;
  this.count = count;
  this.synContainer = synContainer;
 }
 @Override
 public void run() {
  // TODO Auto-generated method stub
  for(int i=0;i<count;i++){
  try {
   Thread.sleep(200);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  try {
   synContainer.pop();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println(name+"消费后剩余-->"+synContainer.count+"个馒头");
  }
 }
 
 
}


image.png

Training.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 3504楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3505楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 3507楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 3508楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637