什么是“非阻塞”并发,它是如何不同于正常的并发? [英] What is "non-blocking" concurrency and how is it different than normal concurrency?

查看:83
本文介绍了什么是“非阻塞”并发,它是如何不同于正常的并发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 什么是非阻塞并发,它如何与使用线程的正常并发不同?为什么在需要并发性的所有场景中我们不使用非阻塞并发?是否有使用非阻塞并发的开销?

  2. 我听说非阻塞并发在Java中可用。是否有任何特定情况下我们应该使用此功能?

  3. 使用这些方法之一与集合有什么区别或优势吗?


  4. Q3的示例:

      class List 
    {
    private final ArrayList< String> list = new ArrayList< String>();

    void add(String newValue)
    {
    synchronized(list)
    {
    list.add(newValue);
    }
    }
    }

    vs

      private final ArrayList< String> list = Collections.synchronizedList(); 

    问题更多来自学习/理解的观点。非常感谢您的关注。

    解决方案


    什么是非阻塞并发和
    不同。


    正式:


    计算机科学,非阻塞
    同步确保线程
    竞争一个共享资源不
    的执行无限期通过互斥推迟
    。 A
    非阻塞算法是无锁的,如果
    有保证系统范围的
    进度;等待自由,如果还有
    保证每线程进度。
    (wikipedia)


    非正式:非阻塞与阻塞的最有利的特征之一是线程不必被操作系统暂停/唤醒。这样的开销可以达到1ms到几十ms,因此删除这可以是一个巨大的性能增益。在java中,这也意味着您可以选择使用非公平锁定,这可以具有比公平锁定更多的系统吞吐量。


    我听说这是Java中的
    。是否有任何特定的
    情景,我们应该使用此功能


    是的,从Java5。事实上,在Java中,你应该基本上尽可能多地使用java.util.concurrent来满足你的需求(这种情况发生在使用非阻塞并发性很多,但你不必在大多数情况下显式担心)。只有没有其他选项,您应该使用同步包装器(.synchronizedList()等)或手动同步关键字。这样,大多数情况下,您可以使用更易维护,性能更好的应用。



    当存在大量争用时,非阻塞并发特别有利。你不能使用它,当你需要阻塞(公平锁定,事件驱动的东西,队列的最大长度等),但如果你不需要,非阻塞并发倾向于在大多数情况下表现更好。



    集合中使用这些方法之一是否有区别/优点。什么是权衡


    两者都有相同的行为(字节码应该相等)。但我建议使用 Collections.synchronized ,因为它较短=较小的房间拧紧!


    1. What is "non-blocking" concurrency and how is it different than normal concurrency using threads? Why don't we use non-blocking concurrency in all the scenarios where concurrency is required? Is there overhead for using non-blocking concurrency?
    2. I have heard that non-blocking concurrency is available in Java. Are there any particular scenarios where we should use this feature?
    3. Is there a difference or advantage to using one of these methods with a collection? What are the trade-offs?

    Example for Q3:

    class List   
    {  
        private final ArrayList<String> list = new ArrayList<String>();
    
        void add(String newValue) 
        {
            synchronized (list)
            {
                list.add(newValue);
            }
        }
    }  
    

    vs.

    private final ArrayList<String> list = Collections.synchronizedList(); 
    

    The questions are more from a learning/understanding point of view. Thanks for attention.

    解决方案

    What is Non-blocking Concurrency and how is it different.

    Formal:

    In computer science, non-blocking synchronization ensures that threads competing for a shared resource do not have their execution indefinitely postponed by mutual exclusion. A non-blocking algorithm is lock-free if there is guaranteed system-wide progress; wait-free if there is also guaranteed per-thread progress. (wikipedia)

    Informal: One of the most advantageous feature of non-blocking vs. blocking is that, threads does not have to be suspended/waken up by the OS. Such overhead can amount to 1ms to a few 10ms, so removing this can be a big performance gain. In java, it also means that you can choose to use non-fair locking, which can have much more system throughput than fair-locking.

    I have heard that this is available in Java. Are there any particular scenarios we should use this feature

    Yes, from Java5. In fact, in Java you should basically try to meet your needs with java.util.concurrent as much as possible (which happen to use non-blocking concurrency a lot, but you don't have to explicitly worry in most cases). Only if you have no other option, you should use synchronized wrappers (.synchronizedList() etc.) or manual synchronize keyword. That way, you end up most of the time with more maintainable, better performing apps.

    Non-blocking concurrency is particularly advantageous when there is a lot of contention. You can't use it when you need blocking (fair-locking, event-driven stuff, queue with maximum length etc.), but if you don't need that, non-blocking concurrency tends to perform better in most conditions.

    Is there a difference/advantage of using one of these methods for a collection. What are the trade offs

    Both have the same behavior (the byte code should be equal). But I suggest to use Collections.synchronized because it's shorter = smaller room to screw up!

    这篇关于什么是“非阻塞”并发,它是如何不同于正常的并发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆