多线程(十三)--Concurent.util常用类

 

 

一、CountDownLatch

import java.util.concurrent.CountDownLatch;

public class UseCountDownLatch {

    public static void main(String[] args) {

        final CountDownLatch latch = new CountDownLatch(2);

        Thread t1 = new Thread(() -> {
            try {
                System.out.println("进入线程" + Thread.currentThread().getName() + ",等待其他线程处理完成...");
                latch.await();
                System.out.println(Thread.currentThread().getName() + "线程继续执行...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "线程进行初始化操作...");
                Thread.sleep(3000);
                System.out.println(Thread.currentThread().getName() + "线程初始化完毕,通知t1线程继续...");
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "t2");

        Thread t3 = new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "线程进行初始化操作...");
                Thread.sleep(4000);
                System.out.println(Thread.currentThread().getName() + "线程初始化完毕,通知t1线程继续...");
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "t3");

        t1.start();
        t2.start();
        t3.start();
    }
}

实际的使用场景

主线程创建zk客户端去连接zk服务器,然后阻塞。子线程在这个process方法中轮询的去检查连接状态,当收到zk服务端的回调事件的时候,就countDown()。主线程就不再阻塞了。

二、CyclicBarrier

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class UseCyclicBarrier {

    static class Runner implements Runnable {

        private CyclicBarrier barrier;
        private String name;

        public Runner(CyclicBarrier barrier, String name) {
            this.barrier = barrier;
            this.name = name;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(1000 * (new Random().nextInt(5)));
                System.out.println(name + "准备ok");
                barrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println(name + " Go!!");
        }
    }

    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(3);
        ExecutorService executor = Executors.newFixedThreadPool(3);

        executor.submit(new Thread(new Runner(barrier, "zhangsan")));
        executor.submit(new Thread(new Runner(barrier, "lis")));
        executor.submit(new Thread(new Runner(barrier, "wangwu")));

        executor.shutdown();
    }
}

tips:CountDownLatch和CyclicBarrier的区别

CountDownLatch是主线程阻塞,多个子线程给主线程发出通知。

CyclicBarrier是多个线程本身进行阻塞,当所有线程都执行await()方法之后,同一时刻,一起继续往下执行。

CyclicBarrier的使用场景,可以是多个集群要统一做某事的时候使用。

 

三、Future和Callable

 

 

 

四、Semaphore

讲semaphore之前,讲一下高并发的处理思路:

1.网络(Nginx限流)

2.服务端(业务拆分,Nginx分流)

3.Java(限流)

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

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

        ExecutorService pool = Executors.newCachedThreadPool();
        //只能5个线程同时访问
        final Semaphore semaphore = new Semaphore(5);
        //模拟20个客户端访问
        for (int index = 0; index < 20; index++) {
            final int NO = index;
            Runnable run = new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                        System.out.println("Accessing: " + NO);
                        //模拟实际业务逻辑
                        Thread.sleep((long) (Math.random() * 10000));
                        //访问完,释放
                        semaphore.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            pool.execute(run);
        }

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值