一、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();
}
}
}