Consider the following code. Thread A calls run(), then another thread B calls test() and there shouldn’t be any happens-before relationship. I know it is not guaranteed that thread B sees the changes which thread A made. But is it possible that the output of this program is: Answer Yes, it’s possible, because it’s not explicitly forbidden. The read of
Tag: thread-safety
Java async MySQL queries
First of all, I don’t have much experience with thread safe programming. I have a MySQL class, and I want to use one instance in multiple threads to prevent blocking code in the main thread. I read about connection pooling but I want to keep it as simple as it is. This is my MySQL class: Is it possible to
Volatile Keyword & the thread local memory [closed]
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 10 years ago.
How to ensure thread safety of utility static method?
Is there any general way or rules exits by which we can ensure the thread safety of static methods specifically used in various Utility classes of any applications. Here I want to specifically point out the thread safety of Web Applications. It is well know that static methods with Immutable Objects as parameters are thread safe and Mutable Objects are
Why are local variables thread safe in Java
I was reading multi-threading in Java and I come across this Local variables are thread safe in Java. Since then I have been thinking How/Why local variables are thread safe. Can somebody please let me know. Answer When you create a thread it will have its own call stack created. Two threads will have two stacks and one thread never
Using a synchronizedSet to synchronize access between two threads
I’m not able to synchronize two threads using a set: and passing it to two threads. One accessing: and another updating: What happens is that [1], [2], [3] happens in sequence. During [1], it is correct that the set doesn’t have yet the item I’m looking for. But then [2] updates it by adding the item. And during [3], I
I was searching for stopping java thread gracefully and found that, but I cannot know how to check example of this situation
This is good example of stopping thread. How to stop a java thread gracefully? But when I try to check this example I received infinite loop. This is my code: Answer I think you need to start your thread – not run it. By calling run, you are just making a normal method call, not running a separate thread.
Thread Safe Singletons in Java
The wikipedia article on Singletons mentions a few thread safe ways to implement the structure in Java. For my questions, let’s consider Singletons that have lengthy initialization procedures and are acccessed by many threads at once. Firstly, is this unmentioned method thread-safe, and if so, what does it synchronize on? Secondly, why is the following implementation thread safe AND lazy
singleton pattern in java. lazy initialization
1.is there a flaw with the above implementation of the getInstance method? 2.What is the difference between the two implementations.? I have seen a lot of answers on the singleton pattern in stackoverflow but the question I have posted is to know mainly difference of ‘synchronize’ at method and block level in this particular case. Answer 1.is there a flaw