多线程并发常用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();
        }
    }
}
原创文章 28 获赞 26 访问量 3364

猜你喜欢

转载自blog.csdn.net/Avril___/article/details/105697560