简体   繁体   English

同步代码比非同步代码执行得更快

[英]Synchronized code performs faster than unsynchronized one

I came out with this stunning result which i absolutely do not know the reason for: I have two methods which are shortened to: 我出来了这个惊人的结果,我绝对不知道原因:我有两种方法缩写为:

private static final ConcurrentHashMap<Double,Boolean> mapBoolean = 
        new ConcurrentHashMap<Double, Boolean>();
private static final ConcurrentHashMap<Double,LinkedBlockingQueue<Runnable>> map
        = new ConcurrentHashMap<Double, LinkedBlockingQueue<Runnable>>();


protected static <T> Future<T> execute(final Double id, Callable<T> call){
// where id is the ID number of each thread
synchronized(id)
{
   mapBoolean.get();// then do something with the result
   map.get();//the do somethign with the result
}
}

protected static <T> Future<T> executeLoosely(final Double id, Callable<T> call){

 mapBoolean.get();// then do something with the result
 map.get();//the do somethign with the result

 }

} }

On profiling with over 500 threads, and each thread calling each of the above methods 400 times each, I found out that execute(..) performs atleast 500 times better than executeLoosely(..) which is weird because executeLoosely is not synchronized and hence more threads can process the code simultaneously. 在使用超过500个线程进行性能分析,并且每个线程分别调用上述每个方法400次时,我发现execute(..)执行至少比executeLoosely(..)好500倍,这很奇怪,因为executeLoosely不同步,因此更多线程可以同时处理代码。

Any reasons?? 任何原因?

The overhead of using 500 threads on a machine which I assume doesn't have 500 cores, using tasks which takes about 100-1000x as long as a lookup on a Map to execute code which the JVM could detect doesn't do anything, is likely to produce a random outcome. 在我假设没有500个核心的机器上使用500个线程的开销,使用大约100-1000x的任务,只要在Map上执行查找以执行JVM可以检测到的代码就没有做任何事情,是可能产生随机结果。 ;) ;)

Another problem you could have is that a test which faster being performed with one thread can benefit from using synchronized because it biases access to one thread. 您可能遇到的另一个问题是,使用synchronized执行更快的测试可以从使用synchronized中受益,因为它会偏向对一个线程的访问。 ie it turns your multi-threaded test back into a single threaded one which is the fastest in the first place. 即它将您的多线程测试变回单线程测试,这是最快的。

You should compare the timings you get with a single thread doing a loop. 您应该将您获得的时间与执行循环的单个线程进行比较。 If this is faster (which I believe it would be) then its not a useful multi-threaded test. 如果这更快(我相信它会),那么它不是一个有用的多线程测试。

My guess is that you are running the synchronized code after the unsynchronised code. 我的猜测是你在非同步代码之后运行同步代码。 ie after the JVM has warmed up a little. 即JVM已经预热了一点。 Swap the order you perform these tests and run them many times and you will get different results. 交换执行这些测试的顺序并多次运行它们会得到不同的结果。

In the non synchronized scenario : 1) wait to acquire lock on a segment of the map, lock, perform operation on the map, unlock, wait to acquire lock on a segment of the other map, lock, perform operation on the other map, unlock. 在非同步场景中:1)等待获取地图段上的锁定,锁定,对地图执行操作,解锁,等待获取其他地图的某个段的锁定,锁定,在其他地图上执行操作,开锁。 The segment level locking will be performed only in cases of concurrent write to the segment which doesn't look to be the case in your example. 段级锁定仅在并发写入段的情况下执行,在您的示例中看起来不是这种情况。

In the synchronized scenario : 1) wait to lock, perform both the operations, unlock. 在同步方案中:1)等待锁定,执行两个操作,解锁。

The time taken for context switching can have an impact? 上下文切换所需的时间会产生影响吗? How many cores does the machine running the test have? 运行测试的机器有多少个核心? How are the maps structured, same sort of keys? 地图是如何构建的,同样的键?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同步的java代码执行速度比非同步代码快 - Synchronized java code performs times faster than unsynchronized one 为什么非同步对象的性能优于同步对象? - Why do unsynchronized objects perform better than synchronized ones? 声纳:将同步类“Hashtable”替换为非同步类,例如“HashMap” - Sonar: Replace the synchronized class "Hashtable" by an unsynchronized one such as "HashMap" 设计和编写可以执行不同步或同步任务的任务调度程序 - Design and code a task scheduler that can take unsynchronized or synchronized tasks 同步和非同步方法的ConcurrentModificationException - ConcurrentModificationException with synchronized and unsynchronized methods 当 AtomicInteger 比同步更快时 - When AtomicInteger is faster than synchronized 由于未同步的同步方法,导致ConcurrentModicationException - ConcurrentModicationException due to unsynchronized synchronized methods 同步语句和单独的非同步方法 - Synchronized statements and separate unsynchronized methods 同步块可以比Atomics更快吗? - Can synchronized blocks be faster than Atomics? 为什么一段 Java 代码在使用 synchronized 关键字时比不使用它更快? - Why is a block of Java code faster when using synchronized keyword, rather than without using it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM