并发模拟的四种方式
程序员的成长之路
共 6212字,需浏览 13分钟
·
2022-07-13 02:23
阅读本文大概需要 4.5 分钟。
来自:网络
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestConrtoller {
@GetMapping("demo")
public String testDemo() {
return "result~";
}
}
进入下载页面 选择适合自己电脑的版本
httpd.exe -k install
httpd.exe -k start
-c: 并发数
public CountDownLatch(int count) { };
public void await() throws InterruptedException { };
public boolean await(long timeout, TimeUnit unit) throws InterruptedException { };
public void countDown() { };
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.*;
@Slf4j
public class CuncurrencyTest {
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++) {
executorService.execute(() -> {
try {
semaphore.acquire();
add();
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
log.error("exception",e);
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
log.info("count:{}",count);
}
private static void add() {
count++;
}
}
推荐阅读:
互联网初中高级大厂面试题(9个G) 内容包含Java基础、JavaWeb、MySQL性能优化、JVM、锁、百万并发、消息队列、高性能缓存、反射、Spring全家桶原理、微服务、Zookeeper......等技术栈!
⬇戳阅读原文领取! 朕已阅
评论