《从Java面试题看源码》-Flow、SubmissionPubliser源码分析_submissionpublisher 最大容量(1)

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化资料的朋友,可以戳这里获取

+ [SubmissionPublisher](#SubmissionPublisher_95)
+ - [使用示例](#_159)
	- [基本属性](#_177)
	- [构造函数](#_209)
	- [内部类](#_237)
	- * [ConsumerSubscriber](#ConsumerSubscriber_239)
		* [ConsumerTask](#ConsumerTask_280)
		* [BufferedSubscription](#BufferedSubscription_297)
		* [ThreadPerTaskExecutor](#ThreadPerTaskExecutor_816)
	- [基本方法](#_826)
	- * [subscribe](#subscribe_828)
		* [submit](#submit_893)
		* [offer](#offer_902)
		* [close](#close_922)
		* [closeExceptionally](#closeExceptionally_949)
		* [isClosed](#isClosed_978)
		* [getClosedException](#getClosedException_987)
		* [hasSubscribers](#hasSubscribers_996)
		* [getNumberOfSubscribers](#getNumberOfSubscribers_1020)
		* [getExecutor](#getExecutor_1031)
		* [getMaxBufferCapacity](#getMaxBufferCapacity_1040)
		* [getSubscribers](#getSubscribers_1049)
		* [isSubscribed](#isSubscribed_1076)
		* [estimateMinimumDemand](#estimateMinimumDemand_1105)
		* [estimateMaximumLag](#estimateMaximumLag_1136)
		* [consume](#consume_1165)
		* [roundCapacity](#roundCapacity_1178)
		* [doOffer](#doOffer_1194)
		* [retryOffer](#retryOffer_1245)
		* [cleanAndCount](#cleanAndCount_1279)

Flow 、SubmissionPubliser类是 java9中新增的类,都被放在JUC包中

Flow

定义了一种生产者和消费者(订阅者)模型的接口,可以用于流式控制中

Publisher
//流式接口
//定义生产者
@FunctionalInterface
public static interface Publisher<T> {
    /\*\*
 \* 增加订阅者
 \*
 \* @param subscriber 订阅者
 \* @throws NullPointerException if subscriber is null
 \*/
    public void subscribe(Subscriber<? super T> subscriber);
}

Subscriber
//订阅者
public static interface Subscriber<T> {
    /\*\*
 \* 在Publisher接收一个新的订阅者时,会调用该方法
 \*
 \* @param subscription 订阅包,提供了获取下一个元素和取消获取操作的方法
 \*/
    public void onSubscribe(Subscription subscription);

    /\*\*
 \* 获取一个新元素,每次有新元素的时候会被调用
 \*
 \* @param item the item
 \*/
    public void onNext(T item);

    /\*\*
 \* 发送异常时会被调用
 \*
 \* @param throwable the exception
 \*/
    public void onError(Throwable throwable);

    /\*\*
 \* 当数据接收完成后调用
 \*/
    public void onComplete();
}

Subscription
//订阅包
public static interface Subscription {
    /\*\*
 \* 发起请求,获取元素
 \*
 \* @param n 获取的增量,Long.MAX\_VALUE表示无限的
 \*/
    public void request(long n);

    /\*\*
 \* 取消消息接收
 \*/
    public void cancel();
}

Processor
//既可以充当生产者,也可以充当订阅者
public static interface Processor<T,R> extends Subscriber<T>, Publisher<R> {
}

static final int DEFAULT_BUFFER_SIZE = 256;

/\*\*
 \* 定义的默认缓存区,256
 \*
 \* @return the buffer size value
 \*/
public static int defaultBufferSize() {
    return DEFAULT_BUFFER_SIZE;
}

SubmissionPublisher

FLow 类的一种实现,可以在并发环境下使用

JDK中的说明:

SubmissionPublisher提供了使用Executor的构造函数,如果生产者是在独立线程中运行,并且能估计消费者数量,就使用Executors.newFixedThreadPool(int)固定线程数量的线程池,否则使用默认,ForkJoinPool.commonPool()

SubmissionPublisher提供缓冲功能,能够使生产者和消费者以不同的速率运行,每个消费者独立使用一个缓冲区,缓冲区在首次使用的时候创建,提供了一个默认值256,并会根据需要扩大到最大值,容量通常扩大到最近的2的次幂或者支持的最大值

SubmissionPublisher可以在多个线程之间共享,会在发布项目之前执行操作或者会发出一个happen-before信号给每个对应的消费者

发布方法支持设置在缓冲区满时如何处理的不同策略,submit(Object)方法会阻塞直到有可用资源,这种用法最简单,但速度慢;offer方法虽然会丢弃项目,但提供了插入动作重试的机会

如果任何Subscriber方法抛出异常,在其订阅将被取消

方法consume(Consumer)简化了对常见情况的支持,其中订阅者的唯一操作是使用supplied的函数请求和处理所有项目

此类还可以作为生成元素的子类的基类,并使用此类中的方法发布。例如,下面是一个周期性发布supplier生成元素的类。 (实际上,您可以添加独立启动和停止生成的方法,在发布者之间共享Executor等,或者使用SubmissionPublisher作为组件而不是超类。)

class PeriodicPublisher<T> extends SubmissionPublisher<T> {
   final ScheduledFuture<?> periodicTask;
   final ScheduledExecutorService scheduler;
   PeriodicPublisher(Executor executor, int maxBufferCapacity,
                     Supplier<? extends T> supplier,
                     long period, TimeUnit unit) {
     super(executor, maxBufferCapacity);
     scheduler = new ScheduledThreadPoolExecutor(1);
     periodicTask = scheduler.scheduleAtFixedRate(
       () -> submit(supplier.get()), 0, period, unit);
   }
   public void close() {
     periodicTask.cancel(false);
     scheduler.shutdown();
     super.close();
   }
 }

以下是Flow.Processor实现的示例,为了简化说明,使用单步向publisher发起请求,更合适的版本可以使用submit方法或者其他实用方法来监控流量

class TransformProcessor<S,T> extends SubmissionPublisher<T>
    implements Flow.Processor<S,T> {
    final Function<? super S, ? extends T> function;
    Flow.Subscription subscription;
    TransformProcessor(Executor executor, int maxBufferCapacity,
                       Function<? super S, ? extends T> function) {
        super(executor, maxBufferCapacity);
        this.function = function;
    }
    public void onSubscribe(Flow.Subscription subscription) {
        (this.subscription = subscription).request(1);
    }
    public void onNext(S item) {
        subscription.request(1);
        submit(function.apply(item));
    }
    public void onError(Throwable ex) { closeExceptionally(ex); }
    public void onComplete() { close(); }
}

使用示例
@Test
public void test1() throws Exception {
    SubmissionPublisher<Integer> submissionPublisher = new SubmissionPublisher<>();
    submissionPublisher.consume(System.out::println);
    submissionPublisher.submit(1);
    submissionPublisher.submit(2);
    submissionPublisher.offer(3, (x, y) -> {
        System.out.println("xxx");
        return false;
    });
}

使用二进制来表示状态码可以参考:https://blog.csdn.net/qq_36631014/article/details/79675924

基本属性
//缓存区最大值,1073741824
static final int BUFFER_CAPACITY_LIMIT = 1 << 30;
//初始的最大缓存区,缓存区大小必须是2的幂次
static final int INITIAL_CAPACITY = 32;
/\*\*
\* BufferedSubscription通过next字段维护了一个链表,这种结构对循环发布非常有用
\* 需要遍历O(n)次来检查重复的消费者,但是预计消费比发布少的多
\* 取消订阅只发生在遍历循环期间,如果BufferedSubscription方法返回负值表示他们已经关闭
\* 为了减少head-of-line阻塞,submit和offer方法首先调用BufferedSubscription的offer方法
\* 在满的时候提供了重试
\*/
BufferedSubscription<T> clients;
/\*\* 运行状态,只在锁内更新\*/
volatile boolean closed;
/\*\* 在第一次调用subscribe的时候,设置为true,初始化消费的所有者线程\*/
boolean subscribed;
/\*\* 第一次调用subscribe的线程,如果线程曾经改变过,则为null \*/
Thread owner;
/\*\* 如果不为null,说明在closeExceptionally中生成了异常 \*/
volatile Throwable closedException;

// 用于构造BufferedSubscriptions的参数
final Executor executor;
//onNext中发生异常时的处理函数
final BiConsumer<? super Subscriber<? super T>, ? super Throwable> onNextHandler;
//表示缓冲区最大容量
final int maxBufferCapacity;

构造函数
//默认构造函数,默认使用ForkJoinPool的公共线程池异步运行subscriber,除非并发级别不支持,则使用普通的线程池
//ThreadPerTaskExecutor
//subscriber的最大缓冲区为256
//Subscriber的异常处理器为null
public SubmissionPublisher() {
    this(ASYNC_POOL, Flow.defaultBufferSize(), null);
}
//使用指定的线程池,指定的缓冲区容量创建
public SubmissionPublisher(Executor executor, int maxBufferCapacity) {
    this(executor, maxBufferCapacity, null);
}

public SubmissionPublisher(Executor executor, int maxBufferCapacity,
                           BiConsumer<? super Subscriber<? super T>, ? super Throwable> handler) {
    if (executor == null)
        throw new NullPointerException();
    if (maxBufferCapacity <= 0)
        throw new IllegalArgumentException("capacity must be positive");
    this.executor = executor;
    this.onNextHandler = handler;
    //最大容量为离2的幂次最近的值,跟hashMap的计算最大容量算法一样
    this.maxBufferCapacity = roundCapacity(maxBufferCapacity);
}

内部类
ConsumerSubscriber
/\*\* 订阅者,供内部使用\*/
static final class ConsumerSubscriber<T> implements Subscriber<T> {
    final CompletableFuture<Void> status;
    final Consumer<? super T> consumer;
    Subscription subscription;
    ConsumerSubscriber(CompletableFuture<Void> status,
                       Consumer<? super T> consumer) {
        this.status = status; this.consumer = consumer;
    }
    public final void onSubscribe(Subscription subscription) {
        this.subscription = subscription;
        //如果元素已经消费完成,那么取消订阅
        status.whenComplete((v, e) -> subscription.cancel());
        //没有完成继续请求
        if (!status.isDone())
            subscription.request(Long.MAX_VALUE);
    }
    //发送错误时候的处理
    public final void onError(Throwable ex) {
        status.completeExceptionally(ex);
    }
    //元素消费完成时候的处理
    public final void onComplete() {
        status.complete(null);
    }
    //有新元素的时候会被调用
    public final void onNext(T item) {
        try {
            //执行Consumer函数
            consumer.accept(item);
        } catch (Throwable ex) {
            subscription.cancel();
            status.completeExceptionally(ex);
        }
    }
}

ConsumerTask
//订阅者的消费任务
static final class ConsumerTask<T> extends ForkJoinTask<Void>
    implements Runnable, CompletableFuture.AsynchronousCompletionTask {
    final BufferedSubscription<T> consumer;
    ConsumerTask(BufferedSubscription<T> consumer) {
        this.consumer = consumer;
    }
    public final Void getRawResult() { return null; }
    public final void setRawResult(Void v) {}
    public final boolean exec() { consumer.consume(); return false; }
    public final void run() { consumer.consume(); }
}

BufferedSubscription
//基于数组的可扩展的环形缓冲区,通过CAS原子操作来实现插入和获取元素
//在任何时间内最多只有一个活动的消费者任务
//publisher通过锁来保证单个生产者
//生成者和消费者之间的同步依赖于volatile修饰的ctl、demand、waiting变量
//使用ctl管理状态
//缓存区开始很小,只在需要的时候扩容
//使用了Contended类避免CPU伪共享问题
//当生成者和消费者以不同的速度运行时,会出现失衡,通过人为细分一些消费者方法,包括隔离所有subscriber回调
@jdk.internal.vm.annotation.Contended
static final class BufferedSubscription<T>
    implements Subscription, ForkJoinPool.ManagedBlocker {
    long timeout;                      // Long.MAX\_VALUE 表示一直等待
    int head;                          // 下一个获取的位置
    int tail;                          // 下一个插入的位置
    final int maxCapacity;             // 最大缓冲大小
    volatile int ctl;                  // 以volatile方式运行状态标志
    Object[] array;                    // 缓冲数组
    final Subscriber<? super T> subscriber; //消费者
    final BiConsumer<? super Subscriber<? super T>, ? super Throwable> onNextHandler; //用于处理异常
    Executor executor;                 // 线程池,发生错误为null
    Thread waiter;                     // 生成者线程被阻塞
    Throwable pendingError;            // onError发生时会赋值该变量
    BufferedSubscription<T> next;      // publisher使用
    BufferedSubscription<T> nextRetry; // publisher使用

    @jdk.internal.vm.annotation.Contended("c") // 隔离CPU缓存行
    volatile long demand;              // 未获取的请求
    @jdk.internal.vm.annotation.Contended("c")
    volatile int waiting;              // 如果生产者被阻塞,则不为零

    // ctl 16进制值
    static final int CLOSED   = 0x01;  // 设置了,忽略其他位
    static final int ACTIVE   = 0x02;  // 保存消费者任务一直存活
    static final int REQS     = 0x04;  // 非零请求
    static final int ERROR    = 0x08;  // 发生错误时,调用onError
    static final int COMPLETE = 0x10;  // 当完成时,调用onComplete
    static final int RUN      = 0x20;  // 任务正在跑或刚开始跑
    static final int OPEN     = 0x40;  // 订阅后位true

    static final long INTERRUPTED = -1L; // 超时或者中断标志

    BufferedSubscription(Subscriber<? super T> subscriber,
                         Executor executor,
                         BiConsumer<? super Subscriber<? super T>,
 ? super Throwable> onNextHandler,
                         Object[] array,
                         int maxBufferCapacity) {
        this.subscriber = subscriber;
        this.executor = executor;
        this.onNextHandler = onNextHandler;
        this.array = array;
        this.maxCapacity = maxBufferCapacity;
    }

    // Wrappers for some VarHandle methods

    //cas更新ctl变量
    final boolean weakCasCtl(int cmp, int val) {
        return CTL.weakCompareAndSet(this, cmp, val);
    }

    //进行或计算
    final int getAndBitwiseOrCtl(int bits) {
        return (int)CTL.getAndBitwiseOr(this, bits);
    }

    //这段代码会将demand的值减去指定的k
    final long subtractDemand(int k) {
        long n = (long)(-k);
        //为什么还要再加n呢?
        return n + (long)DEMAND.getAndAdd(this, n);
    }

    //cas设置demand值
    final boolean casDemand(long cmp, long val) {
        return DEMAND.compareAndSet(this, cmp, val);
    }

    // SubmissionPublisher实用方法

    /\*\*
 \* 如果关闭了返回true (消费任务可能还在运行中).
 \*/
    final boolean isClosed() {
        return (ctl & CLOSED) != 0;
    }

    /\*\*
 \* 返回估计的元素数量,如果关闭了返回-1
 \*/
    final int estimateLag() {
        int c = ctl, n = tail - head;
        return ((c & CLOSED) != 0) ? -1 : (n < 0) ? 0 : n;
    }

    // 提交元素的方法

    /\*\*
 \* 尝试插入元素,并启动消费任务
 \* @return 如果关闭返回-1,0表示满了
 \*/
    final int offer(T item, boolean unowned) {
        //存放元素的数组
        Object[] a;
        //cap 表示数组的容量
        int stat = 0, cap = ((a = array) == null) ? 0 : a.length; 
        //i 表示要插入的索引位置;n表示已有的元素
        int t = tail, i = t & (cap - 1), n = t + 1 - head;
        if (cap > 0) {
            boolean added;
            if (n >= cap && cap < maxCapacity) // 发生扩容
                added = growAndOffer(item, a, t);
            else if (n >= cap || unowned)      // 如果不是自身的线程,就说明有竞争,需要使用CAS来更新
                added = QA.compareAndSet(a, i, null, item);
            else {                             // 使用release 模式插入元素到数组中
                QA.setRelease(a, i, item);
                added = true;
            }
            //插入成功,tail后移一位
            if (added) {
                tail = t + 1;
                stat = n;
            }
        }
        return startOnOffer(stat);  //尝试唤醒消费线程
    }

    /\*\*
 \* 尝试扩展缓冲区,并插入元素,成功返回true。如果内存溢出就会失败
 \*/
    final boolean growAndOffer(T item, Object[] a, int t) {
        int cap = 0, newCap = 0;
        Object[] newArray = null;
        if (a != null && (cap = a.length) > 0 && (newCap = cap << 1) > 0) { //进行两倍扩容
            try {
                newArray = new Object[newCap];
            } catch (OutOfMemoryError ex) {
            }
        }
        if (newArray == null)
            return false;
        else {
            //从原数组中取值,并插入新数组中
            int newMask = newCap - 1;
            newArray[t-- & newMask] = item;
            for (int mask = cap - 1, k = mask; k >= 0; --k) {
                Object x = QA.getAndSet(a, t & mask, null);
                if (x == null) //x为null说明已经获取
                    break;                    
                else
                    newArray[t-- & newMask] = x;
            }
            array = newArray;
            VarHandle.releaseFence();  //释放空数组
            return true;
        }
    }

    /\*\*
 \* 重试版本,没有扩容或者偏置
 \*/
    final int retryOffer(T item) {
        Object[] a;
        int stat = 0, t = tail, h = head, cap;
        if ((a = array) != null && (cap = a.length) > 0 &&
            QA.compareAndSet(a, (cap - 1) & t, null, item))
            //将tail后移1位
            stat = (tail = t + 1) - h;
        return startOnOffer(stat);
    }

    /\*\*
 \* 唤醒消费者任务
 \* @return 如果是关闭的返回负数
 \*/
    final int startOnOffer(int stat) {
        int c; // 如果存在请求且未激活,则启动或保持活动状态
        if (((c = ctl) & (REQS | ACTIVE)) == REQS &&
            ((c = getAndBitwiseOrCtl(RUN | ACTIVE)) & (RUN | CLOSED)) == 0)
            //启动消费者线程
            tryStart();
        else if ((c & CLOSED) != 0)
            stat = -1;
        return stat;
    }

    /\*\*
 \* 启动消费的任务。 失败设置错误状态。
 \*/
    final void tryStart() {
        try {
            Executor e;
            //消费者任务
            ConsumerTask<T> task = new ConsumerTask<T>(this);
            if ((e = executor) != null)   // 发生错误就跳过
                e.execute(task);
        } catch (RuntimeException | Error ex) {
            //设置ctl状态
            getAndBitwiseOrCtl(ERROR | CLOSED);
            throw ex;
        }
    }

    // 向消费者任务发信号

    /\*\*
 \* 设置给定的控制位,启动任务,如果任务没有运行或者没有关闭
 \* @param 运行状态位
 \*/
    final void startOnSignal(int bits) {
        if ((ctl & bits) != bits &&
            (getAndBitwiseOrCtl(bits) & (RUN | CLOSED)) == 0)
            tryStart();
    }

    //订阅中
    final void onSubscribe() {
        startOnSignal(RUN | ACTIVE);
    }

    //已完成
    final void onComplete() {
        startOnSignal(RUN | ACTIVE | COMPLETE);
    }

    //发生错误
    final void onError(Throwable ex) {
        int c; Object[] a;      //发生异步错误了清空缓冲区
        if (ex != null)
            pendingError = ex; 
        if (((c = getAndBitwiseOrCtl(ERROR | RUN | ACTIVE)) & CLOSED) == 0) {  //还没有关闭
            if ((c & RUN) == 0)  //没有运行,尝试重试
                tryStart();
            else if ((a = array) != null)  //清空缓冲区
                Arrays.fill(a, null);
        }
    }

    //取消
    public final void cancel() {
        onError(null);
    }

    //请求为获取的数据
    public final void request(long n) {
        if (n > 0L) {
            for (;;) {
                long p = demand, d = p + n;  // saturate
                if (casDemand(p, d < p ? Long.MAX_VALUE : d))
                    break;
            }
            startOnSignal(RUN | ACTIVE | REQS);
        }
        else
            onError(new IllegalArgumentException(
                        "non-positive subscription request"));
    }

    // 消费者的方法

    /\*\*
 \* ConsumerTask调用,循环消费元素,或者在提交元素的时候,间接触发
 \*/
    final void consume() {
        Subscriber<? super T> s;
        if ((s = subscriber) != null) {          // hoist checks
            //触发消费者,请求更多的数据
            subscribeOnOpen(s);
            long d = demand;
            //从数组头到尾进行循环
            for (int h = head, t = tail;;) {
                int c, taken; boolean empty;
                //如果发生了错误,中断消费
                if (((c = ctl) & ERROR) != 0) { 
                    closeOnError(s, null);
                    break;
                }
                //一直获取元素,直到消费完或者发生错误
                else if ((taken = takeItems(s, d, h)) > 0) {
                    //头指针后移
                    head = h += taken;
                    //修改未获取数据的局部变量demand
                    d = subtractDemand(taken);
                }
                else if ((d = demand) == 0L && (c & REQS) != 0)
                    weakCasCtl(c, c & ~REQS);    // 已经消费完,删除ctl中的请求标志位
                else if (d != 0L && (c & REQS) == 0)
                    weakCasCtl(c, c | REQS);     // 有新元素需要消费,增加ctl中的请求标志位
                else if (t == (t = tail)) {      // 稳定性检查
                    if ((empty = (t == h)) && (c & COMPLETE) != 0) {  //已经消费完了,但缓存区还没关闭
                        closeOnComplete(s);      //关闭缓存区
                        break;
                    }
                    else if (empty || d == 0L) {
                        //判断ctl运行标志是否有ACTIVE标志,有bit=ACTIVE
                        //没有bit=RUN
                        int bit = ((c & ACTIVE) != 0) ? ACTIVE : RUN;
                        //删除相应的标志位,并更新ctl值,如果是RUN状态,需要退出消费任务
                        if (weakCasCtl(c, c & ~bit) && bit == RUN)
                            break;               // 取消激活或者退出
                    }
                }
            }
        }
    }

    /\*\*
 \* 一直消费元素直到 不可用 或者 达到边界 或者 发生错误
 \*
 \* @param s subscriber 消费者
 \* @param d demand 当前需要获取的数量
 \* @param h current head 当前的头指针位置
 \* @return number taken 获取的数量
 \*/
    final int takeItems(Subscriber<? super T> s, long d, int h) {
        Object[] a;
        int k = 0, cap;
        if ((a = array) != null && (cap = a.length) > 0) {
            int m = cap - 1, b = (m >>> 3) + 1; // min(1, cap/8)
            int n = (d < (long)b) ? (int)d : b;
            for (; k < n; ++h, ++k) {
                //获取元素,并置为null
                Object x = QA.getAndSet(a, h & m, null);
                if (waiting != 0)
                    //唤醒被阻塞的生成者
                    signalWaiter();
                if (x == null)
                    break;
                else if (!consumeNext(s, x))  //调用消费者的onNext方法,来处理元素
                    break;
            }
        }
        return k;
    }

    //调用消费者的onNext方法,来处理元素
    final boolean consumeNext(Subscriber<? super T> s, Object x) {
        try {
            @SuppressWarnings("unchecked") T y = (T) x;
            if (s != null)
                s.onNext(y);
            return true;
        } catch (Throwable ex) {
            //处理异常
            handleOnNext(s, ex);
            return false;
        }
    }

    /\*\*
 \* 处理 Subscriber.onNext中的异常
 \*/
    final void handleOnNext(Subscriber<? super T> s, Throwable ex) {
        BiConsumer<? super Subscriber<? super T>, ? super Throwable> h;
        try {
            if ((h = onNextHandler) != null)
                //调用自定义的BiConsumer函数处理异常
                h.accept(s, ex);
        } catch (Throwable ignore) {
        }
        //关闭缓冲并调用消费者的onError方法
        closeOnError(s, ex);
    }

    /\*\*
 \* 如果是第一次执行,那么执行subscriber.onSubscribe,用于请求更多数据
 \*/
    final void subscribeOnOpen(Subscriber<? super T> s) {
        if ((ctl & OPEN) == 0 && (getAndBitwiseOrCtl(OPEN) & OPEN) == 0)
            consumeSubscribe(s);
    }

    //执行subscriber.onSubscribe,用于请求更多数据
    final void consumeSubscribe(Subscriber<? super T> s) {
        try {
            if (s != null) // ignore if disabled
                s.onSubscribe(this);
        } catch (Throwable ex) {
            closeOnError(s, ex);
        }
    }

    /\*\*
 \* 缓冲区还没关闭,就执行subscriber.onComplete
 \*/
    final void closeOnComplete(Subscriber<? super T> s) {
        if ((getAndBitwiseOrCtl(CLOSED) & CLOSED) == 0)
            consumeComplete(s);
    }

    //执行subscriber.onComplete
    final void consumeComplete(Subscriber<? super T> s) {
        try {
            if (s != null)
                s.onComplete();
        } catch (Throwable ignore) {
        }
    }

    /\*\*
 \* 发生错误,执行subscriber.onError,并且唤醒阻塞的生产者
 \*/
    final void closeOnError(Subscriber<? super T> s, Throwable ex) {
        if ((getAndBitwiseOrCtl(ERROR | CLOSED) & CLOSED) == 0) {
            if (ex == null)
                ex = pendingError;
            pendingError = null;  // detach
            executor = null;      // suppress racing start calls
            signalWaiter();
            consumeError(s, ex);
        }
    }

    //执行subscriber.onError
    final void consumeError(Subscriber<? super T> s, Throwable ex) {
        try {
            if (ex != null && s != null)
                s.onError(ex);
        } catch (Throwable ignore) {
        }
    }

    // 阻塞支持

    /\*\*
 \* 唤醒等待的生产者
 \*/
    final void signalWaiter() {
        Thread w;
        waiting = 0;
        if ((w = waiter) != null)
            LockSupport.unpark(w);
    }

    /\*\*
 \* 如果缓冲区关闭了,或者有可用空间,返回true
 \*/
    public final boolean isReleasable() {
        Object[] a; int cap;
        return ((ctl & CLOSED) != 0 ||
                ((a = array) != null && (cap = a.length) > 0 &&
                 QA.getAcquire(a, (cap - 1) & tail) == null));
    }

    /\*\*
 \* 一直阻塞知道 timeout, closed,或者有可用空间
 \*/
    final void awaitSpace(long nanos) {
        if (!isReleasable()) {
            ForkJoinPool.helpAsyncBlocker(executor, this);
            if (!isReleasable()) {
                timeout = nanos;
                try {
                    ForkJoinPool.managedBlock(this);
                } catch (InterruptedException ie) {
                    timeout = INTERRUPTED;
                }
                if (timeout == INTERRUPTED)
                    Thread.currentThread().interrupt();
            }
        }
    }

    /\*\*
 \* 给 ManagedBlocker提供的用于阻塞的方法
 \*/
    public final boolean block() {
        long nanos = timeout;
        boolean timed = (nanos < Long.MAX_VALUE);
        long deadline = timed ? System.nanoTime() + nanos : 0L;
        while (!isReleasable()) {
            if (Thread.interrupted()) {
                timeout = INTERRUPTED;
                if (timed)
                    break;
            }
            else if (timed && (nanos = deadline - System.nanoTime()) <= 0L)
                break;
            else if (waiter == null)
                waiter = Thread.currentThread();
            else if (waiting == 0)
                waiting = 1;
            else if (timed)
                LockSupport.parkNanos(this, nanos);
            else
                LockSupport.park(this);
        }
        waiter = null;
        waiting = 0;
        return true;
    }

    // VarHandle mechanics 一些变量的封装对象,相当于以前调用UNSAFE的方法
    static final VarHandle CTL;
    static final VarHandle DEMAND;
    static final VarHandle QA;

    static {
        try {
            MethodHandles.Lookup l = MethodHandles.lookup();
            CTL = l.findVarHandle(BufferedSubscription.class, "ctl",
                                  int.class);
            DEMAND = l.findVarHandle(BufferedSubscription.class, "demand",
                                     long.class);
            QA = MethodHandles.arrayElementVarHandle(Object[].class);
        } catch (ReflectiveOperationException e) {
            throw new ExceptionInInitializerError(e);
        }

        // 减少首次加载时候的风险


![img](https://img-blog.csdnimg.cn/img_convert/d1ff72fe689397df750234e12b65c879.png)
![img](https://img-blog.csdnimg.cn/img_convert/40c3f771bca19bfdafcb7378380bcffe.png)
![img](https://img-blog.csdnimg.cn/img_convert/a938c4e4a0123e6fe5656a8dbf43df22.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**



    static {
        try {
            MethodHandles.Lookup l = MethodHandles.lookup();
            CTL = l.findVarHandle(BufferedSubscription.class, "ctl",
                                  int.class);
            DEMAND = l.findVarHandle(BufferedSubscription.class, "demand",
                                     long.class);
            QA = MethodHandles.arrayElementVarHandle(Object[].class);
        } catch (ReflectiveOperationException e) {
            throw new ExceptionInInitializerError(e);
        }

        // 减少首次加载时候的风险


[外链图片转存中...(img-jAOtJyMp-1715343837677)]
[外链图片转存中...(img-seYtAipK-1715343837677)]
[外链图片转存中...(img-e27spj0D-1715343837677)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**

**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

  • 21
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值