- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
实际上,我正在开发一个应用程序,它使用多个线程来处理多个任务,父应用程序接收任务的输出,稍后写入文件,目前我正在使用 arraylist 将输出附加到,但是从队列中删除项目时这恰好很慢。 我正在考虑将 arraylist 部分转换为链表以优化读取和清除,任何人都可以确认我 如果 .Net 的 linkedList 实现是线程安全的,或者甚至使用 arraylist 也是线程安全的?
最佳答案
reference指出:
This type is not thread safe. If the LinkedList(Of T) needs to be accessed by multiple threads, you will need to implement their own synchronization mechanism.
A LinkedList(Of T) can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
编辑:
至于 ArrayList,reference状态:
Public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
An ArrayList can support multiple readers concurrently, as long as the collection is not modified. To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
关于.net - .Net 的 LinkedList 实现是线程安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5789291/
我是一名优秀的程序员,十分优秀!