线程运行时通过JUC获取锁的小工具

/**
 * 可重入锁,synchronized和reentrantLock 都是可重入锁
 * synchronized不需要手动解锁 异常或者执行完自动释放,reentrantLock需要手动解锁
 * reentrantLock可以设置公平非公平并且可以获取锁的状态尝试获取锁并且可以打断
 * 一个是JVM锁对象  一个是JUC的工具  底层采用的cas
 */
public class ReentralLoackDemp {

    private int count = 0;

    private ReentrantLock lock = new ReentrantLock(true);

    void m() {
        for (int i = 0; i < 10; i++) {
            lock.lock();
            try {
                this.count++;
                System.out.println(Thread.currentThread().getName() + ":执行了");
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReentralLoackDemp demp = new ReentralLoackDemp();
        Thread[] threads = new Thread[10];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(demp::m, "thrad" + i);
        }
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println(demp.count);
    }
}


/**
 * 读共享锁  读线程锁允许其他读线程继续读取
 * 写排他锁  写线程的锁不允许其他线程操作。 排他锁又叫独占锁
 * 和数据库锁不一样, 虽然概念差不多 但是一个是数据库一个是操作内存中两者没有关系
 */
public class TestReadWriteLock {

    //可重入锁
    private static ReentrantLock lock = new ReentrantLock();
    private static int count = 0;

    //读写锁
    static ReadWriteLock readWriterLock = new ReentrantReadWriteLock();
    static Lock read;
    static Lock write;
    static {
        read = readWriterLock.readLock();
        write = readWriterLock.writeLock();
    }

    public static void read(Lock lock){
        lock.lock();
        try {
            Thread.sleep(1000);
            System.out.println("reader..");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void write(Lock lock, int i){
        lock.lock();
        try {
            Thread.sleep(1000);
            System.out.println("write, " + i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {

        for (int i = 0; i < 15; i++) {
//            new Thread(()-> read(lock)).start();
            new Thread(()-> read(read)).start();
        }

        for (int i = 0; i < 2; i++) new Thread(()-> write(write, 1)).start();
    }
}


/**
 * 倒数计数器 和 join功能差不多 相当于一个计数小工具
 */
public class CountDownLatchDemo {

    static CountDownLatch countDownLatch = new CountDownLatch(10);

    void m (){
        System.out.println(Thread.currentThread().getName() + ":在执行");
        countDownLatch.countDown();
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatchDemo countDownLatchDemo = new CountDownLatchDemo();
        System.out.println("所有线程都在执行中。。等待执行完毕");
        for (int i = 0; i < 10; i++) {
            new Thread(countDownLatchDemo::m, "t"+i).start();
        }

        countDownLatch.await();
        System.out.println("所有的都已经执行完毕");
    }

}

/**
 * CyclicBarrier是栅栏操作,await()方法调用一次计数加1,只有当满了20之后 才会执行构造方法的后续操作。
 */
public class CyclicBarrierTest {

    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(20, ()->{
        System.out.println("满了20个人  发车啦!");
    });

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(()-> {
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }
}


public class TestSemaphore {

    public static void main(String[] args) {
        //可以同时允许几个线程同时执行。
        Semaphore semaphore = new Semaphore(2);

        new Thread(()-> {
            try {
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName()+ "当前线程执行之前。。");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            semaphore.release();
            System.out.println(Thread.currentThread().getName()+ "当前线程执行之后。。");
        }, "t1").start();

        new Thread(()-> {
            try {
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName()+ "当前线程执行之前。。");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            semaphore.release();
            System.out.println(Thread.currentThread().getName()+ "当前线程执行之后。。");
        }, "t2").start();

    }
}


还有phaser类和exchanger类

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值