Java中同步和非同步集合类有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21507858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between synchronized and non-synchronized collection classes in Java?
提问by Incredible
I was reading about Java's collections and then I read this line:
我正在阅读有关 Java 的集合,然后我阅读了这一行:
"None of the collection classes are synchronized, but as you will see later in this chapter, it is possible to obtain synchronized versions."
“没有一个集合类是同步的,但是正如您将在本章后面看到的,可以获得同步版本。”
Can anyone tell me what is the difference between synchronized and non-synchronized collections in Java?
谁能告诉我 Java 中同步和非同步集合有什么区别?
回答by Paul Draper
A synchronized collection implies that the class is thread safe. (You can have non-synchronized collections that are also thread safe, but that is a topic for about thousand theses another day.)
同步集合意味着该类是线程安全的。(您可以拥有也是线程安全的非同步集合,但这是另一天大约一千篇论文的主题。)
The collections synchronize mutations by obtaining locks to make sure that other threads don't corrupt the state.
集合通过获取锁来同步突变,以确保其他线程不会破坏状态。
Basically, use the non-synchronized versions, unless you have multiple threads.
基本上,使用非同步版本,除非您有多个线程。
(And if you don't know, a thread is essentially a line of execution within a program. Some programs have multiple threads, all sharing the same code and memory.)
(如果您不知道,线程本质上是程序中的一条执行线。有些程序有多个线程,所有线程共享相同的代码和内存。)
回答by spgodara
Collection classes are not synchronized by default. But if you want a synchronized collection, you can use static method java.util.Collections.synchronizedCollection(Collection<T> c)
. It will create wrapper over your collection object. So, actually, your collection object will not be synchronized, but you will access your object's method via synchronized methods in wrapper object.
默认情况下不同步集合类。但是如果你想要一个同步的集合,你可以使用静态方法 java.util.Collections.synchronizedCollection(Collection<T> c)
。它将在您的集合对象上创建包装器。因此,实际上,您的集合对象不会被同步,但是您将通过包装器对象中的同步方法访问对象的方法。
回答by shubham gaikwad
Non synchronized-It is not-thread safe and can't be shared between many threads without proper synchronization code. While, Synchronized- It is thread-safe and can be shared with many threads.
非同步- 它不是线程安全的,如果没有适当的同步代码,就不能在多个线程之间共享。虽然, 同步- 它是线程安全的,可以与许多线程共享。
回答by A.N.Gupta
In Synchronization, If we are executing something then we need to wait for it to finish before moving to the another task.
在同步中,如果我们正在执行某件事,那么我们需要等待它完成,然后再转移到另一个任务。
Collection classes are not synchronized by default. The collection object is mutable that means once creating the object and that object is calling two threads at a time but one thread is changing the value of the object then it can be effected by another object. So, it is not thread safe.
默认情况下不同步集合类。集合对象是可变的,这意味着一旦创建对象并且该对象一次调用两个线程,但是一个线程正在更改对象的值,然后它可以受另一个对象的影响。所以,它不是线程安全的。
We can explicitly synchronized collection using static method java.util.Collections.synchronizedCollection(Collection<T> c)
我们可以使用静态方法java.util.Collections.synchronizedCollection(Collection<T> c)显式同步集合
回答by Sahan Amarsha
Synchronized basically means that only one threadcan access methods of that particular class at any given time. StringBuffer is an example of a synchronized class. A Synchronized class is a thread-safe class.
同步基本上意味着在任何给定时间只有一个线程可以访问该特定类的方法。StringBuffer 是同步类的一个示例。Synchronized 类是线程安全类。
Non-Synchronized means that two or more threadscan access the methods of that particular class at any given time. StringBuilder is an example of a non-synchronized class. Generally, a non-synchronized class is not thread-safe. (but some non-synchronized classes are thread-safe)
非同步意味着两个或多个线程可以在任何给定时间访问该特定类的方法。StringBuilder 是一个非同步类的例子。通常,非同步类不是线程安全的。(但一些非同步类是线程安全的)