多線的順序控制

基于JOIN

線程自己的邏輯與控制邏輯分離诀紊,執(zhí)行邏輯不受影響巍杈,耦合性最低

        Thread t1 = new Thread(new T1());
        t1.start();
        t1.join();
        Thread t2 = new Thread(new T2());
        t2.start();
        t2.join();
        Thread t3 = new Thread(new T3());
        t3.start();
        t3.join();

基于信號(hào)量

每個(gè)線程需要知道自己運(yùn)行所需要的信號(hào)量闺金,以及開(kāi)啟下一個(gè)線程的信號(hào)量拒课,
有點(diǎn)像 chain 模式狼牺。 執(zhí)行邏輯和控制邏輯有耦合羡儿。


    public static void main(String[] args) throws Exception {

        Semaphore lock1 = new Semaphore(1);
        Semaphore lock2 = new Semaphore(1);
        Semaphore lock3 = new Semaphore(1);
        lock2.acquire();
        lock3.acquire();
        for(int i =0; i < 10; i++) {



            Thread t1 = new Thread(new T1(lock1,lock2));
            Thread t2 = new Thread(new T2(lock2, lock3));
            t2.start();
            Thread t3 = new Thread(new T3(lock3, lock1));
            t3.start();
            t1.start();
        }

    }


    static class T1 implements Runnable{

        public Semaphore nextLock;
        Semaphore current;
        public T1(Semaphore current, Semaphore nextlock){
            this.nextLock=nextlock;
            this.current =current;
        }

        @Override
        public void run() {
            try {
                current.acquire();
                System.out.println("Sleep");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-111");
            nextLock.release();
        }
    }

    static class T2 implements Runnable{
        public Semaphore nextLock;
        Semaphore current;
        public T2(Semaphore current, Semaphore nextlock){
            this.nextLock=nextlock;
            this.current =current;
        }

        @Override
        public void run() {
                try {
                    current.acquire();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-222");
                nextLock.release();
            }
    }

    static class T3 implements Runnable{

        public Semaphore nextLock;
        Semaphore current;
        public T3(Semaphore current, Semaphore nextlock){
            this.nextLock=nextlock;
            this.current =current;
        }

        @Override
        public void run() {
                try {
                    current.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-333");
                nextLock.release();
            }

    }

基于countDownlatch

和信號(hào)量的思路是一樣的,每個(gè)線程需要等待自己 latch是钥,并且完成任務(wù)之后掠归,對(duì)下一個(gè)latch 進(jìn)行 countDown缅叠。

    public static void main(String[] args) throws Exception {


        for(int i =0; i < 1; i++) {


            CountDownLatch c1 = new CountDownLatch(1);
            CountDownLatch c2 = new CountDownLatch(1);

            Thread t1 = new Thread(new T1(c1));
            Thread t2 = new Thread(new T2(c1,c2));
            t2.start();
            Thread t3 = new Thread(new T3(c2));
            t3.start();
            t1.start();
        }

    }


    static class T1 implements Runnable{

        public CountDownLatch c1;
        public T1(CountDownLatch c1){
            this.c1=c1;
        }

        @Override
        public void run() {
            try {
                System.out.println("Sleep");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-111");
            c1.countDown();
        }
    }

    static class T2 implements Runnable{
        public CountDownLatch c1;
        CountDownLatch c2;
        public T2(CountDownLatch c1, CountDownLatch c2){
            this.c1=c1;
            this.c2 =c2;
        }

        @Override
        public void run() {
                try {
                    c1.await();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-222");
                c2.countDown();
            }
    }

    static class T3 implements Runnable{

        CountDownLatch c2;
        public T3(CountDownLatch c2){
            this.c2 =c2;
        }

        @Override
        public void run() {
                try {
                    c2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("I am T-333");
            }

    }

基于條件變量

條件變量是lock+ condition, 是最接近用 notify+wait 來(lái)實(shí)現(xiàn)的方式虏冻。
每個(gè)lock 可以穿件多個(gè)條件變量肤粱, 每個(gè) 條件變量 await 都能夠釋放lock。
但是condition 的singal 方法可以精確的喚醒等待的線程厨相。這是條件變量現(xiàn)對(duì)于notify的優(yōu)勢(shì)领曼,可以更加精準(zhǔn)的控制。


    public static void main(String[] args) throws Exception {


        Lock lock = new ReentrantLock();
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();

        Thread t1 = new Thread(new T1(lock, condition1));
        Thread t2 = new Thread(new T2(lock, condition1,condition2));
        t2.start();
        Thread t3 = new Thread(new T3(lock, condition2,null));
        t3.start();
        t1.start();

    }


    static class T1 implements Runnable{

        public Lock lock;
        Condition nextCondition;
        public T1(Lock lock, Condition nextCondition){
            this.lock = lock;
            this.nextCondition =nextCondition;
        }

        @Override
        public void run() {
            try {
                lock.lock();
                System.out.println("Sleep");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-111");
            nextCondition.signal();
            lock.unlock();
        }
    }

    static class T2 implements Runnable{

        public Lock lock;
        Condition nextCondition;
        Condition currentCondition;
        public T2(Lock lock, Condition currentCondition,Condition nextCondition){
            this.lock = lock;
            this.nextCondition =nextCondition;
            this.currentCondition = currentCondition;
        }

        @Override
        public void run() {

            lock.lock();
            try {
                currentCondition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-222");
            nextCondition.signal();
            lock.unlock();
        }
    }

    static class T3 implements Runnable{

        public Lock lock;
        Condition nextCondition;
        Condition currentCondition;
        public T3(Lock lock, Condition currentCondition,Condition nextCondition){
            this.lock = lock;
            this.nextCondition =nextCondition;
            this.currentCondition = currentCondition;
        }

        @Override
        public void run() {

            lock.lock();
            try {
                currentCondition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("I am T-333");
            lock.unlock();
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蛮穿,一起剝皮案震驚了整個(gè)濱河市庶骄,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌践磅,老刑警劉巖单刁,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異府适,居然都是意外死亡羔飞,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門檐春,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)褥傍,“玉大人,你說(shuō)我怎么就攤上這事喇聊』蟹纾” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵誓篱,是天一觀的道長(zhǎng)朋贬。 經(jīng)常有香客問(wèn)我,道長(zhǎng)窜骄,這世上最難降的妖魔是什么锦募? 我笑而不...
    開(kāi)封第一講書人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮邻遏,結(jié)果婚禮上糠亩,老公的妹妹穿的比我還像新娘。我一直安慰自己准验,他們只是感情好赎线,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著糊饱,像睡著了一般垂寥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,443評(píng)論 1 302
  • 那天滞项,我揣著相機(jī)與錄音狭归,去河邊找鬼。 笑死文判,一個(gè)胖子當(dāng)著我的面吹牛过椎,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播戏仓,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼疚宇,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了柜去?” 一聲冷哼從身側(cè)響起灰嫉,我...
    開(kāi)封第一講書人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤拆宛,失蹤者是張志新(化名)和其女友劉穎嗓奢,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體浑厚,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡股耽,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钳幅。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片物蝙。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖敢艰,靈堂內(nèi)的尸體忽然破棺而出诬乞,到底是詐尸還是另有隱情,我是刑警寧澤钠导,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布震嫉,位于F島的核電站,受9級(jí)特大地震影響牡属,放射性物質(zhì)發(fā)生泄漏票堵。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一逮栅、第九天 我趴在偏房一處隱蔽的房頂上張望悴势。 院中可真熱鬧,春花似錦措伐、人聲如沸特纤。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)叫潦。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間矗蕊,已是汗流浹背短蜕。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留傻咖,地道東北人朋魔。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像卿操,于是被迫代替她去往敵國(guó)和親警检。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容