import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class CyclicBarrierDemo {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
@Override
public void run() {
System.out.println("所有人都到了,大家统一出发");
}
});
for (int i = 0; i < 5; i++) {
new Thread(new Task(cyclicBarrier)).start();
}
}
static class Task implements Runnable {
private final CyclicBarrier cyclicBarrier;
public Task(CyclicBarrier cyclicBarrier) {
this.cyclicBarrier = cyclicBarrier;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " 现在前往集合地点");
Thread.sleep((long) (Math.random() * 10000));
System.out.println(Thread.currentThread().getName() + " 已经到了集合地点,等待其他人到达");
cyclicBarrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
}
}
import java.util.concurrent.*;
public class Test {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(5);
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
final int no = i + 1;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep((long) (Math.random() * 10000));
System.out.println("Num:" + no);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
latch.countDown();
}
}
};
service.submit(runnable);
}
System.out.println("等待5个线程执行完....");
latch.await();
System.out.println("所有线程都已经执行完,可以进入下一个环节了。");
}
}
import java.util.concurrent.*;
/**
* 模拟跑步比赛,5名选手等待1名裁判发令,发令后所有人同时开始跑步
*/
public class Test {
public static void main(String[] args) throws InterruptedException {
// 1名裁判
CountDownLatch latch = new CountDownLatch(1);
// 5名选手
ExecutorService service = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
final int no = i + 1;
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println("NO." + no + "号选手准备完毕,等待发令");
latch.await();
System.out.println("NO." + no + "开始跑步");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
service.submit(runnable);
}
System.out.println("裁判检查发令枪....");
Thread.sleep(2000);
System.out.println("裁判检查完毕,比赛开始....");
latch.countDown();
}
}
import java.util.concurrent.*;
public class Test {
static Semaphore semaphore = new Semaphore(3, true);
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(50);
for (int i = 0; i < 100; i++) {
service.submit(new Task());
}
service.shutdown();
}
static class Task implements Runnable {
@Override
public void run() {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 拿到了许可证");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 释放了许可证");
semaphore.release();
}
}
}
4. Condition
import java.util.PriorityQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ConditionDemo {
private final int queueSize = 10;
private final PriorityQueue<Integer> queue = new PriorityQueue<>(queueSize);
private final Lock lock = new ReentrantLock();
private final Condition notFull = lock.newCondition();
private final Condition notEmpty = lock.newCondition();
public static void main(String[] args) {
ConditionDemo demo = new ConditionDemo();
Produce produce = demo.new Produce();
Consume consume = demo.new Consume();
produce.start();
consume.start();
}
/**
* 消费者
*/
class Consume extends Thread {
@Override
public void run() {
try {
consume();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void consume() throws InterruptedException {
while (true) {
lock.lock();
try {
while (queue.size() == 0) {
System.out.println("队列空,等待数据");
notEmpty.await();
}
// 从队列中取数据
queue.poll();
// 并且唤醒生产者
notFull.signal();
System.out.println("从队列中取走了一条数据,队列剩余:" + queue.size());
} finally {
lock.unlock();
}
}
}
}
/**
* 生产者
*/
class Produce extends Thread {
@Override
public void run() {
try {
produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void produce() throws InterruptedException {
while (true) {
lock.lock();
try {
while (queue.size() == queueSize) {
System.out.println("队列满,等待消费者进行消费");
notFull.await();
}
// 开始生产数据
queue.offer(1);
// 并且唤醒消费者
notEmpty.signal();
System.out.println("向队列添加了一条数据,队列剩余:" + queue.size());
} finally {
lock.unlock();
}
}
}
}
}
5. Phaser
import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Phaser;
public class PhaserTest {
Phaser phaser = new Phaser();
ExecutorService executorService = Executors.newCachedThreadPool();
class Worker implements Runnable {
@Override
public void run() {
phaser.register();
while (true) {
try {
Thread.sleep(500);
System.out.println("working:" + phaser.getPhase());
phaser.arriveAndAwaitAdvance();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void run() throws InterruptedException {
phaser.register();
executorService.execute(new Worker());
executorService.execute(new Worker());
executorService.execute(new Worker());
executorService.execute(new Worker());
while (true) {
phaser.arriveAndAwaitAdvance();
System.out.println("Sync..." + phaser.getPhase());
}
}
public static void main(String[] args) throws InterruptedException {
var test = new PhaserTest();
test.run();
}
}
6. Exchanger
今天给大家分享一份面试攻略,这是一个从阿里出来的大佬总结提供的面试攻略。他在两个月内一共面试了 五十多 场,最终拿了包括 B站、美团、滴滴、百度、字节跳动 在内的 8 个大厂 Offer。
群内知道后,很多朋友都求他分享经验,于是他就干脆就找上我一起写了一篇博客,进行了比较系统的总结————纯干货预警,建议收藏慢慢看。
点击这里进入文章:?【毕业季】阿里出身大佬总结出来的 98%通过率的面试攻略!手把手教你拿下大厂offer!
原文:https://blog.51cto.com/u_15219956/2949725