juc工具类.md

Posted by     小兵兵 on Wednesday, May 20, 2020

目录

倒计数器

package com.ruoyi.project.juc;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        //倒计数器
        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();
        }
        //倒计数器等待,直到计数器的值减为0,后面的代码才会往下执行
        countDownLatch.await();
        System.out.println("线程"+Thread.currentThread().getName()+"班长关门,最后走人!");
    }
}

循环屏障

package com.ruoyi.project.juc;

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

public class CyclicBarrierTest {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            //屏障线程--->其他线程干活完后执行自己的线程任务  干活
            System.out.println("*************召唤神龙");
        });

        for (int i = 1; i <= 7; i++) {
            int finalI = i;
            new Thread(() -> {
                //其他线程 干活
                System.out.println("线程-"+Thread.currentThread().getName() + "收集龙珠" + finalI);
                try {
                    //其他线程干活完后等待,直到屏障线程设置的大小
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }, String.valueOf(i)).start();
        }
    }
}

信号

package com.ruoyi.project.juc;

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

public class SemaphoreTest {
    public static void main(String[] args) throws InterruptedException {
        //3个资源
        Semaphore semaphore = new Semaphore(3);

        //6个线程
        for (int i = 1; i <= 6; i++) {
            semaphore.acquire();
            new Thread(()->{
                System.out.println("线程"+Thread.currentThread().getName()+"抢占资源");
                try {
                    //睡眠3秒
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //释放资源
                semaphore.release();
            },String.valueOf(i)).start();
        }
    }
}

枚举

package com.ruoyi.project.juc;

public enum EnumTest {
    //声明数据,one--->表  id、name、age--->表的字段
    one(1,"沈利兵",18),two(2,"小兵兵",28),

    three(3,"小红",33),four(4,"小白",34),

    five(5,"小明",20),six(6,"小狗",26);


    int id;
    String name;
    int age;

    EnumTest(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "EnumTest{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
//        System.out.println(EnumTest.five.name);
        EnumTest[] values = EnumTest.values();
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i]);
        }
    }
}