java并发模拟——多线程计数
java并发模拟——多线程计数
最近在学习多线程与高并发的知识,这是一个能力进阶的必要途径。在大量的系统中,都会多多少少存在并发问题,如何更好的解决高并发是一个探究的问题。
下面我准备了一个简单的多线程计数demo来模拟并发操作,观察打印输出情况,真正的去感受一下并发操作。
首先环境搭建,我用的是springboot,在spring官网或者idea中初始化一个maven项目生成项目之后,先配置pom.xml,添加相关的依赖,pom.xml:
重点是添加了lombok依赖,有关日志打印的,其他没有的自行加入。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>concurrency</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>concurrency</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后我们在看主函数,新建一个类ConcurrencyTest,引入Slf4j注解,定义线程池,信号量,初始计数,日志,请求总数,一次请求允许的并发数(即线程数)等。每一个请求时,所有线程同时执行add操作,我们来看最后会执行多少次。代码如下:
package com.example.concurrency;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
@Slf4j
public class ConcurrencyTtest {
//请求总数
public static int clientTotal=5000;
//同时并发执行的线程数
public static int threadTotal=200;
//一开始计数为0
public static int count=0;
//定义一个日志
private static Logger log= LoggerFactory.getLogger(ConcurrencyTtest.class);
public static void main(String []args) throws InterruptedException {
//定义线程池
ExecutorService executorService= Executors.newCachedThreadPool();
//定义一个threadTotal的信号量
final Semaphore semaphore=new Semaphore(threadTotal);
//定义一个计数递减锁
final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);
for(int i=0;i<clientTotal;i++) {
executorService.execute(() -> {
try {
semaphore.acquire();
add();
semaphore.release();
} catch (Exception e) {
log.error("exception",e);
}
//
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
log.error("count:{}"+count);
}
//加1操作
public static void add(){
count++;
}
}
我们来执行一下结果(连续执行三次,看结果)
我们发现,每次结果都不同,正常情况下,我们想如果只有一个进程,那麽结果肯定是5000,不会出现错误情况。这就是当线程数增多,并发量增多,导致程序出错,系统出错。在实际的大型项目中,这种情况经常出现,我们也要不断的去应付高并发的情况,提出相应的解决方案。后续的我会不断给出高并发的相关知识以及解决方案。