Thread safety of a Dictionary with multiple concurrent readers and no writers

For your future reference, the documentation is here: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx It says: A Dictionary 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 … Read more

Is the Session object from Python’s Requests library thread safe?

After reviewing the source of requests.session, I’m going to say the session object might be thread-safe, depending on the implementation of CookieJar being used. Session.prepare_request reads from self.cookies, and Session.send calls extract_cookies_to_jar(self.cookies, …), and that calls jar.extract_cookies(…) (jar being self.cookies in this case). The source for Python 2.7’s cookielib acquires a lock (threading.RLock) while it … Read more

Inflate a view in a background thread

The LayoutInflater does not make any assumptions about what thread it runs on. And nothing is mentioned about this in its documentation. Also its code seams to be thread-agnostic. On the other hand, Views that are created by LayoutInflater might instantiate Handlers in their constructors. Well, they probably shouldn’t do that, but there is no … Read more

ConcurrentHashMap: avoid extra object creation with “putIfAbsent”?

Java 8 introduced an API to cater for this exact problem, making a 1-line solution: public void record(String key, String value) { entries.computeIfAbsent(key, k -> Collections.synchronizedList(new ArrayList<String>())).add(value); } For Java 7: public void record(String key, String value) { List<String> values = entries.get(key); if (values == null) { entries.putIfAbsent(key, Collections.synchronizedList(new ArrayList<String>())); // At this point, there … Read more

Do I need to lock or mark as volatile when accessing a simple boolean flag in C#?

Firstly, threading is tricky ;-p Yes, despite all the rumours to the contrary, it is required to either use lock or volatile (but not both) when accessing a bool from multiple threads. For simple types and access such as an exit flag (bool), then volatile is sufficient – this ensures that threads don’t cache the … Read more

Why is List not thread-safe?

You really need to classify Java’s Vector’s type of thread safety. Javas Vector is safe to be used from multiple threads because it uses synchronization on the methods. State will not be corrupted. However, Java’s vector’s usefulness is limited from multiple threads without additional synchronization. For example, consider the simple act of reading an element … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)