生产消费问题整合版

代码如下:

public class SynchronizedExample {

 /**
  * 生产仓库
  * @author fancy
  *
  */
 private static class Factory {
  //最大库存量
  public static final int MAX_SIZE = 100;
  //当前存储数量
  public int currentNum;
  
  public Factory() {}
  
  public Factory(int currentNum) {
   this.currentNum = currentNum;
  }
  
  /**
   * 生产指定数量的产品
   */
  public synchronized void produce(int needNum) {
   while (needNum + currentNum > MAX_SIZE) {
    System.out.println("最多只能生产 " + (MAX_SIZE - currentNum) + " , 无法满足需求的数量 " + needNum);
    try {
     wait();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   //满足条件后生产之并更新当前库存
   currentNum += needNum;
   System.out.println("已经生产了 " + needNum + " , 当前的库存数量为 " + currentNum);
   //唤醒在此对象上监听器上等待的所有线程
   notifyAll();
  }
  
  /**
   * 消费者库存
   */
  public synchronized void consume(int needNum) {
   //需求量大于当前库存
   while (needNum > currentNum) {
    System.out.println("当前可消费库存只有 " + currentNum + " , 无法满足需求 " + needNum);
    try {
     wait();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   
   currentNum -= needNum;
   System.out.println("已经消费了 " + needNum + " , 当前的库存数量为 " + currentNum);
   notifyAll();
  }
 }
 
 /**
  * 生产者
  */
 private static class Productor extends Thread {
  private int needNum;
  private Factory mFactory;
  
  public Productor(int needNum, Factory mFactory) {
   this.needNum = needNum;
   this.mFactory = mFactory;
  }

  @Override
  public void run() {
   mFactory.produce(needNum);
  }
 }
 
 /**
  * 消费者
  */
 private static class Customer extends Thread {
  private int needNum;
  private Factory mFactory;
  
  public Customer(int needNum, Factory mFactory) {
   this.needNum = needNum;
   this.mFactory = mFactory;
  }
  
  @Override
  public void run() {
   mFactory.consume(needNum);
  }
  
 }
 
 public static void main(String[] args) {
  Factory mFactory = new Factory(100);
  Customer mCustomer = new Customer(30, mFactory);
  Customer mCustomer2 = new Customer(50, mFactory);
  Productor mProductor = new Productor(170, mFactory);
  
  mCustomer.start();
  mCustomer2.start();
  mProductor.start();
 }
}


### 回答1: Spring Boot是一款用于简化Spring应用程序开发的框架,而Kafka是一款高性能的分布式消息系统。在Spring Boot中整合Kafka可以实现多生产者多消费者的功能。 首先,我们需要在pom.xml文件中添加Kafka的依赖: ``` <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> ``` 接下来,我们需要配置Kafka的相关信息。在application.properties文件中添加Kafka的相关配置项,包括Kafka服务器地址、生产者和消费者的配置等。 然后,我们需要创建生产者和消费者的类。对于生产者,可以使用KafkaTemplate来发送消息,通过指定Topic名称和消息内容来发送消息: ```java @Autowired private KafkaTemplate<String, String> kafkaTemplate; public void sendMessage(String topic, String message) { kafkaTemplate.send(topic, message); } ``` 对于消费者,我们可以使用@KafkaListener注解来订阅指定的Topic,然后通过处理方法来处理接收到的消息: ```java @KafkaListener(topics = "topic-name") public void receiveMessage(String message) { // 处理接收到的消息 } ``` 最后,我们需要在应用程序启动时配置Kafka的相关配置。可以使用@Configuration注解来定义一个配置类,并在类中配置Kafka的相关信息。然后,在应用程序启动时,通过@SpringBootApplication注解来扫描配置类。 通过以上的步骤,我们就可以实现Spring Boot与Kafka的整合,并实现多生产者多消费者的功能。当有新的消息发送到Kafka的Topic时,消费者将能够接收到并进行相应的处理。 ### 回答2: Spring Boot是一个开源的Java框架,可用于快速开发基于Spring的应用程序。而Kafka是一个分布式的流处理平台,它能够将大量数据流进行高效地处理和传输。 在Spring Boot中整合Kafka,可以实现多生产者和多消费者的功能。首先,在项目的pom.xml文件中添加Kafka相关的依赖,例如: ``` <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> ``` 然后,在application.properties文件中配置Kafka的连接信息,包括Kafka的地址、端口等: ``` spring.kafka.bootstrap-servers=localhost:9092 ``` 接下来,创建生产者的代码。在Spring Boot中,可以使用KafkaTemplate来发送消息。例如,通过向topic发送消息: ``` @Autowired private KafkaTemplate<String, String> kafkaTemplate; public void sendMessage(String message) { kafkaTemplate.send("topicName", message); } ``` 创建消费者的代码也很简单。在Spring Boot中,可以使用@KafkaListener注解来监听特定的topic,并在接收到消息时执行相应的方法。例如: ``` @KafkaListener(topics = "topicName", groupId = "groupId") public void receiveMessage(String message) { // 处理接收到的消息 } ``` 最后,为了支持多个生产者和消费者,可以在配置文件中配置多个topic和groupId。每个生产者和消费者可以发送和接收不同的topic消息,并使用不同的groupId进行消费。这样就实现了Spring Boot中多生产者和多消费者的整合。 通过以上步骤,我们就可以在Spring Boot中实现Kafka的多生产者和多消费者功能。无论是发送消息还是接收消息,都可以得到很好的扩展和灵活性。 ### 回答3: 在Spring Boot中整合Kafka实现多生产者和多消费者的过程相对简单。首先,我们需要在pom.xml文件中添加Kafka的依赖: ```xml <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> ``` 接下来,我们需要在application.properties或application.yml配置文件中配置Kafka的相关属性,包括Kafka服务器地址、端口、消费者和生产者的配置等。 接下来,我们可以创建一个生产者实例来发送消息: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; @Component public class KafkaProducer { @Autowired private KafkaTemplate<String, String> kafkaTemplate; public void sendMessage(String topic, String message) { kafkaTemplate.send(topic, message); } } ``` 通过使用注入的KafkaTemplate实例,我们可以调用send方法来发送消息到指定的主题。 然后,我们可以创建一个消费者实例来接收消息: ```java import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class KafkaConsumer { @KafkaListener(topics = "topicName") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 通过使用@KafkaListener注解来监听指定的主题,当有消息到达时,Kafka会自动调用receiveMessage方法来处理接收到的消息。 如果我们需要使用多个生产者和消费者,只需要在相应的类上添加不同的注解和配置即可。 综上所述,通过Spring Boot的Kafka整合,我们可以轻松实现多个生产者和多个消费者之间的消息传递。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值