簡體   English   中英

這個C#類/方法線程安全和並發訪問安全嗎?

[英]Is this c# class/method thread-safe and concurrent access safe?

在下面的代碼中,附件對象是線程安全的嗎? 如果在應用程序運行多個線程時調用代碼對附件對象進行了更改,可能會發生什么?

謝謝。

public static class Util {
    public static int AddMessage(string message, IEnumerable<FileAttachment> attachments, Dictionary<int, object> customFieldValues = null, int? assigneeUserId = null) {
        foreach (FileAttachment current2 in attachments) {
            //do something
        }
    }
}

在下面的代碼中,附件對象是線程安全的嗎? 而且,如果在運行多個線程的應用程序中調用代碼對附件對象進行了更改,可能會發生什么?

沒有任何跡象表明attachments在這里是線程安全的。 如果其他線程可以訪問這些實例,並更改這些對象內的屬性,則可能會發生不良情況。

一個簡單的例子-如果實際的可枚舉碰巧是List<T> ,並且另一個線程添加或刪除了附件,則由於基礎集合的更改, foreach循環將在枚舉時拋出...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM