[Concurrent programming - foundation] (b) concurrent simulation

A concurrent simulation tool

1.1、Postman

Http request Simulator
Postman64 bit installation package
Postman32 bit installation package

2.1、Apache Bench (AB)

Apache comes with a tool to test site performance (command-line tool, without graphical interface, the most commonly used)

3.1、JMeter

Apache organization to develop stress testing tool (more powerful than AB, need to be installed)
apache-jmeter-5.2.1_src.zip

Second, concurrent simulation code

2.1, concurrent simulation code, under the code does not have atomic count ++, thread-safe
package com.tangxz._3test;

import com.tangxz.annoations.NotThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @Info: 测试不具有原子性的int++操作
 * @Author: 唐小尊
 * @Date: 2019/12/26  15:17
 */
@Slf4j
@NotThreadSafe
public class ConcurrencyTest {
    //请求总数
    public static int clientTotal = 5000;
    //同时并发执行的线程数
    public static int threadTotal = 200;
    //计数的值
    public static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        //信号量
        final Semaphore semaphore = new Semaphore(threadTotal);
        //计数器,统计计数结果
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        //发送请求
        for (int i = 0; i < clientTotal; i++) {
            //lambda表达式
            executorService.execute(() -> {
                try {
                    //引入信号量,不报错才执行add,获取许可
                    semaphore.acquire();
                    add();
                    //释放进程,释放许可
                    semaphore.release();
                } catch (Exception e) {
                    log.error("exception", e);
                }
                //执行一次就减一一次
                countDownLatch.countDown();
            });
        }
        //保证countDown减为0(执行完成)
        countDownLatch.await();
        //{}代表的是后面的那个值
        log.info("conut:{}", count);
    }

    public static void add() {
        count++;
    }
}
2.2、CountDownLatch

       Counter, initialization of the incoming initial value, when a thread is executed once after the implementation of the countDown () method, which has atomic performed once, the initial value is -1, and finally judged by the await () method and the blocking process, when the value of the counter countDownLatch equal to 0 if allowed by the process, or wait.

2.3、Semaphore

       Counting semaphore, initialized when you can pass a value, while allowing only representative of how many threads through each semaphore must be released by the acquiring its thread, often used to limit the number of threads that can access certain resources, such as by Semaphore limit flow. Semaphore only three operations, initialization, increase, decrease.

Published 20 original articles · won praise 1 · views 565

Guess you like

Origin blog.csdn.net/weixin_42295814/article/details/103770183