- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用netty项目中的benchmark io.netty.microbench.concurrent.FastThreadLocalFastPathBenchmark。
当我直接运行它时,Beachmark的结果显示FastThreadLocal比ThreadLocal快得多。
但是,当我将 get() 方法更改为 set() 方法时,Beachmark 的结果显示 FastThreadLocal 并不比 ThreadLocal 快多少。
这是结果:
Benchmark Mode Cnt Score Error Units
FastThreadLocalFastPathBenchmark.fastThreadLocal thrpt 20 1233.992 ± 101.533 ops/s
FastThreadLocalFastPathBenchmark.jdkThreadLocalGet thrpt 20 1203.562 ± 77.841 ops/s
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.microbench.concurrent;
import io.netty.microbench.util.AbstractMicrobenchmark;
import io.netty.util.concurrent.FastThreadLocal;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Threads;
import java.util.Random;
/**
* This class benchmarks the fast path of FastThreadLocal and the JDK ThreadLocal.
*/
@Threads(4)
@Measurement(iterations = 10, batchSize = 100)
public class FastThreadLocalFastPathBenchmark extends AbstractMicrobenchmark {
private static final Random rand = new Random();
@SuppressWarnings("unchecked")
private static final ThreadLocal<Integer>[] jdkThreadLocals = new ThreadLocal[128];
@SuppressWarnings("unchecked")
private static final FastThreadLocal<Integer>[] fastThreadLocals = new FastThreadLocal[jdkThreadLocals.length];
static {
for (int i = 0; i < jdkThreadLocals.length; i ++) {
final int num = rand.nextInt();
jdkThreadLocals[i] = new ThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return num;
}
};
fastThreadLocals[i] = new FastThreadLocal<Integer>() {
@Override
protected Integer initialValue() {
return num;
}
};
}
}
@Benchmark
public int jdkThreadLocalGet() {
int result = 0;
for (ThreadLocal<Integer> i: jdkThreadLocals) {
i.set(rand.nextInt());
}
return result;
}
@Benchmark
public int fastThreadLocal() {
int result = 0;
for (FastThreadLocal<Integer> i: fastThreadLocals) {
i.set(rand.nextInt());
}
return result;
}
}
获取()结果:
Benchmark Mode Cnt Score Error Units
FastThreadLocalFastPathBenchmark.fastThreadLocal thrpt 20 51634.074 ± 6936.996 ops/s
FastThreadLocalFastPathBenchmark.jdkThreadLocalGet thrpt 20 53212.778 ± 660.392 ops/s
最佳答案
嗯,FastThreadLocal
针对 get
进行了优化,而不是针对 set
,所以您所看到的一般来说并不奇怪。
关于java - 为什么netty FastThreadLocal的基准测试结果没有比ThreadLocal快多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57884851/
我一直在读这个article关于 Tomcat 中的 ThreadLocal 泄漏。第一个示例包含以下代码: public class MyCounter { private int
天真地,我希望 ThreadLocal 是 Thread 到值类型的某种 WeakHashMap。所以当我了解到一个 ThreadLocal 的值实际上是 saved in a map in the
一 简单例子 1 代码 package concurrent.threadlocal; /** * ThreadLocal测试 * * @author cakin */ public class T
并发编程-ThreadLocal 说在前面的话 今天的文章很短,但是很经典,值得你仔细阅读每一个文字… 正如我开篇所说,我们要整理一些java并发编程的学习文档,这一篇就是第七篇:ThreadLoca
最近接了一个新需求,业务场景上需要在原有基础上新增2个字段,接口新增参数意味着很多类和方法的逻辑都需要改变,需要先判断是否属于该业务场景,再做对应的逻辑。原本的打算是在入口处新增变量,在操作数据的时
前言 ThreadLocal为变量在每个线程中都创建了一个副本,所以每个线程可以访问自己内部的副本变量,不同线程之间不会互相干扰。本文会基于实际场景介绍ThreadLocal如何使用以及内部实现机
来源:blog.csdn.net/mycs2012/article/details/90898128 1、FastThreadLocal的引入背景和原理简介 2、实现源码分析 2.1、Unpadded
来源:blog.csdn.net/mycs2012/article/details/90898128 1、FastThreadLocal的引入背景和原理简介 2、实现源码分析 2.1、Unpadded
.Net 4. ThreadLocal<> 实现 IDisposable。但似乎调用 Dispose() 实际上并没有释放对所持有的线程本地对象的引用。 这段代码重现了这个问题: using Syst
在类(class)ReentrantReadWriteLock以下是奇怪的评论: transient ThreadLocalHoldCounter readHolds; Sync() { re
如果我们有一个 ThreadLocal 属性(每个线程都有其唯一的属性),那么哪个是正确的(我们不想使用自动 setter/getter): A) private ThreadLocal _someP
我正在浏览 ThreadLocal类文档,想知道它可以在什么场景下使用。 首先我认为它可以用于那些我们有第三方/遗留类并且我们想要处理同步问题的场景。然后我查看了 ThreadLocal 的其他示例,
我最近读了一篇关于 Equation Group's Sophisticated Hacking 的文章确凿的证据是一个常量,也出现在 JDK 8 源代码中,例如ThreadLocal.java HA
我的基于 ThreadLocal 的类遇到问题。任何帮助,将不胜感激。这是一个带有简单列表的基类: public class ThreadLocalTest { protected static fi
使用ThreadLocal类编程时,字段应该声明为final吗?如果编写类似 private ThreadLocal threadLocal 的代码稍后在构造函数中初始化它,因为变量 threadLo
我有以下类(class) 这个类用来保存我所有的ThreadLocal数据成员 public class ThreadLocalManager { public static final Th
在一次工作 session 上。我听说Thread Local绝对是一种反模式,因为新的应用程序服务器使用称为新IO的新线程技术。事实上,他们告诉我ThreadLocal的问题是一个完整的线程必须等待
ThreadLocal 是否会自动清除为已完成的线程创建的值? 最佳答案 是的,这些变量可用于垃圾收集,但前提是没有其他对这些值的引用(由其他线程持有)。但是当你说一个线程完成时,它不应该像一个线程池
我意识到 ThreadLocal 已被多次访问,尤其是 SimpleDateFormat 示例。 但似乎即使将 SDF 设置为“ThreadLocal”,我们仍然为每个线程创建一个 SDF() 实例,
在 JSR 315 中添加了对 servlet 和过滤器的异步支持。在这样的过滤器中创建的 ThreadLocal 线程安全吗? ResourceFilter com.app.fil
我是一名优秀的程序员,十分优秀!