Java用BlockingQueue来实现简单的消息队列(生产者与消费者模式)

学习过程,当做笔记记录下来了:

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class ProducerAndConsumer {
	public static void main(String[] args) throws InterruptedException {
		  ProducerAndConsumer pac = new ProducerAndConsumer();
		  //创建一个容量为10的消息队列
		  BlockingQueue<Message> blockingQueue = new ArrayBlockingQueue<Message>(10);
		  //创建带有消息队列的生产者
		  Producer producer = pac.new Producer(blockingQueue);
		  //创建带有消息队列的消费者1
		  Consumer c1 = pac.new Consumer(blockingQueue);
		  c1.setName("Tread-c1");
		  c1.start();
		  //创建带有消息队列的消费者2
		  Consumer c2 = pac.new Consumer(blockingQueue);
		  c2.setName("Tread-c2");
		  c2.start();
		  for(int i = 0; i < 1000; i++){
		   //模拟生产者投放消息
		   Message message = pac.new Message();
		   message.setBody(i+"号的body");
		   message.setTopoic(i+"号的topoic");
		   producer.sendMessage(message);
		   //Thread.sleep(500);
		}
	}
	
//生产者
 class Producer{
	  private BlockingQueue<Message> queue;
	  public Producer(BlockingQueue<Message> queue){
	  	this.queue = queue;
	  }
	  public void sendMessage(Message message){
	   	try {
	    		queue.put(message);
	   	} catch (InterruptedException e) {
	   		 e.printStackTrace();
	   	}
	  }
}

//消费者
 class Consumer extends Thread{
	  private BlockingQueue<Message> queue;
	  public Consumer(BlockingQueue<Message> queue){
	  	 this.queue = queue;
	  }
	  @Override
	  public void run() {
	   	while(true){
	   		 if(!queue.isEmpty()){
		    		try {
		     			//System.out.println("线程:"+Thread.currentThread().getName()+"获取到的topic值为:"+queue.take().getBody());
		      			System.out.println("线程:"+Thread.currentThread().getName()+"获取到的body值为:"+queue.take().getBody());
		    		 } catch (InterruptedException e) {
		      			e.printStackTrace();
		    	 	}
		   	}
	  	 }
	  }
 }

//消息
 class  Message{
	  public Message(){}
	  private String topoic;
	  private String body;
	 
	  public String getTopoic() {
	   	return topoic;
	  }
	  public synchronized void setTopoic(String topoic) {
	   	this.topoic = topoic;
	  }
	  public String getBody() {
	  	 return body;
	  }
	  public synchronized void setBody(String body) {
	  	 this.body = body;
	  }
	  
}

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【项目介绍】 Java开发基于多线程和NIO实现聊天室源码+项目说明(含服务端+客户端).zip 涉及到的技术点 - 线程池ThreadPoolExecutor - 阻塞队列BlockingQueue生产者消费者模式 - Selector - Channel - ByteBuffer - ProtoStuff 高性能序列化 - HttpClient连接池 - Spring依赖注入 - lombok简化POJO开发 - 原子变量 - 内置锁 - CompletionService - log4j+slf4j日志 - 实现的功能 - 登录注销 - 单聊 - 群聊 - 客户端提交任务,下载图片并显示 - 上线下线公告 - 在线用户记录 - 批量下载豆瓣电影的图片,并打为压缩包传输给客户端 - 客户端使用方式: - 登录:默认用户名是user1-user5,密码分别是pwd1-pwd5 - 例:打开客户端后输入用户名为user1,密码为pwd1 - 注销:关闭客户端即可 - 单聊:@username:message - 例:@user2:hello - 群聊:message - 例:hello,everyone - 提交任务:task.file:图片的URL / task.crawl_image:豆瓣电影的id[?imageSize=n] 可以加请求参数 - 例1:task.file:https://img1.doubanio.com/view/movie_poster_cover/lpst/public/p2107289058.webp 下载完毕后会弹出一个框,输入想将其保存到的路径,比如E:/img.webp - 例2:task.crawl_image:1292371?imageSize=2 下载完毕后在弹出的框中输入E:/images.zip 【备注】 1.项目代码均经过功能验证ok,确保稳定可靠运行。欢迎下载食用体验! 2.主要针对各个计算机相关专业,包括计算机科学、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师、企业员工。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。在使用过程中,如有问题或建议,请及时沟通。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值