多線程併發常用API countDownlatch syslicbarrier semaphore

CountDownLatch
在這裏插入圖片描述
demo1

火箭發射10 9 80
await方法將會執行,直到計數到0
package com.wsx.countDown;

import java.util.concurrent.CountDownLatch;

public class CountDownDemo {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"號同學離開");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }

        try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); }

        System.out.println(Thread.currentThread().getName()+"班長鎖門");
    }
}

demo2

package com.wsx.countDown;

import java.util.concurrent.CountDownLatch;

public class CountDownDemo2 {
    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"國被滅");
                countDownLatch.countDown();
            },CountryEnum.findCountry(i).getRetName()).start();
        }

        try { countDownLatch.await(); } catch (InterruptedException e) { e.printStackTrace(); }

        System.out.println(Thread.currentThread().getName()+"秦國贏");
    }
}
分界線------------------------------------------
package com.wsx.countDown;

public enum  CountryEnum {
    ONE(1,"齊"),TWO(2,"楚"),THREE(3,"燕"),FOUR(4,"漢"),FIVE(5,"趙"),SIX(6,"魏");
    private Integer retCode;
    private String retName;

    public Integer getRetCode() {
        return retCode;
    }

    public void setRetCode(Integer retCode) {
        this.retCode = retCode;
    }

    public String getRetName() {
        return retName;
    }

    public void setRetName(String retName) {
        this.retName = retName;
    }

    CountryEnum(Integer retCode, String retName) {
        this.retCode = retCode;
        this.retName = retName;
    }
    public static CountryEnum findCountry(int index){
        CountryEnum[] values = CountryEnum.values();
        for (CountryEnum element:values) {
            if(index == element.getRetCode()){
                return element;
            }
        }
        return null;
    }
}

CyclicBarrier
在這裏插入圖片描述
內存屏障點
demo

package com.wsx.cyclicBarrier;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("召喚神龍");
        });

        for (int i = 1; i <= 7; i++) {
            int tempint = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"第"+tempint+"龍珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}

Semaphore
在這裏插入圖片描述
場景:秒殺,電商併發,微信紅包

多個線程搶多個資源

package com.wsx.semaPhore;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaPhoreDemo {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        for (int i = 1; i <= 4; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"號停車");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread().getName()+"號離開");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            },String.valueOf(i)).start();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章