thread-safety

Should I acquire a lock on Properties first before looping setProperety?

余生颓废 提交于 2019-12-23 21:58:29
问题 The java class Properties is a thread-safe Class as per it's documentation: This class is thread-safe: multiple threads can share a single Properties object without the need for external synchronization. Because of that reason, I have the habit of maintaining my Properties in a HashMap implementation of Map , which is not thread-safe, but much more lightweight. More specifically, implementing thread-safety requires additional locking mechanisms, which will have an impact on performance. I can

How to wait for a spawned thread to finish in Python

六月ゝ 毕业季﹏ 提交于 2019-12-23 19:28:11
问题 I want to use threads to do some blocking work. What should I do to: Spawn a thread safely Do useful work Wait until the thread finishes Continue with the function Here is my code: import threading def my_thread(self): # Wait for the server to respond.. def main(): a = threading.thread(target=my_thread) a.start() # Do other stuff here 回答1: You can use Thread.join. Few lines from docs. Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is

Is find_or_create_by thread safe in Mongoid?

孤街浪徒 提交于 2019-12-23 19:12:01
问题 I have a web app that uses Mongoid's find_or_create_by method. poll = Poll.find_or_create_by(fields) Before we go into production, I am trying to run through the failure scenarios, and it occurred to me that multiple users could try to access this resource using this method. Is there any likelihood that it could create multiple instances of the same object? What can I do to prevent that? 回答1: Disclaimer: I'm new to Mongoid and Rails so I could be totally wrong. Looking at modifiable.rb and

Is this using of dictionary thread-safe or not?

北战南征 提交于 2019-12-23 18:53:09
问题 Here is two my variants of method, which returns a string, associated with enum value (kept in dictionary). First variant is slower, but thread-safe, second is faster, but i don't know, whether it is thread-safe or not. First: string GetStringForEnum (SomeEnum e) { string str = null; lock (someDictionary) //someDictionary is not used anywhere else (only in this method) { if (!someDictionary (e, out str)) { someDictionary.Add (e, "somehowCreatedString"); } return str; } Second variant: string

Thread safe way of reading a value from a dictionary that may or may not exist

∥☆過路亽.° 提交于 2019-12-23 18:11:32
问题 So, I've been spoiled by ConcurrentDictionary and it's awesome TryGetValue method. However, I'm constrained to using only regular Dictionary because this is in a portable class library targeting phone and other platforms. I'm trying to write a very limited subset of a Dictionary and exposing it in a thread-safe manner. I basically need something like GetOrAdd from ConcurrentDictionary. Right now, I have this implemented like: lock (lockdictionary) { if (!dictionary.ContainsKey(name)) { value

Dictionary as thread-safe variable

笑着哭i 提交于 2019-12-23 17:47:19
问题 I have a class (singleton) and it contains a static Dictionary private static Dictionary<string, RepositoryServiceProvider> repositoryServices = null; in the instance of this class I populate the dictionary (can occur from multiple threads). At first I had just RepositoryServiceProvider service = null; repositoryServices.TryGetValue(this.Server.Name, out service); if (service == null) { service = new RepositoryServiceProvider(this.Server); repositoryServices.Add(this.Server.Name, service); }

ReentrantReadWriteLock multiple reading threads

元气小坏坏 提交于 2019-12-23 15:23:59
问题 Good Day I have a question relating ReentrantReadWriteLocks. I am trying to solve a problem where multiple reader threads should be able to operate in parallel on a data structure, while one writer thread can only operate alone (while no reader thread is active). I am implementing this with the ReentrantReadWriteLocks in Java, however from time measurement it seems that the reader threads are locking each other out aswell. I don't think this is supposed to happen, so I am wondering if I

Using EnterCriticalSection in Thread to update VCL label

本小妞迷上赌 提交于 2019-12-23 15:05:05
问题 I'm new to threads. I'm using a 3rd party library that uses threads which at times call a procedure I've provided. How do I update update a TLabel.Caption from my procedure when its called by the thread? If I've called InitializeCriticalSection elsewhere, is it as simple as EnterCriticalSection(CritSect); GlobalVariable := 'New TLabel.Caption'; LeaveCriticalSection(CritSect); And then in my main thread: EnterCriticalSection(CritSect); Label1.Caption:= GlobalVariable; LeaveCriticalSection

the java concurrent modification exception debacle of 2010

老子叫甜甜 提交于 2019-12-23 12:44:00
问题 Painting some particles stored in an ArrayList. This code works fine: super.paintComponent(g); for (Particle b: particleArr){ g.setColor(b.getColor()); g.fillOval(b.getXCoor() + 5,b.getYCoor(), b.getParticleSize(),b.getParticleSize()); } However this code throws a concurrent modification exception: public void paintComponent(Graphics g){ //paint particles super.paintComponent(g); for (Particle b: particleArr){ g.setColor(b.getColor()); if (b.isDead()) particleArr.remove(b); else if (!b

What is the lifecycle of a jsp PageContext object - is it threadsafe?

狂风中的少年 提交于 2019-12-23 12:23:14
问题 Are jsp PageContext objects created and destroyed as part of the http request-response cycle or are they cached and reused between requests. PageContext has life-cycle methods that suggest reuse between requests. ie initialize(), release(). If they are reused this could pose serious concurrency problems: if two http requests arrive, requesting the same jsp page, and each request is processed by its own thread, but sets attributes on a shared PageContext object, they will render each others'