thread-safety

Java Concurrency Incrementing a Value

怎甘沉沦 提交于 2019-12-18 17:08:21
问题 I have been reading about volatile and synchronized in Java but have been scratching my head in confusion. I am hoping someone can help me clear up a problem private HashMap<String,int> map = new HashMap<String,int>(); In my thread if (map.get("value") == null) { map.put("value",0); } map.put("value",map.get("value")+1); My goal is to have all the threads share this map . If I add volatile it doesn't seem to fix the problem for me (I output and see that map is being override to each time). I

C# - Are Parameters Thread Safe in a Static Method?

蹲街弑〆低调 提交于 2019-12-18 17:02:22
问题 Is this method thread-safe? It seems as though it isn't... public static void Foo(string _str, Guid _id) { _str = _str + _id.ToString(); /* Do Stuff */ return } 回答1: The parameters are, in this case, two immutable values. Within a method, only a single thread is operating on the set of parameters, as multiple threads calling the method will each have their own stack and execution context, which means each thread has their own independent set of parameters and local variables, so no other

Should my Scala actors' properties be marked @volatile?

﹥>﹥吖頭↗ 提交于 2019-12-18 16:27:18
问题 In Scala, if I have a simple class as follows: val calc = actor { var sum = 0 loop { react { case Add(n) => sum += n case RequestSum => sender ! sum } } } Should my field sum be marked @volatile ? Whilst the actor is logically single-threaded (i.e. the messages are processed sequentially), the individual reactions may be happening on separate threads and hence the state variable may be being altered on one thread and then read from another. 回答1: You don't need to mark them as volatile. The

Why should you lock threads?

左心房为你撑大大i 提交于 2019-12-18 15:53:40
问题 I've read a lot of examples on locking threads.. but why should you lock them? From my understanding, when you initiate threads without joining them, they will compete with the main thread and all other threads for resources and then execute, sometimes simultaneously, sometimes not. Does locking ensure that threads DON'T execute simultaneously? Also, what wrong with threads executing simultaneous? Isn't that even better? (faster overall execution) When you lock threads, will it lock them all

@PersistenceContext EntityManager thread-safety in Spring and Java EE

纵然是瞬间 提交于 2019-12-18 15:33:46
问题 EntityManager is not thread-safe by definition. Servlets specs says that in non-distributed environment and without implementing SingleThreadModel , there is only one servlet instance per definition . Therefore, in Java EE when you inject an EntityManager through the @PersistenceContext into Servlet's field - it's not thread safe: public class MyServlet extends HttpServlet { // Not thread-safe, should be using EMF instead. @PersistenceContext private EntityManager em; } Is this correct to say

Log4Net FileAppender not thread safe?

有些话、适合烂在心里 提交于 2019-12-18 14:52:36
问题 I need to log to a file because the customer doesn't have a console of something where I can log to with log4net. Now I read that the FileAppender is not thread safe. Is there anyhow a way to log to file within an app that logs out of different threads or what would be a common alternative? 回答1: Log4Net itself is thread-safe even if FileAppender isn't - the framework manages everything for you. So long as you log in the normal way (rather than directly writing to the appender) you should be

Modifying list from another thread while iterating (C#)

天涯浪子 提交于 2019-12-18 13:23:11
问题 I'm looping through a List of elements with foreach, like this: foreach (Type name in aList) { name.doSomething(); } However, in an another thread I am calling something like aList.Remove(Element); During runtime, this causes an InvalidOperationException: Collection was modified; enumeration operation may not execute. What is the best way to handle this (I would perfer it to be rather simple even at the cost of performance)? Thanks! 回答1: Thread A: lock (aList) { foreach (Type name in aList) {

java.text.SimpleDateFormat not thread safe

橙三吉。 提交于 2019-12-18 13:20:57
问题 Synchronization Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally The above line is mentioned in the JavaDoc of SimpleDateFormat class. Does it mean that we should not create the SimpleDateFormat objects as Static. And If we create it as static, so wherever we are using this object we need to keep it in Synchronised Block. 回答1: Yes SimpleDateFormat is

Qt5: How to use qDebug() to log in a file, multi-thread application

十年热恋 提交于 2019-12-18 13:07:35
问题 I started using Qt5 few days ago. I needed a logger for my app and I decided to use qDebug , but it seems it has to be "redirected" in order to have the logs in a file. I used qInstallMessageHandler to do that and I wrote my own handler as presented below (inspired from other people here). It seems it works, but as I am not a guru, I have to ask: Is it ok to use this in a multi-thread application or not? Also, if it is ok/safe for using in a multi-thread app, can it be improved somehow? void

Turning off COW in GCC

…衆ロ難τιáo~ 提交于 2019-12-18 12:15:51
问题 I've known for a while that GCC uses COW (Copy-On-Write) for std::string , making it impossible to use std::string in a multi-threaded program. But as far as I know C++11 prohibits an implementation from using COW, because threads are now defined by the standard, and move semantics pretty much obsolete the need for COW anyway. Now, GCC 4.6 implements a great deal of the C++11 standard. Yet it seems that the implementation is still using COW semantics. This was brought to my attention by