栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多线程与高并发基础总结-juc中的锁

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多线程与高并发基础总结-juc中的锁

JUC就是java.util.concurrent工具包的简称

Reentrantlock

代码

public class ReentrantlockTest {

    public static void main(String[] args) {
        AtomicInteger a= new AtomicInteger(10);
//        1 可以替代 synchronized
 //        2 需要自己加锁和解锁
        ReentrantLock reentrantLock=new ReentrantLock();
        new Thread(()->{
            try {   reentrantLock.lock();


            } catch (Exception e) {
                e.printStackTrace();
            }finally {
              reentrantLock.unlock();
            }
            System.out.println("=======");}).start();
        
//        3 底层是aqs实现、 同时他比synchronized 多喝很多灵活的方法  并且是可以在加锁的过程中被打断的 底层实现也不一致 synchronized 是升级锁的 
        
    }
}

底层是AQS 是一种公平锁 当然 默认是flase。同时这里的公平是指的 有一个队列 线程进来等待锁的释放。资源可以使用了 那么队列里先进来的 进进行资源的占用

countDownLatch

代码

public class countDownTest {

    public static void main(String[] args) {
        // 比如我现在要团购
        CountDownLatch countDownLatch = new CountDownLatch(50);
        for (int i = 0; i < 80; i++) {
            System.out.println(i);
            new Thread(() -> {
                System.out.println("我要抢购");
                countDownLatch.countDown();
                try {
                    countDownLatch.await();
                    System.out.println("ok");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }

    }
}

结果如图 在拦截到50 后 后面的ok 来开始打印

cyclicbarrier
public class CyclicbarrierTest {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier=new CyclicBarrier(10,()-> {System.out.println("开团");});
        for (int i = 0; i < 20; i++) {
            System.out.println(i);
            new Thread(()->{
                try {
                    cyclicBarrier.await();
                }catch (Exception e){
                    e.printStackTrace();
                }

            }).start();
        }
    }
}

结果如图 拦住后打印

Exchanger

线程之间通信

public class ExchangerTest {

    public static void main(String[] args) {
        Exchanger exchanger = new Exchanger();
        new Thread(() -> {
            String name = "1";
            try {
               name=(String) exchanger.exchange(name);
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("1:" + name);
        }).start();
        new Thread(() -> {
            String name = "2";
            try {
               name= (String) exchanger.exchange(name);
            } catch (Exception e) {
                e.printStackTrace();
            }

            System.out.println("2:" + name);
        }).start();
    }
}

结果

locksupport

可以指定现场被唤醒 可以提前声明不堵塞

代码

public class locksupportTest {
    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("park" + i);
                if (i == 5) {
                    LockSupport.park();
                    System.out.println("parkTest01");

                }
            }
        });
        t1.start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("park" + i);
                if (i == 5) {
                    LockSupport.unpark(t1);
                    System.out.println("parkTest02");

                }
            }
        }).start();


    }
}

结果

ReentrantReadWriteLock

读写锁 其实是共享锁和排他锁的结合 读共享 写排他 都是读的话可以进来 写的话就在外面等待 如果当前线程是写的话 都在外面等待

代码

public class ReadWirteLockTest {



    public static void main(String[] args) {
        int count = 0;
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
        ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();
        ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();
        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                try {

                    readLock.lock();
                   System.out.println(System.currentTimeMillis());
                   TimeUnit.SECONDS.sleep(1);
                    System.out.println("read" + count);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    readLock.unlock();
                }
            }).start();
            new Thread(()->{
                try {

                    writeLock.lock();
                    System.out.println(System.currentTimeMillis());
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("write"+count);
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    writeLock.unlock();
                }
            }).start();

        }

    }
}

细看结果 看读的时间戳。可以证明读共享 写独享

semaphore
public class semaphoreTest {
    public static void main(String[] args) {
        Semaphore semaphore =new Semaphore(2);
        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            new Thread(()->{
              try {

                  semaphore.acquire();
                  System.out.println("2 ge ");
                  semaphore.release();
              }catch (Exception e){
                  e.printStackTrace();
              }finally {
                  semaphore.release();
              }
            }).start();

        }
    }
}

设置容量 2 第三个的时候 一起执行 再释放

phaser 阶段性的

明日搬砖不忙补 继承后重写onadvance 方法 执行advance 结尾方法表示阶段结束 register 结尾表示线程不进入下一阶段

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/832256.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号