thread-safety

Java hashmap readonly thread safety

久未见 提交于 2020-01-01 12:09:38
问题 I have this code that has a shared hash map initialized in static block. I don't expose the hashmap and it's used read-only (get and containKey). I wanted to make sure if this is thread-safe. public class MyClass { private static final Map<String, MyObject> myMap; static { myMap = new MyLoader().load() } public MyClass() { if (containsKey(someKey)) { // do something } myMap.get(something) } static boolean containsKey(String key) { // do some other stuff return myMap.containsKey(key) } } 回答1:

Separate Logic Thread From Event Dispatch Thread

霸气de小男生 提交于 2020-01-01 08:57:49
问题 This is the smallest runnable SSCCE ,of my project, that I could implement to show you. I've read that calling the game logic from the Event Dispacth Thread is a bad practice , how can I separate them, because as you can see update() and repaint() are related into loop and how can I separate code in a pretty way, I'm getting in trouble with this, trying to find out how to do it. I've posted a similar question regarding and I got an answer,that says to use a Swing Timer,but i have huge task to

Is javax.naming.InitialContext ThreadSafe

点点圈 提交于 2020-01-01 08:35:20
问题 Currently I am use following code to lookup EJB3 sateless session beans for normal POJO class. (We are in JEE5 so we can not inject Stateless Session Beans in normal POJO class I have to use look up) import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.log4j.Logger; public Object getEJB(String jndiName) { logger.debug("WEBSPHERE EJB Lookup : " + jndiName); String modifiedJndiName = ""; Hashtable<Object, Object> properties =

Thread-safe static variables without mutexing?

邮差的信 提交于 2020-01-01 08:28:52
问题 I remember reading that static variables declared inside methods is not thread-safe. (See What about the Meyer's singleton? as mentioned by Todd Gardner) Dog* MyClass::BadMethod() { static Dog dog("Lassie"); return &dog; } My library generates C++ code for end-users to compile as part of their application. The code it generates needs to initialize static variables in a thread-safe cross-platform manner. I'd like to use boost::call_once to mutex the variable initialization but then end-users

Thread-safe static variables without mutexing?

只谈情不闲聊 提交于 2020-01-01 08:28:20
问题 I remember reading that static variables declared inside methods is not thread-safe. (See What about the Meyer's singleton? as mentioned by Todd Gardner) Dog* MyClass::BadMethod() { static Dog dog("Lassie"); return &dog; } My library generates C++ code for end-users to compile as part of their application. The code it generates needs to initialize static variables in a thread-safe cross-platform manner. I'd like to use boost::call_once to mutex the variable initialization but then end-users

How to easy make this counter property thread safe?

*爱你&永不变心* 提交于 2020-01-01 08:27:52
问题 I have property definition in class where i have only Counters, this must be thread-safe and this isn't because get and set is not in same lock, How to do that? private int _DoneCounter; public int DoneCounter { get { return _DoneCounter; } set { lock (sync) { _DoneCounter = value; } } } 回答1: If you're looking to implement the property in such a way that DoneCounter = DoneCounter + 1 is guaranteed not to be subject to race conditions, it can't be done in the property's implementation. That

Best practice for eventbus with thread safety

拜拜、爱过 提交于 2020-01-01 06:11:31
问题 My app has activities for the user interaction and a background service which is the only place where the data model is being modified. The background service listens to actions that where made by the user as well as incoming messages from the network. Therefore concurrency issues can arise which I try to prevent by using a handler. For the event layer I use greenrobots Eventbus. This is all working well but I wonder if there is a smarter/faster/less code extensive (and therefore less error

SqlConnection Thread-Safe?

喜你入骨 提交于 2020-01-01 05:09:09
问题 I have a Log class which put logs in Windows journal and in a SQL table. In order to optimize my code, I would like use only one SqlConnection . In MSDN, it says: Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe. My question is : private static readonly SqlConnection conn = new SqlConnection(ConfigParameters.Instance.UIDConnection); Is it thread-safe ? If yes, when use Open() and Close() ? If no, how use

Keras “pickle_safe”: What does it mean to be “pickle safe”, or alternatively, “non picklable” in Python?

旧巷老猫 提交于 2020-01-01 05:02:29
问题 Keras fit_generator() has a parameter pickle_safe which defaults to False . Training can run faster if it is pickle_safe, and accordingly set the flag to True ? According to Kera's docs: pickle_safe : If True, use process based threading. Note that because this implementation relies on multiprocessing, you should not pass non picklable arguments to the generator as they can't be passed easily to children processes. I don't understand exactly what this is saying. How can I determine if my

msdn: What is “Thread Safety”?

て烟熏妆下的殇ゞ 提交于 2020-01-01 04:48:08
问题 In many MSDN documents, this is written under the Thread Safety heading; "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." for example; here can someone explain it please in a rather simple way? Thank you :) 回答1: Eric Lippert has an excellent blog post about this. Basically it's somewhat meaningless on its own. Personally I don't trust MSDN too much on this front, when I see that boiler-plate. It doesn