久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

什么是故障安全&Java中的快速失敗迭代器

What are fail-safe amp; fail-fast Iterators in Java(什么是故障安全amp;Java中的快速失敗迭代器)
本文介紹了什么是故障安全&Java中的快速失敗迭代器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

Java 中有兩種類型的迭代器:fail-safe 和 fail-fast.

There are two types of iterators in Java: fail-safe and fail-fast.

這是什么意思,它們之間有什么區(qū)別?

What does this mean, and is the difference between them?

推薦答案

它們有什么區(qū)別...

What is the difference between them ...

故障安全"(在工程中)意味著某事以一種不會造成損害或造成損害最小的方式發(fā)生故障.嚴(yán)格來說,在 Java 中不存在作為故障安全迭代器的東西.如果迭代器失敗(通常意義上的失敗"),您可以預(yù)期會發(fā)生損壞.

"Fail-safe" (in engineering) means that something fails in a way that causes no or minimal damage. Strictly speaking, there is no such thing in Java as a fail-safe iterator. If an iterator fails (in the normal sense of "fail"), you can expect damage to occur.

我懷疑你的意思實際上是弱一致".迭代器.javadoc 說:

I suspect that you actually mean "weakly consistent" iterators. The javadoc says:

大多數(shù)并發(fā) Collection 實現(xiàn)(包括大多數(shù)隊列)也不同于通常的 java.util 約定,因為它們的 Iterators 和 Spliterators 提供弱一致而不是快速失敗遍歷."p>

通常,弱一致性意味著如果集合與迭代同時被修改,則迭代所看到的內(nèi)容的保證會更弱.(詳細(xì)信息將在每個并發(fā)集合類 javadocs 中指定.)

Typically, weak consistency means that if a collection is modified concurrently with an iteration, the guarantees of what the iteration sees are weaker. (The details will be specified in each concurrent collection classes javadocs.)

快速失敗"(在系統(tǒng)設(shè)計中)表示積極檢查故障條件,以便故障條件(在可能的情況下1)在造成過多損壞之前檢測到.在 Java 中,快速失敗的迭代器會因拋出 ConcurrentModificationException 而失敗.

"Fail-fast" (in systems design) means that the failure condition is checked aggressively so that the failure condition is (where possible1) detected before too much damage can be done. In Java, a fail-fast iterator fails by throwing a ConcurrentModificationException.

快速失敗"的替代方案;和弱一致"是迭代失敗不可預(yù)測的語義;例如有時給出錯誤的答案或拋出意外的異常.(這是早期 Java 版本中 Enumeration API 的一些標(biāo)準(zhǔn)實現(xiàn)的行為.)

The alternative to "fail-fast" and "weakly consistent" is semantic where the iteration fails unpredictably; e.g. to sometimes give the wrong answer or throw an unexpected exception. (This was the behavior of some standard implementations of the Enumeration API in early versions of Java.)

...它們與我們用于收集的迭代器有什么不同.

... and are they different from the iterator we use for collection.

沒有.這些是標(biāo)準(zhǔn)集合類型實現(xiàn)的迭代器的屬性;即它們要么快速失敗",要么快速失敗".或弱一致"... 在同步和 Java 內(nèi)存模型方面正確使用時1.

No. These are properties of the iterators implemented by standard Collection types; i.e. they are either "fail fast" or "weakly consistent" ... when used correctly with respect to synchronization and the Java memory model1.

快速失敗的迭代器通常使用集合對象上的 volatile 計數(shù)器實現(xiàn).

Fail-fast iterators are typically implemented using a volatile counter on the collection object.

  • 當(dāng)集合更新時,計數(shù)器會增加.
  • 創(chuàng)建Iterator時,計數(shù)器的當(dāng)前值嵌入到Iterator對象中.
  • 當(dāng)執(zhí)行 Iterator 操作時,該方法會比較兩個計數(shù)器值,如果它們不同則拋出 CME.
  • When the collection is updated, the counter is incremented.
  • When an Iterator is created, the current value of the counter is embedded in the Iterator object.
  • When an Iterator operation is performed, the method compares the two counter values and throws a CME if they are different.

相比之下,弱一致性迭代器通常是輕量級的,并利用每個并發(fā)集合的內(nèi)部數(shù)據(jù)結(jié)構(gòu)的屬性.沒有通用模式.如果您有興趣,請閱讀不同集合類的源代碼.

By contrast, weakly consistent iterators are typically light-weight and leverage properties of each concurrent collection's internal data structures. There is no general pattern. If you are interested, read the source code for different collection classes.

1 - 快速失敗迭代器行為假設(shè)應(yīng)用程序在同步和內(nèi)存模型方面已正確實現(xiàn).(換句話說,應(yīng)用程序 是線程安全的.)例如,如果您在沒有正確同步的情況下迭代 ArrayList,快速失敗"會導(dǎo)致機制應(yīng)該檢測并發(fā)修改(盡管不能保證),但可能無法防止由于應(yīng)用程序的不安全行為而損壞列表.為了說明,javadoc for Vector.iterator() 是這樣說的:

1 - The rider is that fail-fast iterator behavior assumes that the application is correctly implemented with respect to synchronization and the memory model. (In other words, the application is thread-safe.) For example, if you iterated an ArrayList without proper synchronization, the "fast fail" mechanism should detect the concurrent modification (though that isn't guaranteed), but may not prevent the list from being corrupted due to the application's unsafe behavior. To illustrate, the javadoc for Vector.iterator() says this:

無法保證迭代器的快速失敗行為,因為一般來說,在存在不同步的并發(fā)修改的情況下無法做出任何硬保證.快速失敗的迭代器會盡最大努力拋出 ConcurrentModificationException.因此,編寫一個依賴此異常來確保其正確性的程序是錯誤的:迭代器的快速失敗行為應(yīng)僅用于檢測錯誤."

"The fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs."

這篇關(guān)于什么是故障安全&Java中的快速失敗迭代器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 免费在线精品视频 | 国产农村妇女毛片精品久久麻豆 | 成人午夜网站 | 欧美日韩一二三区 | 亚洲欧美综合精品久久成人 | 久草免费在线视频 | 狠狠ri| 在线观看日韩精品视频 | 一区二区三区精品视频 | 欧美性网 | 午夜影院在线观看 | 国产精品区二区三区日本 | 男人视频网站 | 日本免费黄色 | 国产一区不卡在线观看 | 香蕉大人久久国产成人av | 亚洲一级av毛片 | 国产在线一区二区三区 | 九九久久国产精品 | 精品自拍视频 | 亚洲在线视频 | 国产精品99久久久久久www | 日韩午夜 | 日韩a| 中文字幕免费观看 | 日韩乱码一二三 | 福利视频三区 | 国产亚洲精品91 | 黑人性hd| 综合久久综合久久 | 国产一级片av | 午夜电影福利 | 一级片在线免费看 | 久久看精品 | 色综合99| 毛片一级片 | 中文字幕视频在线 | aa级毛片毛片免费观看久 | 国产伦精品一区二区 | 日日夜夜精品 | 午夜精品久久 |