性能上的同步与重入锁定

性能上的同步与重入锁定

本文介绍了性能上的同步与重入锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于多线程系统的Queue实现,我遇到了一些惊喜。这是: -

I have been through a set of few surprises when it comes to Queue implementation for a Multithreading system. Here is:-

场景: -
1生产者,1个消费者: - 生产者将整数放入队列。消费者只是将其从队列中删除。

The Scenario:-1 producer, 1 consumer:- A producer puts an integer into a queue. A consumer simply removes it from the queue.

队列的基础数据结构: -
TreeSet(我从未想过会使用),LinkedList, LinkedBlockingQueue(具有不确定的大小)

The underlying data structure of the queue:-TreeSet (which I never thought I will use), LinkedList, LinkedBlockingQueue(with indefinite size)

代码: - TreeSet的
作为队列: -

The code:-of TreeSet as a queue:-

while (i < 2000000) {
        synchronized (objQueue) {

            if (!(objQueue.size() > 0)) {
                try {
                    objQueue.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            Integer x = objQueue.first();
            if (x != null) {
                objQueue.remove(x);
                ++i;
            }
        }
    }

编辑: -

      while (i < 2000000) {
        synchronized (objQueue) {
            objQueue.add(i);
            ++i;
            objQueue.notify();
        }
    }

对于LinkedBlockingQueue: -

For LinkedBlockingQueue:-

     while (i < 2000000){
        try {
            objQueue.put(i);
            ++i;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            Thread.currentThread().interrupt();
        }
    }

      while (i < 2000000) {
        try {
            objQueue.take();
            ++i;

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            Thread.currentThread().interrupt();
        }
    }

对于LinkedList: - 类似代码同步。

For LinkedList :- similar code with synchronized.

问题: -

1)当我通过Visual VM测量性能时,我观察到了生产者代码,TreeSet比LinkedBlockingQueue和LinkedList表现更好,即使它花费O(log n)时间,在Linked结构中创建对象也是一个重要的开销。为什么理论与实践有很大不同?为什么我们更喜欢在队列实现中使用Tree结构的Linked,Array结构?

1) When I measured the performance via Visual VM, I observed that the for the producer code, TreeSet performs better than LinkedBlockingQueue and LinkedList, even though it takes O(log n) time, the creation of objects in Linked structures is a significant overhead. Why is the theory quite different to the practice ? Why do we prefer Linked, Array structures over Tree structures in queue implementations ?

2)由于TreeSet比LinkedList表现得更好,所以synchronized作为ReeentrantLock的明显赢家出现了表现优于LinkedBlockingQueue。我希望我可以附加Visual VM结果。它不是与文章投票,

2) The synchronized comes out as a clear winner vs the ReeentrantLock because TreeSet performed better than LinkedList which performed better than LinkedBlockingQueue. I wish I could attach the Visual VM results. It is not in votes with the article, http://www.ibm.com/developerworks/java/library/j-jtp10264/index.html

操作在

Dell Vostro 1015,核心2 duo 2.10,2GB Ram,32位操作系统和

Dell Vostro 1015, core 2 duo 2.10, 2GB Ram with 32 bit operating system and with

JVM:Java HotSpot(TM)客户端VM(20.1 -b02,混合模式)
Java:版本1.6.0_26,供应商Sun Microsystems Inc.

JVM: Java HotSpot(TM) Client VM (20.1-b02, mixed mode)Java: version 1.6.0_26, vendor Sun Microsystems Inc.

推荐答案

1。 ReentrantLock 可能更容易使用,如果您需要实现遍历链表的线程,锁定下一个节点然后解锁当前节点。

1. ReentrantLock might be more apt to use if you need to implement a thread that traverses a linked list, locking the next node and then unlocking the current node.

2。 已同步关键字适用于某种情况例如锁定粗化,提供自适应旋转,偏置锁定以及通过逃逸分析锁定省略的可能性。这些优化目前尚未针对ReentrantLock实施。

2. Synchronized keyword is apt in situation such as lock coarsening, provides adaptive spinning,biased locking and the potential for lock elision via escape analysis. Those optimizations aren't currently implemented for ReentrantLock.

对于正确的效果比较,请参阅:

这篇关于性能上的同步与重入锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:24