问题描述
- 什么是非阻塞并发,它如何与使用线程的正常并发不同?为什么在需要并发性的所有场景中我们不使用非阻塞并发?是否有使用非阻塞并发的开销?
- 我听说非阻塞并发在Java中可用。是否有任何特定情况下我们应该使用此功能?
- 使用这些方法之一与集合有什么区别或优势吗?
- 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?
- I have heard that non-blocking concurrency is available in Java. Are there any particular scenarios where we should use this feature?
- Is there a difference or advantage to using one of these methods with a collection? What are the trade-offs?
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();
问题更多来自学习/理解的观点。非常感谢您的关注。
正式:
非正式:非阻塞与阻塞的最有利的特征之一是线程不必被操作系统暂停/唤醒。这样的开销可以达到1ms到几十ms,因此删除这可以是一个巨大的性能增益。在java中,这也意味着您可以选择使用非公平锁定,这可以具有比公平锁定更多的系统吞吐量。
是的,从Java5。事实上,在Java中,你应该基本上尽可能多地使用java.util.concurrent来满足你的需求(这种情况发生在使用非阻塞并发性很多,但你不必在大多数情况下显式担心)。只有没有其他选项,您应该使用同步包装器(.synchronizedList()等)或手动同步
关键字。这样,大多数情况下,您可以使用更易维护,性能更好的应用。
当存在大量争用时,非阻塞并发特别有利。你不能使用它,当你需要阻塞(公平锁定,事件驱动的东西,队列的最大长度等),但如果你不需要,非阻塞并发倾向于在大多数情况下表现更好。
两者都有相同的行为(字节码应该相等)。但我建议使用 Collections.synchronized
,因为它较短=较小的房间拧紧!
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.
Formal:
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.
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.
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!
这篇关于什么是“非阻塞”并发,它是如何不同于正常的并发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!