午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

java - 控制并發線程數

瀏覽:186日期:2024-02-14 09:11:02

問題描述

問題解答

回答1:

如果你了解信號量的實現機制,那么這道題目也是一個意思。

public class Test { private final Integer maxCounter = 3; private Integer current = 0; public void call1() {//在這里補充代碼synchronized (this) { try {while (current.equals(maxCounter)) { // 請求 到達上限 wait();} } catch (InterruptedException ex) { } current++; notifyAll();}call2(current);synchronized (this) { try {while (current == 0) { wait();} } catch (InterruptedException ex) { } current--; notifyAll();} } private void call2(Integer current) {System.out.println(Thread.currentThread().getName() + ': I’m called ' + current);// 下面的休眠 2 秒鐘用于測試try { Thread.sleep(2000);} catch (InterruptedException ex) { ex.printStackTrace(System.err);} } static class TestThread implements Runnable {private Test t;public TestThread(Test t) { this.t = t;}@Overridepublic void run() { t.call1();} } public static void main(String[] args) {Test t1 = new Test();TestThread tt = new TestThread(t1);for (int i = 0; i < 10; i++) { Thread t = new Thread(tt, 'Thread-' + i); t.start();} }}

運行這段代碼,你可以發現每 2 秒內,最多只有 3 (maxCounter)個線程在運行。

回答2:

用CountDownLatch。。。

標簽: java
相關文章:
主站蜘蛛池模板: 超碰久操 | 天天干天天弄 | 自拍一级片 | 国产免费不卡 | 欧美韩一区二区 | 日本精品久久久久久 | 美日韩视频| 久久欧洲 | 伊人综合视频 | 97中文在线| 欧美大片免费看 | 欧美一级啪啪 | 久久99久久99精品免费看小说 | 美女在线播放 | a天堂在线观看视频 | 国产精品乱 | 久99热| 中国三级视频 | 国产女人18毛片水18精品 | 久久久久久久久99 | 精品一区二区三区在线观看视频 | 日韩中文字幕久久 | 国产精品私拍 | 啪啪福利社 | 国产精品自拍偷拍 | 强开乳罩摸双乳吃奶羞羞www | 亚洲成人资源 | 国产精品久久久久桃色tv | 亚洲视频99 | 成人自拍一区 | 一级免费大片 | 亚洲福利小视频 | 国产精品热久久 | 午夜爽爽| 免费观看视频在线观看 | 深夜小视频在线观看 | 伊人激情 | 亚洲成年人网站在线观看 | 99在线免费观看视频 | 亚洲伊人网站 | 欧美午夜一区二区 |