一段简单代码理解异步请求消息队列在高并发环境中的作用

来源:互联网 发布:js多文件上传 编辑:程序博客网 时间:2024/05/22 06:15

普通同步方法被调用模拟

package top.yuyufeng.mq;import java.util.Random;import java.util.concurrent.atomic.AtomicInteger;/** * Created by yuyufeng on 2017/8/29. */public class MyServer {    private static AtomicInteger atomicInteger = new AtomicInteger(1);    public static void createOrder(String product) {        int res = atomicInteger.getAndIncrement();        if (res >= 10) {            System.err.println("超过当前容量(10) " + res);        }        System.out.println(product + "当前正在处理的线程数 " + res);        doJob();        atomicInteger.getAndDecrement();    }    private static void doJob() {        Random random = new Random();        long sleepTime = random.nextInt(10) * 100;        try {            Thread.sleep(sleepTime);        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        //模拟发起请求  并发一旦上去,就会超过限制大小,有宕机危险        for (int i = 0; i < 12; i++) {            new Thread() {                @Override                public void run() {                    for (int j = 0; j < 10; j++) {                        MyServer.createOrder(Thread.currentThread().getName());                    }                    System.out.println(Thread.currentThread().getName() + "结束:" + atomicInteger);                }            }.start();        }    }}

运行这段代码之后,你会看到,并发一单上去,当前处理的进程会超过额定容量(这里设置为最高能应对10个进程同时处理,超过会卡顿)

应用消息队列后,变成异步请求

package top.yuyufeng.mq;import java.util.Queue;import java.util.Random;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.atomic.AtomicInteger;/** * Created by yuyufeng on 2017/8/29. */public class MyServerByQuene {    private static AtomicInteger atomicInteger = new AtomicInteger();    //这里使用jdk队列,代替常用的消息队列服务    private static Queue<String> queue = new ConcurrentLinkedQueue<>();    private static ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(10);    public static void createOrder(String product) {        queue.add(product);    }    static {        //10个进程阻塞处理请求        for (int i = 0; i < 10; i++) {            threadPoolExecutor.execute(new Runnable() {                @Override                public void run() {                    while (true) {                        String product = queue.poll();                        if (product != null) {                            int res = atomicInteger.getAndIncrement();                            if (res >= 10) {                                System.err.println("超过当前容量(10) " + res);                            }                            System.out.println(product + "当前正在处理的线程数 " + res);                            doJob();                            atomicInteger.getAndDecrement();                        } else {                            try {                                Thread.sleep(1000);                                System.out.println("MyServerByQuene.run 正在监听...当前进程数量: " + atomicInteger);                            } catch (InterruptedException e) {                            }                        }                    }                }            });        }    }    private static void doJob() {        Random random = new Random();        long sleepTime = random.nextInt(10) * 100;        try {            Thread.sleep(sleepTime);        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public static void main(String[] args) {        //模拟发起请求,这里的并发更大 ,但是是异步消息队列,会等待处理.能抗住高并发        for (int i = 0; i < 20; i++) {            new Thread() {                @Override                public void run() {                    for (int j = 0; j < 10; j++) {                        MyServerByQuene.createOrder(Thread.currentThread().getName() + j);                    }                    System.out.println(Thread.currentThread().getName() + "结束:" + atomicInteger);                }            }.start();        }    }}

运行之后,你会发现,无论多少个线程去请求,总的正在处理线程的数量不会变.

这就是消息队列,异步请求在高并发环境下所起的作用

阅读全文
1 0
原创粉丝点击