think in java 浅谈 SerialNumberChecker+ SimpleMicroBenchmark+SynchronizationComparisons

本文主要是介绍think in java 浅谈 SerialNumberChecker+ SimpleMicroBenchmark+SynchronizationComparisons,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

think in java 浅谈 SerialNumberChecker+ SimpleMicroBenchmark+SynchronizationComparisons

11 SerialNumberChecker

  • 添加 synchronized 有效保证了线程安全
    • volatile not means thread safe
    • 有效后,就找不到重复的元素,程序进行循环,记得手动关闭

附核心的修改代码

public  static int nextSerialNumber() { //here modifyreturn serialNumber++; // Not thread-safe //    private static volatile int serialNumber = 0;}

12 实战 SimpleMicroBenchmark

先秀出结果

//本机结果
synchronized:  298930769
Lock:          251456214
Lock/synchronized = 0.841
//教材结果
/* Output: (75% match)
synchronized:  244919117
Lock:          939098964
Lock/synchronized = 3.834
*/
  • 结果和教材代码竟然不符合,教材上说75%情况下 Lock慢,但是我是隐形加锁的慢,并且二者在多次实验里差距不大了
  • TODO: 留待后续研究,建议减少数据量来测试

13 有意思的实战 SynchronizationComparisons

13.1 我的浅识

这里的结果输出
肯定是baseline最快。
次之是比较synchronized /Lock/Atomic的系统延迟

  • 原生代码运行的不是顺利
    分别对Baseline/Atomic 修改了回绕的边界,才完整跑过5轮测试

  • 大的数据量读/写环境下,synchronized 没有优势。在简单的生产者/消费者案例里面,
    在简单的生产者/消费者案例里面,趋势和教材一致。
    在我的电脑上(i5+4G Ram+ssd),跌倒到第二轮(100000),synchronized完全就落后于lock/atomic变量。
    以常见的 synchronized/ Lock 的测试为例,lock领先的优势始终保持在1.3x以下,没有出现源代码示范输出的大数据量的巨大落差(高达65倍)
    我想,某种情形下,大数据量,synchronized可以代替lock ??
    不过肯定的是,小数据量,synchronized比Lock看起来更简单 ,更快。

    待确定的是,要确定时间的消耗的主体,要确认时间的延迟是来自于锁。

  • Atomic的不适合性
    首先看原生代码的注释

        // Oops! Relying on more than one Atomic at// a time doesn't work. But it still gives us// a performance indicator:
同一地方使用不止一个原子类,无法正常工作。
  • 果然atomic的使用有局限性

13.2 作者原文结论解读

This program uses the Template Method design pattern24 to put all the common code in the

base class and isolate all the varying code in the derived class implementations of
accumulate( ) and read( ). In each of the derived classes SynchronizedTest, LockTest,
and AtomicTest, you can see how accumulate( ) and read( ) express different ways of
implementing mutual exclusion.

    模板 设计模式
In this program, tasks are executed via a FixedThreadPool in an attempt to keep all the

thread creation at the beginning, and prevent any extra cost during the tests. Just to make
sure, the initial test is duplicated and the first result is discarded because it includes the
initial thread creation.

    使用线程池,不计入线程创建的开销并且,第一次使用线程的时候,有一个热身的线程哈!

A CyclicBarrier is necessary because we want to make sure all the tasks have completed
before declaring each test complete.

    同步器CyclicBarrier,保证测试完之前运行完

A static clause is used to pre-load the array of random numbers, before any tests begin. This
way, if there is any overhead to generating random numbers, we won’t see it during the test.

Each time accumulate( ) is called, it moves to the next place in the array preLoaded
(wrapping to the beginning of the array) and adds another randomly generated number to
value. The multiple Modifier and Reader tasks provide contention on the Accumulator
object.

Notice that in AtomicTest, I observe that the situation is too complex to try to use Atomic
objects—basically, if more than one Atomic object is involved, you will probably be forced to
give up and use more conventional mutexes (the JDK documentation specifically states that
using Atomic objects only works when the critical updates for an object are confined to a
single variable). However, the test is left in place so that you can still get a feel for the
performance benefit of Atomic objects.

多个Atomic对象 不适合这里的应用,详细参考jdk说明

In main( ), the test is run repeatedly and you can decide to ask for more than five repetitions
(the default). For each repetition, the number of test cycles is doubled, so you can see how
the different mutexes behave when running for longer and longer times. As you can see from
the output, the results are rather surprising. For the first four iterations, the synchronized
keyword seems to be more efficient than using a Lock or an Atomic. But suddenly, a
threshold is crossed and synchronized seems to become quite inefficient, while Lock and
Atomic seem to roughly maintain their proportion to the BaseLine test, and therefore
become much more efficient than synchronized.

    锁的趋势

Keep in mind that this program only gives an indication of the differences between the
various mutex approaches, and the output above only indicates these differences on my
particular machine under my particular circumstances. As you can see if you experiment with
it, there can be significant shifts in behavior when different numbers of threads are used and
when the program is run for longer periods of time. Some hotspot runtime optimizations are
not invoked until a program has been running for several minutes, and in the case of server
programs, several hours.

其他考量因素:机器运行时的配置因素 | 影响gc的因素|jvm优化的因素等

That said, it is fairly clear that using Lock is usually significantly more efficient than using
synchronized, and it also appears that the overhead of synchronized varies widely, while
Locks are relatively consistent.

本节重点:syschronized不稳定,而lock更稳定。

Does this mean you should never use the synchronized keyword? There are two factors to
consider: First, in SynchronizationComparisons.java, the bodies of the mutexed
methods are extremely small. In general, this is a good practice—only mutex the sections that
you absolutely must. However, in practice the mutexed sections may be larger than those in
the above example, and so the percentage of time in the body will probably be significantly
bigger than the overhead of entering and exiting the mutex, and could overwhelm any benefit
of speeding up the mutex. Of course, the only way to know is— when you’re tuning for
performance, no sooner—to try the different approaches and see what impact they have.
Second, it’s clear from reading the code in this chapter that the synchronized keyword
produces much more readable code than the lock try/finally-unlock idiom that Locks
require, and that’s why this chapter primarily uses the synchronized keyword. As I’ve
stated elsewhere in this book, code is read much more than it is written—when
programming, it is more important to communicate with other humans than it is to
communicate with the computer—and so readability of code is critical. As a result, it makes
sense to start with the synchronized keyword and only change to Lock objects when you
are tuning for performance.

lock 和syschronize 的选择: 可读性 和 高效性的平衡

Finally, it’s nice when you can use the Atomic classes in your concurrent program, but be
aware that, as we saw in SynchronizationComparisons.java, Atomic objects are only
useful in very simple cases, generally when you only have one Atomic object that’s being
modified and when that object is independent from all other objects. It’s safer to start with
more traditional mutexing approaches and only attempt to change to Atomic later, if
performance requirements dictate

其他 好书推荐

dw的设计模式
thinking in patterns

这篇关于think in java 浅谈 SerialNumberChecker+ SimpleMicroBenchmark+SynchronizationComparisons的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/894380

相关文章

Springboot实现推荐系统的协同过滤算法

《Springboot实现推荐系统的协同过滤算法》协同过滤算法是一种在推荐系统中广泛使用的算法,用于预测用户对物品(如商品、电影、音乐等)的偏好,从而实现个性化推荐,下面给大家介绍Springboot... 目录前言基本原理 算法分类 计算方法应用场景 代码实现 前言协同过滤算法(Collaborativ

Python与Java交互出现乱码的问题解决

《Python与Java交互出现乱码的问题解决》在现代软件开发中,跨语言系统的集成已经成为日常工作的一部分,特别是当Python和Java之间进行交互时,编码问题往往会成为导致数据传输错误、乱码以及难... 目录背景:为什么会出现乱码问题产生的场景解决方案:确保统一的UTF-8编码完整代码示例总结在现代软件

Java 如何创建和使用ExecutorService

《Java如何创建和使用ExecutorService》ExecutorService是Java中用来管理和执行多线程任务的一种高级工具,可以有效地管理线程的生命周期和任务的执行过程,特别是在需要处... 目录一、什么是ExecutorService?二、ExecutorService的核心功能三、如何创建

springboot实现配置文件关键信息加解密

《springboot实现配置文件关键信息加解密》在项目配置文件中常常会配置如数据库连接信息,redis连接信息等,连接密码明文配置在配置文件中会很不安全,所以本文就来聊聊如何使用springboot... 目录前言方案实践1、第一种方案2、第二种方案前言在项目配置文件中常常会配置如数据库连接信息、Red

JavaScript时间戳与时间的转化常用方法

《JavaScript时间戳与时间的转化常用方法》在JavaScript中,时间戳(Timestamp)通常指Unix时间戳,即从1970年1月1日00:00:00UTC到某个时间点经过的毫秒数,下面... 目录1. 获取当前时间戳2. 时间戳 → 时间对象3. 时间戳php → 格式化字符串4. 时间字符

Java遍历HashMap的6种常见方式

《Java遍历HashMap的6种常见方式》这篇文章主要给大家介绍了关于Java遍历HashMap的6种常见方式,方法包括使用keySet()、entrySet()、forEach()、迭代器以及分别... 目录1,使用 keySet() 遍历键,再通过键获取值2,使用 entrySet() 遍历键值对3,

Java版本不兼容问题详细解决方案步骤

《Java版本不兼容问题详细解决方案步骤》:本文主要介绍Java版本不兼容问题解决的相关资料,详细分析了问题原因,并提供了解决方案,包括统一JDK版本、修改项目配置和清理旧版本残留等步骤,需要的朋... 目录错误原因分析解决方案步骤第一步:统一 JDK 版本第二步:修改项目配置第三步:清理旧版本残留兼容性对

Spring实现Bean的初始化和销毁的方式

《Spring实现Bean的初始化和销毁的方式》:本文主要介绍Spring实现Bean的初始化和销毁的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Bean的初始化二、Bean的销毁总结在前面的章节当中介绍完毕了ApplicationContext,也就

Java的"伪泛型"变"真泛型"后对性能的影响

《Java的伪泛型变真泛型后对性能的影响》泛型擦除本质上就是擦除与泛型相关的一切信息,例如参数化类型、类型变量等,Javac还将在需要时进行类型检查及强制类型转换,甚至在必要时会合成桥方法,这篇文章主... 目录1、真假泛型2、性能影响泛型存在于Java源代码中,在编译为字节码文件之前都会进行泛型擦除(ty

Java中的getBytes()方法使用详解

《Java中的getBytes()方法使用详解》:本文主要介绍Java中getBytes()方法使用的相关资料,getBytes()方法有多个重载形式,可以根据需要指定字符集来进行转换,文中通过代... 目录前言一、常见重载形式二、示例代码三、getBytes(Charset charset)和getByt