一:栈  ,先进后出,通常叫压栈,往箱子里面放衣服,最先进去,先进后出,后进先出LIFO.底层采用Vector

import java.util.Stack;public class StackQueueTest {public static void main(String[] args) {//栈  ,先进后出,通常叫压栈,往箱子里面放衣服,最先进去,先进后出,后进先出LIFO.底层采用VetorStack<String> stack=new Stack<String>();stack.push("");  //添加stack.pop();  //移除(在peel()方法功能之上进行移除操作)stack.peek(); //取出//因为Stack在市场上比较陈旧,所以用的不多了//队列.Queue 在企业中用的比较多,比如:消息队列(淘宝秒杀,抢购的时候,流量1000万,但是服务器1秒中只能处理10万请求,处理办法:将请求储存到队列消息服务器//FIFO 先进先出,第一个先出去,火车进隧道一样,jdk1.5出来的接口,它主要需要引用的包是Java.util.concurrent.LinkedlockingQueue/BlockingQueue/PriorityBlockingQueue//activeMQ 做了解用//双端队列:Deque<E> FIFO,LIFO,能够代替Stack}}

二:队列.Queue 在企业中用的比较多,比如:消息队列(淘宝秒杀,抢购的时候,流量1000万,但是服务器1秒中只能处理10万请求,处理办法:将请求储存到队列消息服务器
        //FIFO 先进先出,第一个先出去,火车进隧道一样,jdk1.5出来的接口,它主要需要引用的包是           Java.util.concurrent.LinkedlockingQueue/BlockingQueue/PriorityBlockingQueue
        //activeMQ 做了解用

 1.ActiveMQ 提供了Windows 和Linux、Unix 等几个版本,这里选择了Linux 版本下进行开发

ActiveMQ官网下载地址:http://activemq.apache.org/components/classic/download/

下载完安装包,解压之后的目录:

从它的目录来说,还是很简单的:

  • bin存放的是脚本文件
  • conf存放的是基本配置文件
  • data存放的是日志文件
  • docs存放的是说明文档
  • examples存放的是简单的实例
  • lib存放的是activemq所需jar包
  • webapps用于存放项目的目录

2、启动ActiveMQ

进入到ActiveMQ 安装目录的Bin 目录,linux 下输入 ./activemq start 启动activeMQ 服务。

   输入命令之后,会提示我们创建了一个进程IP 号,这时候说明服务已经成功启动了。

ActiveMQ默认启动时,启动了内置的jetty服务器,提供一个用于监控ActiveMQ的admin应用。 
  admin:http://127.0.0.1:8161/admin/

  我们在浏览器打开链接之后输入账号密码(这里和tomcat 服务器类似)

  默认账号:admin

  密码:admin

到这里为止,ActiveMQ 服务端就启动完毕了。

   ActiveMQ 在linux 下的终止命令是 ./activemq stop

3.创建一个ActiveMQ工程

项目目录结构:

上述在官网下载ActiveMq 的时候,我们可以在目录下看到一个jar包:

这个jar 包就是我们需要在项目中进行开发中使用到的相关依赖。

3.1创建生产者

public class Producter {//ActiveMq 的默认用户名private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;//ActiveMq 的默认登录密码private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;//ActiveMQ 的链接地址private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;AtomicInteger count = new AtomicInteger(0);//链接工厂ConnectionFactory connectionFactory;//链接对象Connection connection;//事务管理Session session;ThreadLocal<MessageProducer> threadLocal = new ThreadLocal<>();public void init(){try {//创建一个链接工厂connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);//从工厂中创建一个链接connection  = connectionFactory.createConnection();//开启链接connection.start();//创建一个事务(这里通过参数可以设置事务的级别)session = connection.createSession(true,Session.SESSION_TRANSACTED);} catch (JMSException e) {e.printStackTrace();}}public void sendMessage(String disname){try {//创建一个消息队列Queue queue = session.createQueue(disname);//消息生产者MessageProducer messageProducer = null;if(threadLocal.get()!=null){messageProducer = threadLocal.get();}else{messageProducer = session.createProducer(queue);threadLocal.set(messageProducer);}while(true){Thread.sleep(1000);int num = count.getAndIncrement();//创建一条消息TextMessage msg = session.createTextMessage(Thread.currentThread().getName()+"productor:我是大帅哥,我现在正在生产东西!,count:"+num);System.out.println(Thread.currentThread().getName()+"productor:我是大帅哥,我现在正在生产东西!,count:"+num);//发送消息messageProducer.send(msg);//提交事务session.commit();}} catch (JMSException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}
}

3.2创建消费者

public class Comsumer {private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;private static final String BROKEN_URL = ActiveMQConnection.DEFAULT_BROKER_URL;ConnectionFactory connectionFactory;Connection connection;Session session;ThreadLocal<MessageConsumer> threadLocal = new ThreadLocal<>();AtomicInteger count = new AtomicInteger();public void init(){try {connectionFactory = new ActiveMQConnectionFactory(USERNAME,PASSWORD,BROKEN_URL);connection  = connectionFactory.createConnection();connection.start();session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);} catch (JMSException e) {e.printStackTrace();}}public void getMessage(String disname){try {Queue queue = session.createQueue(disname);MessageConsumer consumer = null;if(threadLocal.get()!=null){consumer = threadLocal.get();}else{consumer = session.createConsumer(queue);threadLocal.set(consumer);}while(true){Thread.sleep(1000);TextMessage msg = (TextMessage) consumer.receive();if(msg!=null) {msg.acknowledge();System.out.println(Thread.currentThread().getName()+": Consumer:我是消费者,我正在消费Msg"+msg.getText()+"--->"+count.getAndIncrement());}else {break;}}} catch (JMSException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}
}

4.运行ActiveMQ项目

4.1生产者开始生产消息

public class TestMq {public static void main(String[] args){Producter producter = new Producter();producter.init();TestMq testMq = new TestMq();try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}//Thread 1new Thread(testMq.new ProductorMq(producter)).start();//Thread 2new Thread(testMq.new ProductorMq(producter)).start();//Thread 3new Thread(testMq.new ProductorMq(producter)).start();//Thread 4new Thread(testMq.new ProductorMq(producter)).start();//Thread 5new Thread(testMq.new ProductorMq(producter)).start();}private class ProductorMq implements Runnable{Producter producter;public ProductorMq(Producter producter){this.producter = producter;}@Overridepublic void run() {while(true){try {producter.sendMessage("Jaycekon-MQ");Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}}}}
}

运行结果:

INFO | Successfully connected to tcp://localhost:61616
Thread-6productor:我是大帅哥,我现在正在生产东西!,count:0
Thread-4productor:我是大帅哥,我现在正在生产东西!,count:1
Thread-2productor:我是大帅哥,我现在正在生产东西!,count:3
Thread-5productor:我是大帅哥,我现在正在生产东西!,count:2
Thread-3productor:我是大帅哥,我现在正在生产东西!,count:4
Thread-6productor:我是大帅哥,我现在正在生产东西!,count:5
Thread-3productor:我是大帅哥,我现在正在生产东西!,count:6
Thread-5productor:我是大帅哥,我现在正在生产东西!,count:7
Thread-2productor:我是大帅哥,我现在正在生产东西!,count:8
Thread-4productor:我是大帅哥,我现在正在生产东西!,count:9
Thread-6productor:我是大帅哥,我现在正在生产东西!,count:10
Thread-3productor:我是大帅哥,我现在正在生产东西!,count:11
Thread-5productor:我是大帅哥,我现在正在生产东西!,count:12
Thread-2productor:我是大帅哥,我现在正在生产东西!,count:13
Thread-4productor:我是大帅哥,我现在正在生产东西!,count:14
Thread-6productor:我是大帅哥,我现在正在生产东西!,count:15
Thread-3productor:我是大帅哥,我现在正在生产东西!,count:16
Thread-5productor:我是大帅哥,我现在正在生产东西!,count:17
Thread-2productor:我是大帅哥,我现在正在生产东西!,count:18
Thread-4productor:我是大帅哥,我现在正在生产东西!,count:19

4.2消费者开始消费

public class TestConsumer {public static void main(String[] args){Comsumer comsumer = new Comsumer();comsumer.init();TestConsumer testConsumer = new TestConsumer();new Thread(testConsumer.new ConsumerMq(comsumer)).start();new Thread(testConsumer.new ConsumerMq(comsumer)).start();new Thread(testConsumer.new ConsumerMq(comsumer)).start();new Thread(testConsumer.new ConsumerMq(comsumer)).start();}private class ConsumerMq implements Runnable{Comsumer comsumer;public ConsumerMq(Comsumer comsumer){this.comsumer = comsumer;}@Overridepublic void run() {while(true){try {comsumer.getMessage("Jaycekon-MQ");Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}}}}
}

运行结果:

INFO | Successfully connected to tcp://localhost:61616
Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:4--->0
Thread-3: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:36--->1
Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:38--->2
Thread-5: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:37--->3
Thread-2: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:2--->4
Thread-3: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:40--->5
Thread-4: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:42--->6
Thread-5: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:41--->7
Thread-2: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:1--->8
Thread-3: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:44--->9
Thread-4: Consumer:我是消费者,我正在消费MsgThread-4productor:我是大帅哥,我现在正在生产东西!,count:46--->10
Thread-5: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:45--->11
Thread-2: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:3--->12
Thread-3: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:48--->13
Thread-4: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:50--->14
Thread-5: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:49--->15
Thread-4: Consumer:我是消费者,我正在消费MsgThread-2productor:我是大帅哥,我现在正在生产东西!,count:54--->16
Thread-2: Consumer:我是消费者,我正在消费MsgThread-5productor:我是大帅哥,我现在正在生产东西!,count:6--->17
Thread-3: Consumer:我是消费者,我正在消费MsgThread-6productor:我是大帅哥,我现在正在生产东西!,count:52--->18
Thread-5: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:53--->19
Thread-4: Consumer:我是消费者,我正在消费MsgThread-3productor:我是大帅哥,我现在正在生产东西!,count:58--->20

查看运行结果,我们可以做ActiveMQ 服务端,Queues 中查看我们生产的消息。

5.ActiveMQ的特性

5.1ActiveMq特性

  1. 多种语言和协议编写客户端。语言: Java, C, C++, C#, Ruby, Perl, Python, PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
  2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
  3. 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
  4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4, GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
  5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
  6. 支持通过JDBC和journal提供高速的消息持久化
  7. 从设计上保证了高性能的集群,客户端-服务器,点对点
  8. 支持Ajax
  9. 支持与Axis的整合
  10. 可以很容易得调用内嵌JMS provider,进行测试

5.2什么情况下使用ActiveMq

  1. 多个项目之间集成 
    (1) 跨平台 
    (2) 多语言 
    (3) 多项目
  2. 降低系统间模块的耦合度,解耦 
    (1) 软件扩展性
  3. 系统前后端隔离 
    (1) 前后端隔离,屏蔽高安全区

Java中关于队列与栈的区别相关推荐

  1. Java中的堆和栈的区别

    当一个人开始学习Java或者其他编程语言的时候,会接触到堆和栈,由于一开始没有明确清晰的说明解释,很多人会产生很多疑问,什么是堆,什么是栈,堆和栈有什么区别?更糟糕的是,Java中存在栈这样一个后进先 ...

  2. JAVA中阻塞队列的类别和区别(转载)

    这篇文章将介绍什么是阻塞队列,以及Java中阻塞队列的4种处理方式,并介绍Java 7中提供的7种阻塞队列,最后分析阻塞队列的一种实现方式. 阻塞队列(BlockingQueue)是一个支持两个附加操 ...

  3. python中的队列和栈_python的队列和栈

    (一)队列和栈的区别 1.队列: 队列是一种特殊的线性表.其两头都有限制,插入只能在表的一端进行(只进不出),而删除只能在表的另一端进行(只出不进),允许删除的一端称为队尾(rear),允许插入的一端 ...

  4. (转)Java中equals和==、hashcode的区别

    背景:学习辉哥总结的基础知识,从头来,直面短板. 1 问题引入及分析 请看下面的代码清单1 @Testpublic void test01() {String a = "a" + ...

  5. java中引用类型和基本类型的区别

    java中引用类型和基本类型的区别 一.数据类型 1.基本类型 基本数据类型只有8种,可按照如下分类 ①整数类型:long.int.short.byte ②浮点类型:float.double ③字符类 ...

  6. java中成员变量和全局变量的区别_成员变量与全局变量的区别

    全局变量:也叫成员变量,是指在类中定义的变量:它在整个类中都有效 全局变量又可分为:类变量和实例变量 1.类变量:又叫静态变量  用static修饰  它可以直接用类名调用  也可以用对象调用   而 ...

  7. Java中实现接口与继承的区别

    ** Java中实现接口与继承的区别 ** 首先,先来了解一下什么是接口和继承.接口一般是使用interface来定义的.接口定义同类的定义类似,分为接口的声明和接口体,其中接口体由常量定义和方法定义 ...

  8. Java中PreparedStatement和Statement的用法区别

    Java中PreparedStatement和Statement的用法区别 (2012-08-01 11:06:44) 转载▼ 标签: 杂谈   1. PreparedStatement接口继承Sta ...

  9. Java中long和Long有什么区别

    Java中long和Long有什么区别(转) Java的数据类型分两种: 1.基本类型:long,int,byte,float,double,char 2. 对象类型(类): Long,Integer ...

最新文章

  1. python-web自动化-Python+Selenium之expected_conditions:各种判断
  2. 如何上传file对象
  3. kernel panic 和 kernel Oops
  4. LVS原理介绍及安装过程
  5. fileinputstream_从Java中的FileInputStream读取字节
  6. java 异常处理的关键字_java异常,异常处理,异常类 关键字:throws 和 throw 自定义的异常类...
  7. Tomcat startup.bat 后台运行,不再弹出 Dos 黑框
  8. C语言基础知识(期末喽)
  9. 基于RV1126平台imx291分析 --- v4l2_pipeline_pm_use
  10. java-多线程编程
  11. Kubelet cAdvisor 资源监控
  12. Matlab查看像素坐标
  13. Adobe Bridge使用教程:BR键盘快捷键大全
  14. 未来发展人工智能的意义是什么?
  15. Android中文件的读写---assets和raw下的资源文件
  16. 1602液晶显示屏显示字符
  17. Android 读取Txt文件内容
  18. 靠股票能改变普通人命运吗?
  19. iOS xxx is missing from working copy
  20. 红米note7找android,红米 NOTE7 PRO简易测评与去广告

热门文章

  1. MQ集群(rabbitMQ)
  2. 一个屌丝程序猿的人生(二十三)
  3. 解析程序化中的机器人的算法写作
  4. 对Java建造者模式(Builder)的一点理解
  5. 手脚老冰凉 妙招来调养
  6. DEAP:使用生理信号进行情绪分析的数据库(三、实验分析与结论)
  7. 橘红色html语言,橘红色调色概念和调配方法!
  8. php搞笑证件,怎么制作搞笑证件 网络搞笑证件制作的软件怎么用的
  9. 以太坊POA共识算法解析
  10. ETH基于POA的环境搭建