I have this static class: that have lots of methods and this one above is one of these. what I would like to do is that if two callers write to the same file, the writing should be done in mutual exclusion. But when callers write to different files, this doesn’t happen (they don’t write in mutual exclusion). This versione
Tag: synchronized
is synchronized needed in getValue() ? & volatile needed?
I’ve a class in multithreading application: Answer The keyword volatile gives you the visibility aspects and without that you may read some stale value. A volatile read adds a memory barrier such that the compiler, hardware or the JVM can’t reorder the memory operations in ways that would violate the visibility guarantees provided by the memory model. According to the
Visibility of mutable object under different locks in Java
mFeaute is a mutable object. I want to know if the change of mFeature in setFeature(Feature feature) is visible to mFeature in useFeature(…) with a different explicit form of synchronized. Thanks. Answer The above code is suffering from a data race and hence is broken. You do not have a happens before edge between the write and the read of
How Can I print like below using thread synchronization method?
I have used thread Synchronization method to print ASCII code and its value like below example. Ex:- A 65 B 66 C 67 . . . . Z 90 But the output is this. Following are the two threads. Thread 1 Thread 2 Main What can I do for it without changing main method? Answer This can be solved using
Implementing a resource read/write lock in Java
I’m trying to implement a simple read/write lock for a resource accessed concurrently by multiple threads. The workers randomly try reading or writing to a shared object. When a read lock is set, workers should not be able to write until the lock is released. When a write lock is set, read and write are not permitted. Although my implementation
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
why using volatile with synchronized block?
I saw some examples in java where they do synchronization on a block of code to change some variable while that variable was declared volatile originally .. I saw that in an example of singleton class where they declared the unique instance as volatile and they sychronized the block that initializes that instance … My question is why we declare