项目采用springboot基础框架快速开发,具体初始化方法不再赘述,不明白的可以看课程或者自行百度
1. 一些注解的准备
线程安全注解
importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;/**
* 对于线程安全的类,加入一个@ThreadSafe注解的标示
* @Target(ElementType.TYPE) 说明作用于类上
* @Retention(RetentionPolicy.SOURCE) 指定注解作用的范围,在编译的时候就会被忽略掉
* @author gaowenfeng
*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.SOURCE)public @interface ThreadSafe {Stringvalue()default"";}
线程不安全注解
importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;/**
* 用来标示[线程不安全的类]
* @Target(ElementType.TYPE) 说明作用于类上
* @Retention(RetentionPolicy.SOURCE) 指定注解作用的范围,在编译的时候就会被忽略掉
* @author gaowenfeng
*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.SOURCE)public @interface NotThreadSafe {Stringvalue()default"";}
推荐写法注解
/**
* 用来标记[推荐]的类或者写法
*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.SOURCE)public@interfaceRecommend {Stringvalue()default"";}
不推荐写法注解
/**
* 用来标记[不推荐]的类或者写法
*/@Target(ElementType.TYPE)@Retention(RetentionPolicy.SOURCE)public@interfaceNotRecommend {Stringvalue()default"";}
ElementType 详解
publicenum ElementType {/** Class, interface (including annotation type), or enum declaration *//** 声明注解作用在类,借口,枚举上*/TYPE,/** Field declaration (includes enum constants) *//** 声明注解作用在属性上*/FIELD,/** Method declaration *//** 声明注解作用在方法上*/METHOD,/** Formal parameter declaration *//** 声明注解作用在参数上*/PARAMETER,/** Constructor declaration *//** 声明注解作用在构造函数上*/CONSTRUCTOR,/** Local variable declaration *//** 声明注解作用在本地变量上*/LOCAL_VARIABLE,/** Annotation type declaration *//** 声明注解作用在注解上*/ANNOTATION_TYPE,/** Package declaration *//** 声明注解作用在包上*/PACKAGE,/** * Type parameter declaration * *@since1.8 *//** 声明注解可以应用在TYPE声明上*/TYPE_PARAMETER,/** * Use of a type * Type.TYPE_USE 表示这个 Annotation 可以用在所有使用 Type 的地方(如:泛型,类型转换等) *@since1.8 */TYPE_USE}
RetentionPolicy详解
publicenumRetentionPolicy {/**
* Annotations are to be discarded by the compiler.
* 在编译的时候会被取消,只用于声明,理解,或者测试
*/SOURCE,/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
* 注解将被编译器记录在类文件中,但在运行时不需要由VM保留,(默认的选项)
*/CLASS,/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
* 注解将被编译器记录在类文件中,但在运行时由VM保留,这样他们可以被反射获取(当你需要获取注解中字段的属性值的时候,需要用这个,比如AOP)
* @see java.lang.reflect.AnnotatedElement
*/RUNTIME}
etentionPolicy.SOURCE仅仅是给应用层开发人员用的,RetentionPolicy.CLASS 需要应用层和底层系统开发人员配合使用的,所以仅仅是应用层开发的我们是一脸懵逼,意识CLASS和SOURCE的区别。(具体参考)
2.并发模拟
并发模拟工具

并发模拟工具
Postman:Http请求模拟工具

postman
Apache附带的工具

AB
# -n 总请求数# -c 并发数$ ab -n1000-c50http://localhost:8080/testThis is ApacheBench, Version2.3<$Revision:1807734$>Copyright1996Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking localhost (be patient)Completed100requestsCompleted200requestsCompleted300requestsCompleted400requestsCompleted500requestsCompleted600requestsCompleted700requestsCompleted800requestsCompleted900requestsCompleted1000requestsFinished1000requestsServer Software:Server Hostname: localhostServer Port:8080Document Path: /testDocument Length:4bytes# 并发量Concurrency Level:50# 整个测试耗时Time takenfortests:0.285seconds# 完成的请求数Complete requests:1000# 失败的请求数Failed requests:0# 所有请求的相应数据的长度综合(HTTP相应数据的 头信息+正文数据的长度)(不包括请求的长度)Total transferred:136000bytes# 所有请求的相应数据中,正文相应的综合HTML transferred:4000bytes# 吞吐率(与并发数相关)(Complete requests/0.285 seconds)Requests per second:3505.09[#/sec] (mean)# 用户平均请求等待时间Time per request:14.265[ms] (mean)# 服务器平均请求等待时间Time per request:0.285[ms] (mean, across all concurrent requests)# 这些请求在单位时间内从服务器获取的数据长度(Total transferred/Time taken for tests)Transfer rate:465.52[Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect:052.7513Processing:195.0837Waiting:164.0636Total:2145.01440Percentage of the requests served within a certain time (ms)50%1466%1575%1680%1790%1995%2198%3099%34100%40(longest request)
JMeter

JMeter

创建线程组

创建HTTP请求

创建监视器

log
并发模拟-代码

CountDownLatch

Semaphore
importcom.gwf.concurrency.annoations.NotThreadSafe;importlombok.extern.slf4j.Slf4j;importjava.util.concurrent.*;/**
* 并发测试
* @author gaowenfeng
*/@Slf4j@NotThreadSafepublicclassConcurrencyTest{/** 请求总数 */publicstaticintclientTotal =5000;/** 同时并发执行的线程数 */publicstaticintthreadTotal =50;publicstaticintcount =0;publicstaticvoidmain(String[] args)throws InterruptedException{// 创建线程池ExecutorService executorService =newThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,newSynchronousQueue<>(), r ->newThread(r,"测试线程"));// 信号量,闭锁final Semaphore semaphore =newSemaphore(threadTotal); final CountDownLatch countDownLatch =newCountDownLatch(clientTotal);// 模拟并发请求for(inti =0; i < clientTotal; i++) { executorService.execute(()->{try{// 请求一个信号,如果信号量小于clientTotal,则阻塞semaphore.acquire(); add();// 释放一个信号semaphore.release(); }catch(InterruptedException e) {log.error("exception",e); } countDownLatch.countDown(); }); }// 阻塞直到countDown 的次数为threadTotalcountDownLatch.await();// 关闭线程池executorService.shutdown();log.info("count:{}",count); }/**
* 本质上应该是这个方法线程不安全
*/privatestaticvoidadd(){ count++; }}
作者:Meet相识_bfa5
链接:https://www.jianshu.com/p/8dfe542ce22c
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友评论