并發(fā)模擬的四種方式
點(diǎn)擊上方“碼農(nóng)突圍”,馬上關(guān)注
這里是碼農(nóng)充電第一站,回復(fù)“666”,獲取一份專屬大禮包
真愛,請(qǐng)?jiān)O(shè)置“星標(biāo)”或點(diǎn)個(gè)“在看

一、Postman

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~";
}
}















進(jìn)入下載頁面 選擇適合自己電腦的版本







httpd.exe -k install

httpd.exe -k start
-c: 并發(fā)數(shù)
















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++;
}
}
上面是對(duì)代碼的并發(fā)模擬的簡(jiǎn)單形式,值得注意的是,這里提到的兩個(gè)類不是專門做并發(fā)模擬,它們的用途很廣泛,等之后更新Java網(wǎng)絡(luò)編程的東西的時(shí)候,還會(huì)詳細(xì)介紹它們。
-End-
最近有一些小伙伴,讓我?guī)兔φ乙恍?nbsp;面試題 資料,于是我翻遍了收藏的 5T 資料后,匯總整理出來,可以說是程序員面試必備!所有資料都整理到網(wǎng)盤了,歡迎下載!
點(diǎn)擊??卡片,關(guān)注后回復(fù)【面試題】即可獲取
評(píng)論
圖片
表情

