thread-safety

Do I need to synchronize access to immutable types in Java?

拟墨画扇 提交于 2019-12-20 04:23:04
问题 Let's say I have this class: class Zoo { protected String bearName; protected Double trainerSalary; protected Integer monkeyCount; } Can one thread write to these fields, and another one read them, without requiring synchronized access to the Zoo object? Note: these values can be treated separate from one another, so it doesn't matter that the trainerSalary is changed while the monkeyCount is read. EDIT : Just to clarify, the fields are mutable ; only their referenced objects are immutable .

Realm thread safe object with singleton

谁说胖子不能爱 提交于 2019-12-20 03:44:15
问题 So I have a singleton class that manages the current user that is logged into my application. It looks like this class SessionManager { static let shared = SessionManager() var loggedInUser: User? } The problem is that if I want to get that user object from a background thread, I can't as I get the following exception: 'RLMException', reason: 'Realm accessed from incorrect thread.' After some googling I found Thread Safe Reference's. So I changed my class to be something like this instead:

Is Java BitSet thread safe for concurrent readonly operations

我只是一个虾纸丫 提交于 2019-12-20 03:18:13
问题 I have multiple threads in my application accessing a BitSet concurrently. The documentation says: A BitSet is not safe for multithreaded use without external synchronization. It doesn't say if it is not safe for reading or writing. Can anybody explain. 回答1: A BitSet is only safe for read-only operations if there is a "happens before" relationship between the last action that initializes the BitSet and the actions that read it. The simplest way to achieve this is using a final . For example:

Threading and deadlock in Swing application

大憨熊 提交于 2019-12-20 03:16:43
问题 I'm getting a deadlock in the Swing application I maintain, and although I have a workaround that appears to work, I'm not sure that I have understood what I am doing and haven't just hidden a race condition that may pop up again later. A thread trace shows the deadlock is occurring between two threads, AWT-EventQueue-0 and AWT-EventQueue-1. My first question is which if either of these is the infamous Event Dispatching Thread. Both threads have the following at the bottom of their stack

Why this broken program always runs?

邮差的信 提交于 2019-12-20 03:12:48
问题 I was trying examples from JCIP and Below program should not work but even if I execute it say 20 times it always work which means ready and number are becoming visible even if it should in this case public class NoVisibility { private static boolean ready; private static int number; private static class ReaderThread implements Runnable { public void run() { while (!ready) Thread.yield(); System.out.println(number); } } public static void main(String[] args) { System.out.println(Runtime

Thread-safety of static initializers in C#

丶灬走出姿态 提交于 2019-12-19 19:04:14
问题 Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { public static object GetNewObject() { /* arbitrary code that returns a new object */ } } Which of the following does C# guarantee, when MyStaticClass.myField is not yet initialized? If threads 1 and 2 try to access myField together (in that order),

Thread-safety of static initializers in C#

三世轮回 提交于 2019-12-19 19:03:58
问题 Everyone says static initializers are thread-safe, but I'm worried about a particular detail. Let's say I have static class MyStaticClass { public static readonly object myField = MyOtherClass.GetNewObject(); } static class MyOtherClass { public static object GetNewObject() { /* arbitrary code that returns a new object */ } } Which of the following does C# guarantee, when MyStaticClass.myField is not yet initialized? If threads 1 and 2 try to access myField together (in that order),

Why is a static thread_local object in C++ constructed twice?

别等时光非礼了梦想. 提交于 2019-12-19 17:38:18
问题 This code: #include <iostream> #include <thread> #include <mutex> struct Singl{ Singl(Singl const&) = delete; Singl(Singl&&) = delete; inline static thread_local bool alive = true; Singl(){ std::cout << "Singl() " << std::this_thread::get_id() << std::endl; } ~Singl(){ std::cout << "~Singl() " << std::this_thread::get_id() << std::endl; alive = false; } }; static auto& singl(){ static thread_local Singl i; return i; } struct URef{ ~URef(){ const bool alive = singl().alive; std::cout << alive

Thread safety and System.Text.Encoding in C#

余生长醉 提交于 2019-12-19 12:24:40
问题 Is it safe to use the same Encoding object from different threads? By "using" I mean, calling Encoding.GetString() , Encoding.GetBytes() and write some XML with an XmlWriter (created by something like XmlWriter.Create(myStream, new XmlWriterSettings() { Encoding = myEncoding }) . The msdn site states that "Any instance members are not guaranteed to be thread safe". So, how can I safely write two XML documents concurrently? (thank you!!) 回答1: Yes, it should be safe to use the same Encoding

Jersey, Guice and Hibernate - EntityManager thread safety

烂漫一生 提交于 2019-12-19 12:20:06
问题 I have used this tutorial them same way in my application: http://www.benmccann.com/hibernate-with-jpa-annotations-and-guice/ My app is JAX-RS web service which will receive many concurrent requests and make updates to database. GenericDAOImpl.java implementation: public class GenericDAOImpl<T> implements GenericDAO<T> { @Inject protected EntityManager entityManager; private Class<T> type; public GenericDAOImpl(){} public GenericDAOImpl(Class<T> type) { this.type = type; } @Override public