为什么Java Vector被认为是遗留类,已过时或已弃用?
在使用并发时,它的使用是否有效?
如果我不想手动同步对象而只想使用线程安全的集合而不需要制作底层数组的新副本(就像CopyOnWriteArrayList那样),那么它可以使用Vector吗?
怎么样Stack,这是一个子类Vector,我应该怎么用,而不是它?
我有一个ArrayList实例化并在后台线程上填充(我用它来存储Cursor数据).同时,它可以在主线程上访问,并通过使用foreach进行迭代.所以这显然可能导致抛出异常.
我的问题是什么是使这个类字段线程安全的最佳实践,而不是每次都复制它或使用标志?
class SomeClass {
private final Context mContext;
private List<String> mList = null;
SomeClass(Context context) {
mContext = context;
}
public void populateList() {
new Thread(new Runnable() {
@Override
public void run() {
mList = new ArrayList<>();
Cursor cursor = mContext.getContentResolver().query(
DataProvider.CONTENT_URI, null, null, null, null);
try {
while (cursor.moveToNext()) {
mList.add(cursor.getString(cursor.getColumnIndex(DataProvider.NAME)));
}
} catch (Exception e) {
Log.e("Error", e.getMessage(), e);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}).start();
} …Run Code Online (Sandbox Code Playgroud) 我想使用集合类来添加,删除和检索多个线程中的对象.
Collections.synchronizedList并且Vector这两个类都是线程安全的.有哪位告诉我之间的区别Collections.synchronizedList,并Vector解释说,我什么时候应该使用与Vector和Collections.synchronizedList?
如果多个线程访问向量,vector将确保只有一个线程可以同时访问该向量.SynchronizedList是相同的.那有什么区别?如何在某些同步情况下选择?
java ×4
vector ×2
android ×1
arraylist ×1
collections ×1
deprecated ×1
list ×1
obsolete ×1
stack ×1