JUC并发工具使用案例

包含以下并发工具的调用
synchronized 同步锁
ReentrantLock 同步锁
Condition 线程通讯
ReentrantReadWriteLock 读写锁 读写分离
StampedLock 读写锁 (悲观 乐观) 读的同时可以写
AtomicInteger 原子性递增
LongAdder 分段式锁 (分段累加 最后汇总)
CountDownLatch 同步计数器
CyclicBarrier 循环栅栏
Phaser 阶段器 对所有注册的线程内进行分段阻塞
Semaphore 计数信号器 (限流作用)
Exchanger 交换器
LockSupport 控制线程阻塞与唤醒

先写个Main方法及主方法接口

public class ThreadMain {
    public static void main(String[] args) throws Exception {
//        MainInterface mainInterface = new SynClass();  //synchronized 同步锁
//        MainInterface mainInterface = new ReentrantLockClass();  //ReentrantLock 同步锁
//        MainInterface mainInterface = new ConditionClass();  //Condition 线程通讯
//        MainInterface mainInterface = new ReadWriteLockClass();  //ReentrantReadWriteLock 读写锁  读写分离
//        MainInterface mainInterface = new StampedLockClass();  //StampedLock 读写锁 (悲观 乐观)  读的同时可以写
//        MainInterface mainInterface = new AtomicIntClass();  //AtomicInteger 原子性递增
//        MainInterface mainInterface = new LongAdderClass();  //LongAdder 分段式锁 (分段累加 最后汇总)
//        MainInterface mainInterface = new CountDownClass();  //CountDownLatch 同步计数器
//        MainInterface mainInterface = new CyclicBarrierClass();  //CyclicBarrier 循环栅栏
//        MainInterface mainInterface = new PhaserClass(); //  阶段器  类似于循环栅栏 但是可以更精确的分段控制
//        MainInterface mainInterface = new SemaphoreClass(); //计数信号器 (限流作用)
//        MainInterface mainInterface = new ExchangerClass();
//        MainInterface mainInterface = new LockSupportClass();
//        mainInterface.mainMethod();

    }

}

interface MainInterface {
    void mainMethod() throws Exception;
}

synchronized 同步锁


class SynClass implements MainInterface {

    Object o = new Object();
    int count = 0;

    @Override
    public void mainMethod() throws Exception {
        long startTime =System.currentTimeMillis();
        Thread[] threads = new Thread[1000];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i=0;i<10000;i++) {
                            synchronized (o){
                                count++;
                            }
                        }
                    }
            });
        }
        for (Thread thread: threads) {
            thread.start();
        }
        for (Thread thread: threads) {
            thread.join();
        }


        long endTime =System.currentTimeMillis();
        System.out.println("synchronized :" + count + " 消耗时间:" + (endTime - startTime) + "ms");
    }
}

ReentrantLock 同步锁 与synchronized 类似,需要手动加锁解锁

class ReentrantLockClass implements MainInterface {

    int count = 0;

    ReentrantLock reentrantLock = new ReentrantLock();
    @Override
    public void mainMethod() throws Exception {
        long startTime =System.currentTimeMillis();
        Thread[] threads = new Thread[1000];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<10000;i++) {
                        try {
                            reentrantLock.lock();
                            count++;
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (reentrantLock!=null) {
                                reentrantLock.unlock();
                            }
                        }
                    }
                }
            });
        }
        for (Thread thread: threads) {
            thread.start();
        }
        for (Thread thread: threads) {
            thread.join();
        }


        long endTime =System.currentTimeMillis();
        System.out.println("synchronized :" + count + " 消耗时间:" + (endTime - startTime) + "ms");
    }
}

Condition 线程通讯
通过lock.newCondition()实例化不同的Condition ,对不同的Condition 进行阻塞、唤醒操作,实现线程通讯

class ConditionClass implements MainInterface {
    Lock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();

    @Override
    public void mainMethod() throws Exception {

        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                System.out.println("线程1等待");
                try {
                    condition1.await();
                    System.out.println("线程1继续");
                    condition2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                lock.unlock();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                System.out.println("线程2等待");
                try {
                    condition1.signal();
                    condition2.await();
                    System.out.println("线程2继续");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                lock.unlock();
            }
        }).start();

    }
}

ReentrantReadWriteLock 读写锁 读写分离,可以同时读,写互斥

class ReadWriteLockClass implements MainInterface {
    String msg = "origin";
    ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    @Override
    public void mainMethod() throws Exception {
        Thread[] reads = new Thread[10];
        for (int i=0;i<reads.length;i++) {
            reads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    readWriteLock.readLock().lock();
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("R" +msg);
                    readWriteLock.readLock().unlock();
                }
            });
        }

        Thread[] writes = new Thread[5];
        for (int i=0;i<writes.length;i++) {
            writes[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    readWriteLock.writeLock().lock();
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    msg +="。";
                    System.out.println("W" + msg);
                    readWriteLock.writeLock().unlock();
                }
            });
        }

        for (Thread thread: reads) {
            thread.start();
        }
        for (Thread thread: writes) {
            thread.start();
        }

    }
}

StampedLock 读写锁 乐观读 读的同时可以写

class StampedLockClass implements MainInterface {
    String msg = "origin";
    StampedLock stampedLock = new StampedLock();
    @Override
    public void mainMethod() throws Exception {
        Thread[] reads = new Thread[10];
        for (int i=0;i<reads.length;i++) {
            reads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    long l = -1;
                    l = stampedLock.tryOptimisticRead();  //获取乐观读锁
                    while (stampedLock.validate(l)){
                    }
                    l = stampedLock.readLock();
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("R" + msg);
                    stampedLock.unlockRead(l);
                }
            });
        }

        Thread[] writes = new Thread[5];
        for (int i=0;i<writes.length;i++) {
            writes[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    long l = -1;
                    l = stampedLock.writeLock();
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    msg += "。";
                    System.out.println("W" + msg);
                    stampedLock.unlockWrite(l);
                }
            });
        }

        for (Thread thread: reads) {
            thread.start();
        }
        for (Thread thread: writes) {
            thread.start();
        }

    }
}

AtomicInteger 原子性递增

class AtomicIntClass implements MainInterface {

    AtomicInteger atomicInteger = new AtomicInteger();

    @Override
    public void mainMethod() throws Exception {
        long startTime =System.currentTimeMillis();
        Thread[] threads = new Thread[1000];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<10000;i++) {
                        atomicInteger.incrementAndGet();
                    }
                }
            });
        }
        for (Thread thread: threads) {
            thread.start();
        }
        for (Thread thread: threads) {
            thread.join();
        }
        long endTime =System.currentTimeMillis();
        System.out.println("AtomicInteger :" + atomicInteger.get() + " 消耗时间:" + (endTime-startTime) + "ms");
    }
}

LongAdder 分段式锁 (分段累加 最后汇总)

class  LongAdderClass implements MainInterface {

    LongAdder longAdder = new LongAdder();

    @Override
    public void mainMethod()  throws Exception {
        long startTime = System.currentTimeMillis();
        Thread[] threads = new Thread[1000];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<10000;i++) {
                        longAdder.increment();
                    }
                }
            });
        }
        for (Thread thread: threads) {
            thread.start();
        }
        for (Thread thread: threads) {
            thread.join();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("LongAdder :" + longAdder.longValue() + " 消耗时间:" + (endTime-startTime) + "ms");
    }
}

CountDownLatch 同步计数器
阻塞主线程、等待N个线程执行完,继续主线程执行
通过new CountDownLatch(1000)初始化计数器,通过 countDownLatch .await()进行阻塞, 等待计数器归零

class CountDownClass implements MainInterface {

    CountDownLatch countDownLatch = new CountDownLatch(1000);
    int count = 0;
    @Override
    public void mainMethod() throws Exception {
        long startTime =System.currentTimeMillis();
        Thread[] threads = new Thread[1000];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<10000;i++) {
                        synchronized (this.getClass()){
                            count++;
                        }
                    }
                    countDownLatch.countDown();
                }
            });
        }
        for (Thread thread: threads) {
            thread.start();
        }
        countDownLatch.await();
        long endTime =System.currentTimeMillis();
        System.out.println("CountDownLatch :" + count + " 消耗时间:" + (endTime-startTime) + "ms");
    }
}

CyclicBarrier 循环栅栏
每隔N个线程,异步调用一个外部方法
通过 new CyclicBarrier(5, Thread) 初始话循环栅栏,通过cyclicBarrier.await()阻塞线程,每满5线程数唤醒所阻塞的线程并且调用Thread中方法

class CyclicBarrierClass implements MainInterface {

    @Override
    public void mainMethod() throws Exception {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
            @Override
            public void run() {
                System.out.println("满员 发车。。。");
            }
        });
        Thread[] threads = new Thread[30];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("等待线程" + this);
                        cyclicBarrier.await();
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        for (Thread thread: threads) {
            thread.start();
        }
    }
}

Phaser 阶段器 分阶段阻塞线程
重写Phaser类中onAdvance方法,初始化线程时进行注册phaser.register(),实现分阶段阻塞,类似于运动员比赛过程,
全员到齐->第一场比赛->第二场比赛

class PhaserClass extends Phaser implements MainInterface {


    @Override
    public void mainMethod() throws Exception {
        Phaser phaser = new PhaserClass();
        Thread[] threads = new Thread[5];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "到达赛场");
                    phaser.arriveAndAwaitAdvance();
                    System.out.println(Thread.currentThread().getName() + "第一场比赛结束");
                    phaser.arriveAndAwaitAdvance();
                    System.out.println(Thread.currentThread().getName() + "第二场比赛结束");
                    phaser.arriveAndAwaitAdvance();
                    System.out.println(Thread.currentThread().getName() + "第三场比赛结束");

                }
            }, "运动员"+i);
            phaser.register();
        }

        for (Thread thread: threads) {
            thread.start();
        }
    }

    @Override
    protected boolean onAdvance(int phase, int registeredParties) {
        switch (phase) {
            case 0:
                System.out.println("第一场比赛ING。。。");
                return false;
            case 1:
                System.out.println("第二场比赛ING。。。");
                return false;
            case 2:
                System.out.println("第三场比赛ING。。。");
                return true;
            default:
                return true;

        }
    }
}

Semaphore 计数信号器 (限流作用)
通过 new Semaphore(2) 初始化计数器,调用semaphore.acquire()消耗数目,等计数器数目为0时阻塞,调用semaphore.release()释放计数器

class SemaphoreClass implements MainInterface {

    Semaphore semaphore = new Semaphore(2);
    @Override
    public void mainMethod() throws Exception {
        Thread[] threads = new Thread[10];
        for (int i=0;i<threads.length;i++) {
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                        System.out.println("限流车辆" + this);
                        TimeUnit.SECONDS.sleep(1);
                        semaphore.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        for (Thread thread: threads) {
            thread.start();
        }

    }
}

Exchanger 交换器 交换两个线程的数据
调用 exchanger.exchange(Object) 进行阻塞,等待另一个线程调用exchanger.exchange(Object) ,实现Object内容互换

class ExchangerClass implements MainInterface {
    Exchanger<String> exchanger = new Exchanger<>();

    @Override
    public void mainMethod() throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String equitment = "屠龙刀";
                System.out.println(this +equitment);
                try {
                    equitment = exchanger.exchange(equitment);
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(this + equitment);
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String equitment = "倚天剑";
                System.out.println(this +equitment);
                try {
                    equitment = exchanger.exchange(equitment);
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println(this + equitment);
            }
        }).start();
    }
}

LockSupport
调用LockSupport.park()阻塞线程 调用LockSupport.unpark(Thread) 唤醒线程,类似于Condition

class LockSupportClass implements MainInterface {
    String[] a = new String[]{"a","b","c","d","e","f"};
    String[] b = new String[]{"1","2","3","4","5","6"};
    Thread t1,t2;

    @Override
    public void mainMethod() throws Exception {

        t1 = new Thread(new Runnable() {
             @Override
             public void run() {
                 for (String str: a) {
                     System.out.printf(str);
                     LockSupport.unpark(t2);
                     LockSupport.park();
                 }
             }
         });
        t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (String str: b) {
                    LockSupport.park();
                    System.out.printf(str);
                    LockSupport.unpark(t1);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值