netty-recyle

博客主要介绍了Recycler的初始化参数,如maxCapacityPerThread等。还阐述了Recycler.get()的流程,包括调用threadLocal.get()、stack.pop()等操作。同时提到FastThreadLocal.initialize()的步骤,以及Stack的相关参数和pop()、scavenge()操作,涉及内存回收。

Recycler
初始化
maxCapacityPerThread=32768
maxSharedCapacityFactor=2
maxDelayedQueuesPerThread=6
ratioMask=7

    protected Recycler() {
        this(DEFAULT_MAX_CAPACITY_PER_THREAD);//32768
    }
      protected Recycler(int maxCapacityPerThread) {
        this(maxCapacityPerThread, MAX_SHARED_CAPACITY_FACTOR);//32768 2
    }
    protected Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor) {
        this(maxCapacityPerThread, maxSharedCapacityFactor, RATIO, MAX_DELAYED_QUEUES_PER_THREAD);//32768 2 8 6
    }
    protected Recycler(int maxCapacityPerThread, int maxSharedCapacityFactor,
                       int ratio, int maxDelayedQueuesPerThread) {
        ratioMask = safeFindNextPositivePowerOfTwo(ratio) - 1;
        if (maxCapacityPerThread <= 0) {
            this.maxCapacityPerThread = 0;
            this.maxSharedCapacityFactor = 1;
            this.maxDelayedQueuesPerThread = 0;
        } else {
            this.maxCapacityPerThread = maxCapacityPerThread;
            this.maxSharedCapacityFactor = max(1, maxSharedCapacityFactor);
            this.maxDelayedQueuesPerThread = max(0, maxDelayedQueuesPerThread);
        }
    }

Recycler.get()
1.调用threadLocal.get()其实就是返回一个Stack没有就创建
2.stack.pop(),没有就调用自己实现的newObject()
3.threadlocal.get()若没有值,会调用到initialize()

    public final T get() {
        if (maxCapacityPerThread == 0) {
            return newObject((Handle<T>) NOOP_HANDLE);
        }
        Stack<T> stack = threadLocal.get();
        DefaultHandle<T> handle = stack.pop();
        if (handle == null) {
            handle = stack.newHandle();
            handle.value = newObject(handle);
        }
        return (T) handle.value;
    }

FastThreadLocal.initialize()
1.调用自己实现的initialValue()
2.setIndexedVariable(index, v);
3.addToVariablesToRemove(threadLocalMap, this);

     private V initialize(InternalThreadLocalMap threadLocalMap) {
        V v = null;
        try {
            v = initialValue();
        } catch (Exception e) {
            PlatformDependent.throwException(e);
        }
        threadLocalMap.setIndexedVariable(index, v);
        addToVariablesToRemove(threadLocalMap, this);
        return v;
    }
     private static void addToVariablesToRemove(InternalThreadLocalMap threadLocalMap, FastThreadLocal<?> variable) {
        Object v = threadLocalMap.indexedVariable(variablesToRemoveIndex);
        Set<FastThreadLocal<?>> variablesToRemove;
        if (v == InternalThreadLocalMap.UNSET || v == null) {
            variablesToRemove = Collections.newSetFromMap(new IdentityHashMap<FastThreadLocal<?>, Boolean>());
            threadLocalMap.setIndexedVariable(variablesToRemoveIndex, variablesToRemove);
        } else {
            variablesToRemove = (Set<FastThreadLocal<?>>) v;
        }

        variablesToRemove.add(variable);
    }

InternalThreadLocalMap.indexedVariable()

  public Object indexedVariable(int index) {
        Object[] lookup = indexedVariables;
        return index < lookup.length? lookup[index] : UNSET;
    }

Recycler 会初始化一个FastThreadLocal

    private final FastThreadLocal<Stack<T>> threadLocal = new FastThreadLocal<Stack<T>>() {
        @Override
        protected Stack<T> initialValue() {
            return new Stack<T>(Recycler.this, Thread.currentThread(), maxCapacityPerThread, maxSharedCapacityFactor,ratioMask, maxDelayedQueuesPerThread);
        }
    };

Stack
maxCapacity = 32768
maxSharedCapacityFactor = 2
ratioMask = 7
maxDelayedQueues = 6
availableSharedCapacity=16384
INITIAL_CAPACITY=256

	Stack(Recycler<T> parent, Thread thread, int maxCapacity, int maxSharedCapacityFactor, int ratioMask, int maxDelayedQueues) {
	    this.parent = parent;
	    this.thread = thread;
	    this.maxCapacity = maxCapacity;
	    availableSharedCapacity = new AtomicInteger(max(maxCapacity / maxSharedCapacityFactor, LINK_CAPACITY));
	    elements = new DefaultHandle[min(INITIAL_CAPACITY, maxCapacity)];
	    this.ratioMask = ratioMask;
	    this.maxDelayedQueues = maxDelayedQueues;
	}

Stack.pop()

  DefaultHandle<T> pop() {
      int size = this.size;
      if (size == 0) {
          if (!scavenge()) {
              return null;
          }
          size = this.size;
      }
      size --;
      DefaultHandle ret = elements[size];
      elements[size] = null;
      if (ret.lastRecycledId != ret.recycleId) {
          throw new IllegalStateException("recycled multiple times");
      }
      ret.recycleId = 0;
      ret.lastRecycledId = 0;
      this.size = size;
      return ret;
  }
  
static final class Stack<T> {
  // we keep a queue of per-thread queues, which is appended to once only, each time a new thread other
  // than the stack owner recycles: when we run out of items in our stack we iterate this collection
  // to scavenge those that can be reused. this permits us to incur minimal thread synchronisation whilst
  // still recycling all items.
  final Recycler<T> parent;
  final Thread thread;
  final AtomicInteger availableSharedCapacity;
  final int maxDelayedQueues;
  private final int maxCapacity;
  private final int ratioMask;
  
  private DefaultHandle<?>[] elements;
  private int size;
  private int handleRecycleCount = -1; // Start with -1 so the first one will be recycled.
  private WeakOrderQueue cursor, prev;
  private volatile WeakOrderQueue head;

Stack.scavenge() 回收内存

  boolean scavenge() {
      // continue an existing scavenge, if any
      if (scavengeSome()) {
          return true;
      }

      // reset our scavenge cursor
      prev = null;
      cursor = head;
      return false;
  }

DefaultHandle

  static final class DefaultHandle<T> implements Handle<T> {
      private int lastRecycledId;
      private int recycleId;

      boolean hasBeenRecycled;

      private Stack<?> stack;
      private Object value;

      DefaultHandle(Stack<?> stack) {
          this.stack = stack;
      }

      @Override
      public void recycle(Object object) {
          if (object != value) {
              throw new IllegalArgumentException("object does not belong to handle");
          }
          stack.push(this);
      }
  }
源码来自:https://pan.quark.cn/s/a4b39357ea24 《C++ Primer》作为C++编程领域中的一部权威著作,主要服务于初学者和经验丰富的开发者,致力于帮助他们深入掌握C++的核心知识。 第一章通常会详细讲解C++语言的基础概念和语法结构,包括变量的使用、数据类型的分类、常量的定义、运算符的应用以及基础的输入输出操作。 接下来,我们将对这一章中的核心知识点和可能的习题解答进行深入分析。 ### 1. 变量与数据类型在C++编程中,变量被视为存储数据的媒介。 每一个变量都必须预先声明其数据类型,常见的数据类型有整型(int)、浮点型(float)、双精度浮点型(double)以及字符型(char)。 例如:```cppint age = 25; // 声明一个整型变量age并赋予其初始值25float weight = 70.5f; // 声明一个浮点型变量weight并赋予其初始值70.5char grade = A; // 声明一个字符型变量grade并赋予其初始值A```### 2. 常量与字面量常量指的是不可更改的值,可以通过`const`关键字进行声明。 例如:```cppconst int MAX_SIZE = 100; // 声明一个整型常量MAX_SIZE,其值为100```字面量是指程序中直接书写的值,如`42`、`3.14`或`"Hello"`。 ### 3. 运算符C++提供了多种运算符,涵盖了算术运算符(+,-,*,/,%)、比较运算符(==,!=,<,>,<=,>=)、逻辑运算符(&&,||,!)以及赋值运算符(=,+=,-=,*=,/=,%=)等。 ### 4. 输入与输出在C++中,使用`std::cin`来实现输...
内容概要:本文详细介绍了一个基于C++的仓库存储管理系统的设计与实现,涵盖了项目背景、目标、挑战及解决方案,并系统阐述了整体架构设计、数据库建模、功能模块划分、权限安全、并发控制、数据一致性保障、异常处理与可扩展性等关键内容。通过面向对象编程思想,采用分层架构与模块化解耦设计,结合STL容器、多线程、锁机制等C++核心技术,实现了高效的库存管理功能,包括入库、出库、盘点、调拨、权限控制、日志追踪与智能报表分析。文中还提供了核心类如Inventory(库存)、User(用户权限)、LogEntry(操作日志)及WarehouseManager(主控制器)的代码示例,展示了数据结构设计与关键算法逻辑。; 适合人群:具备C++编程基础,熟悉面向对象设计与基本数据结构的软件开发人员,尤其适合从事企业级管理系统开发或希望深入理解系统架构设计的中级开发者(工作1-3年);也适用于计算机相关专业学生进行课程设计或毕业项目参考; 使用场景及目标:①学习如何使用C++构建复杂业务系统的整体架构与模块划分方法;②掌握高并发、数据一致性、权限控制、异常处理等企业级系统关键技术的实现思路;③理解仓储管理业务流程及其在软件系统中的建模与落地方式;④为开发类似ERP、MES等后台管理系统提供技术原型与设计参考; 阅读建议:此资源不仅提供理论架构与代码片段,更强调系统设计的完整性与工程实践性。建议读者结合代码示例动手实现核心模块,深入理解类之间的关系与交互逻辑,重点关注多线程安全、事务管理与权限校验等难点环节,并尝试扩展功能如对接GUI界面或数据库持久化模块,以全面提升系统开发能力。
农作物叶子健康与疾病实例分割数据集 一、基础信息 数据集名称:农作物叶子健康与疾病实例分割数据集 图片数量: - 训练集:7,446张图片 - 验证集:970张图片 - 测试集:182张图片 - 总计:8,598张图片 分类类别: - Apple Healthy(健康苹果叶) - Apple Rust Leaf(苹果锈病叶) - Apple Scab Leaf(苹果黑星病叶) - BellPepper Healthy(健康甜椒叶) - BellPepper Leaf Spot(甜椒叶斑病) - Corn Gray Leaf Spot(玉米灰斑病) - Corn Leaf Blight(玉米叶枯病) - Corn Rust Leaf(玉米锈病叶) - Grape Black Rot(葡萄黑腐病) - Grape Healthy(健康葡萄叶) - Squash Powdery Leaf(南瓜白粉病叶) - Tomato Bacterial Spot(番茄细菌性斑点病) - Tomato Healthy(健康番茄叶) - Tomato Septoria(番茄斑枯病) 标注格式:YOLO格式,包含实例分割的多边形标注,适用于实例分割任务。 数据格式:图片来源于农业图像数据库,细节清晰。 二、适用场景 农业植物疾病AI检测系统开发:数据集支持实例分割任务,帮助构建能够自动识别植物叶子疾病区域并分类的AI模型,辅助农民快速诊断和治理。 精准农业应用研发:集成至农业智能管理系统中,提供实时疾病识别功能,为农作物健康管理提供决策支持。 学术研究与创新:支持农业科学与人工智能交叉领域的研究,助力发表高水平农业AI论文。 农业教育与培训:数据集可用于农业院校或培训机构,作为学生学习植物疾病分类和诊断的重要资源。 三、数据集优势 精准标注与多样性:每张图片均经过准确标注,确保疾病区域分割精确。包
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值