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

你好老师

问题:我用管程法实现了多线程的操作(用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 基础深化和提高/多线程技术(旧) 17943楼
JAVA 全系列/第四阶段:数据库与AI协同技术实战/Oracle 数据库的使用 17945楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 17946楼
Python 全系列/第六阶段:数据库与AI协同技术实战/mysql的使用 17947楼
Python 全系列/第一阶段:Python入门/面向对象 17948楼

package com.itbaizhan.maopao;

import java.util.Arrays;

public class TestMaoPao {
    public static void main(String[] args) {
        int[] shuzu = {3, 7, 1, 6, 8, 0, 4, 23, 5, 21};
        int[] shuzu2={51,11,14,66,88,22,33,55,11,10,15,89,37,90};
        maopao(shuzu);
        System.out.println("=============");
        maopao2(shuzu2);
    }
    //基础算法
    public static void maopao(int[] a){
        int temp ;
        for(int i = 0;i<a.length;i++){
            for (int z=0;z<a.length-1-i;z++){
                if (a[z]>a[z+1]){
                    temp=a[z];
                    a[z]=a[z+1];
                    a[z+1]=temp;
                }
            }
            System.out.println("第"+(i+1)+"趟循环结束");
            System.out.println(Arrays.toString(a));
        }
    }
    public static void maopao2(int[] b){
        int temp2;
        boolean r=true;
        for(int i =0;i<b.length;i++){

            for(int z=0;z<b.length-1-i;z++){
                if(b[z]>b[z+1]){
                    temp2=b[z];
                    b[z]=b[z+1];
                    b[z+1]=temp2;
                    r=false;
                }
            }
            System.out.println("第"+(i+1)+"次循环");
            System.out.println(Arrays.toString(b));
            if (r=true){
                break;
            }

        }
    }
}

老师帮忙看一下这个代码是哪里的问题,为什么maopao2方法跑出来排序不正确呢

图片.png

JAVA 全系列/第一阶段:AI驱动的JAVA编程/数组和数据存储 17952楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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