Java equivalence of Intrinsic and explicit Global locking - java

I want to know if the two scenarios are equivalent or not in a multithreaded environment.
private final Lock globalLock = new ReentrantLock();
Scenario 1
public void addListener( Listener listener ) {
globalLock.lock();
try{
//blah blah
}finally {
globalLock.unlock();
}
}
public void removeListener( Listener listener ) {
globalLock.lock();
try{
//blah blah
}finally {
globalLock.unlock();
}
}
Scenario 2
public synchronized addListener( Listener listener ) {
}
public synchronized removeListener( Listener listener ) {
}
I think the first scenario has race conditions in a multithreaded environment because if Thread A calls addListener() method before Thread B calls removeListener(), there is still the potential for Thread B to acquire the lock before Thread A even though Thread A invoked addListener() before Thread B invoked removeListener(). Is this hypothesis correct or Java guarantees that at least one statement of the method will be executed before the Thread is scheduled out.

They are functionally equivalent (assuming globalLock is a final instance variable).
In your first example, if two threads call the two methods simultaneously, only one of them will be able to acquire the lock and the second one will have to wait until the first one releases the lock.

It's not clear what global lock is. If it is an instance field
private final Lock globalLock = new ReentrantLock();
public void addListener( Listener listener ) {
globalLock.lock();
...
then the answer is yes, these two scenarios are equivalent

They are not exactly equivalent in a multithreaded environment.
Yes functionally they are same as suggested by assylias.
But their is big difference among the two approaches.
That big difference is that in scenario 1 your lock is private.
and hence its a good practice because u r sure nobody else can own your lock.
While the scenario is not a very good approach( except in some cases) because
any other user can hold the lock any longer as now your lock is public.
So functionally equivalent but by design they are different.

Related

Synchronizing on this and on other object

I think this question must have been asked somewhere but when I look for it I unfortunately find only different topics. Anyway here is the code:
public class A {
Object lockX = new Object();
Object lockY = new Object();
Object lockZ = new Object();
int c1;
int c2;
public void foo1() {
synchronized(lockX) {
c1++;
}
}
public void bar1() {
synchronized(lockY) {
c1++;
}
}
public void foo2() {
synchronized(lockZ) {
c2++;
}
}
public void bar2() {
synchronized(this) {
c2++;
}
}
}
Basically foo1 and bar1 are incorrect. They use different lock to protect c1, so in fact c1 won't be protected and both those functions can run concurrently. My question however is about foo2 and bar2. Are they ok? They also use different locks but bar2 is locking whole object, so does it prevents modifing c2 concurrently?
bar2 is locking whole object
Once you properly understand the semantics of mutual exclusion locks (mutexes), you will realize that this is an empty statement. A mutex doesn't have any inherent scope: a thread either does or does not hold it at any point in time. Synchronizing on this simply acquires the mutex associated with the this instance. Synchronizing on lockZ acquires an entirely independent mutex, and both can be acquired at the same time.
foo2 and bar2 are similar to foo1 and bar1. In case of bar2 the lock is on class A object where as foo2 is using lock of object lockZ.
The object this doesn't become locked, rather the object this is used as the mutex and the body is prevented from executing concurrently with other code sections also synchronized on this.
The quick answer is: NO. Even c2 is not protected for concurrent access because is locked by different object instances. Moreover a better form for
public void bar2() {
synchronized(this) {
c2++;
}
}
is
synchronized public void bar2() {
c2++;
}
As mentioned in #Marko's the semantics of locking is different from what you already conceive.
Synchronizing on an object X does not mean you are applying an access/method execution restriction on object X itself. When a certain thread A synchronizes over an instance X, it restricts other threads from synchronizing on the same instance X until the thread A finishes execution inside the synchronized(X) block and thus releasing the lock over object X. To make it more clear, only one thread at a time can acquire a lock on a single immutable instance of an object, and until this lock is released, any other thread that is trying to acquire a lock on the same instance will block.
Think of a synchronized block as a room with a door, and this door opens with a key, when a thread wants to enter the room and do stuff inside, it takes a key, opens the door then goes inside but keeping the key with it, other threads that want to enter the room with the same key have to wait on the door until the thread inside finishes its business, leaves the room and sets the key free for other threads to use it. If another thread uses a different key, while another thread is already in the room, it can enter the same room and do its business inside while the other thread is still in there and thus those two threads can modify the same stuff in the room, which may cause what is described as a race condition.
So in your case the answer is : No.

Thread structure in Java

I've got few questions about threads in Java. Here is the code:
TestingThread class:
public class TestingThread implements Runnable {
Thread t;
volatile boolean pause = true;
String msg;
public TestingThread() {
t = new Thread(this, "Testing thread");
}
public void run() {
while (pause) {
//wait
}
System.out.println(msg);
}
public boolean isPause() {
return pause;
}
public void initMsg() {
msg = "Thread death";
}
public void setPause(boolean pause) {
this.pause = pause;
}
public void start() {
t.start();
}
}
And main thread class:
public class Main {
public static void main(String[] args) {
TestingThread testingThread = new TestingThread();
testingThread.start();
testingThread.initMsg();
testingThread.setPause(false);
}
}
Question list:
Should t be volatile?
Should msg be volatile?
Should setPause() be synchronized?
Is this a good example of good thread structure?
You have hit quite a subtlety with your question number 2.
In your very specific case, you:
first write msg from the main thread;
then write the volatile pause from the main thread;
then read the volatile pause from the child thread;
then read msg from the child thread.
Therefore you have transitively established a happens-before relationship between the write and the read of msg. Therefore msg itself does not have to be volatile.
In real-life code, however, you should avoid depending on such subtle behavior: better overapply volatile and sleep calmly.
Here are some relevant quotes from the Java Language Specification:
If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).
If an action x synchronizes-with a following action y, then we also have hb(x, y).
Note that, in my list of actions,
1 comes before 2 in program order;
same for 3 and 4;
2 synchronizes-with 3.
As for your other questions,
ad 1: t doesn't have to be volatile because it's written to prior to thread creation and never mutated later. Starting a thread induces a happens-before on its own;
ad 3: setPause does not have to be synchronized because all it does is set the volatile var.
> Should msg be volatile?
Yes. Does it have to be in this example, No. But I urge you to use it anyway as the codes correctness becomes much clearer ;) Please note that I am assuming that we are discussing Java 5 or later, before then volatile was broken anyway.
The tricky part to understand is why this example can get away without msg being declared as volatile.
Consider this order part of main().
testingThread.start(); // starts the other thread
testingThread.initMsg(); // the other thread may or may not be in the
// while loop by now msg may or may not be
// visible to the testingThread yet
// the important thing to note is that either way
// testingThread cannot leave its while loop yet
testingThread.setPause(false); // after this volatile, all data earlier
// will be visible to other threads.
// Thus even though msg was not declared
// volatile it will piggy back the pauses
// use of volatile; as described [here](http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#volatile)
// and now the testingThread can leave
// its while loop
So if we now consider the testingThread
while (pause) { // pause is volatile, so it will see the change as soon
// as it is made
//wait
}
System.out.println(msg); // this line cannot be reached until the value
// of pause has been set to false by the main
// method. Which under the post Java5
// semantics will guarantee that msg will
// have been updated too.
> Should t be volatile?
It does not matter, but I would suggest making it private final.
> Should setPause() be synchronized?
Before Java 5, then yes. After Java 5 reading a volatile has the same memory barrier as entering a synchronized block. And writing to a volatile has the same memory barrier as at the end of a synchronized block. Thus unless you need the scoping of a synchronized block, which in this case you do not then you are fine with volatile.
The changes to volatile in Java 5 are documented by the author of the change here.
1&2
Volatile can be treated something like as "synchronization on variable",though the manner is different, but the result is alike, to make sure it is read-consistent.
3.
I feel it does not need to, since this.pause = pause should be an atomic statement.
4.
It is a bad example to do any while (true) {do nothing}, which will result in busy waiting, if you put Thread.sleep inside, which may help just a little bit. Please refer to http://en.wikipedia.org/wiki/Busy_waiting
One of a more appropriate way to do something like "wait until being awaken" is using the monitor object(Object in java is a monitor object), or using condition object along with a lock to do so. You may need to refer to http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html
Also, I don't think it is good idea either, that you have a local filed of thread inside your custom Runnable . Please refer to Seelenvirtuose 's comment.

Are java getters thread-safe?

Is is okay to synchronize all methods which mutate the state of an object, but not synchronize anything which is atomic? In this case, just returning a field?
Consider:
public class A
{
private int a = 5;
private static final Object lock = new Object();
public void incrementA()
{
synchronized(lock)
{
a += 1;
}
}
public int getA()
{
return a;
}
}
I've heard people argue that it's possible for getA() and incrementA() to be called at roughly the same time and have getA() return to wrong thing. However it seems like, in the case that they're called at the same time, even if the getter is synchronized you can get the wrong thing. In fact the "right thing" doesn't even seem defined if these are called concurrently. The big thing for me is that the state remains consistent.
I've also heard talk about JIT optimizations. Given an instance of the above class and the following code(the code would be depending on a to be set in another thread):
while(myA.getA() < 10)
{
//incrementA is not called here
}
it is apparently a legal JIT optimization to change this to:
int temp = myA.getA();
while(temp < 10)
{
//incrementA is not called here
}
which can obviously result in an infinite loop.
Why is this a legal optimization? Would this be illegal if a was volatile?
Update
I did a little bit of testing into this.
public class Test
{
private int a = 5;
private static final Object lock = new Object();
public void incrementA()
{
synchronized(lock)
{
a += 1;
}
}
public int getA()
{
return a;
}
public static void main(String[] args)
{
final Test myA = new Test();
Thread t = new Thread(new Runnable(){
public void run() {
while(true)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myA.incrementA();
}
}});
t.start();
while(myA.getA() < 15)
{
System.out.println(myA.getA());
}
}
}
Using several different sleep times, this worked even when a is not volatile. This of course isn't conclusive, it still may be legal. Does anyone have some examples that could trigger such JIT behaviour?
Is is okay to synchronize all methods which mutate the state of an object, but not synchronize anything which is atomic? In this case, just returning a field?
Depends on the particulars. It is important to realize that synchronization does two important things. It is not just about atomicity but it is also required because of memory synchronization. If one thread updates the a field, then other threads may not see the update because of memory caching on the local processor. Making the int a field be volatile solves this problem. Making both the get and the set method be synchronized will as well but it is more expensive.
If you want to be able to change and read a from multiple threads, the best mechanism is to use an AtomicInteger.
private AtomicInteger a = new AtomicInteger(5);
public void setA(int a) {
// no need to synchronize because of the magic of the `AtomicInteger` code
this.a.set(a);
}
public int getA() {
// AtomicInteger also takes care of the memory synchronization
return a.get();
}
I've heard people argue that it's possible for getA() and setA() to be called at roughly the same time and have getA() return to wrong thing.
This is true but you can get the wrong value if getA() is called after setA() as well. A bad cache value can stick forever.
which can obviously result in an infinite loop. Why is this a legal optimization?
It is a legal optimization because threads running with their own memory cache asynchronously is one of the important reasons why you see performance improvements with them. If all memory accesses where synchronized with main memory then the per-CPU memory caches would not be used and threaded programs would run a lot slower.
Would this be illegal if a was volatile?
It is not legal if there is some way for a to be altered – by another thread possibly. If a was final then the JIT could make that optimization. If a was volatile or the get method marked as synchronized then it would certainly not be a legal optimization.
It's not thread safe because that getter does not ensure that a thread will see the latest value, as the value may be stale. Having the getter be synchronized ensures that any thread calling the getter will see the latest value instead of a possible stale one.
You basically have two options:
1) Make your int volatile
2) Use an atomic type like AtomicInt
using a normal int without synchronization is not thread safe at all.
Your best solution is to use an AtomicInteger, they were basically designed for exactly this use case.
If this is more of a theoretical "could this be done question", I think something like the following would be safe (but still not perform as well as an AtomicInteger):
public class A
{
private volatile int a = 5;
private static final Object lock = new Object();
public void incrementA()
{
synchronized(lock)
{
final int tmp = a + 1;
a = tmp;
}
}
public int getA()
{
return a;
}
}
The short answer is your example will be thread-safe, if
the variable is declared as volatile, or
the getter is declared as synchronized.
The reason that your example class A is not thread-safe is that one can create a program using it that doesn't have a "well-formed execution" (see JLS 17.4.7).
For instance, consider
// in thread #1
int a1 = A.getA();
Thread.sleep(...);
int a2 = A.getA();
if (a1 == a2) {
System.out.println("no increment");
// in thread #2
A.incrementA();
in the scenario that the increment happens during the sleep.
For this execution to be well-formed, there must be a "happens before" (HB) chain between the assignment to a in incrementA called by thread #2, and the subsequent read of a in getA called by thread #1.
If the two threads synchronize using the same lock object, then there is a HB between one thread releasing the lock and a second thread acquiring the lock. So we get this:
thread #2 acquires lock --HB-->
thread #2 reads a --HB-->
thread #2 writes a --HB-->
thread #2 releases lock --HB-->
thread #1 acquires lock --HB-->
thread #1 reads a
If two threads share a a volatile variable, there is a HB between any write and any subsequent read (without an intervening write). So we typically get this:
thread #2 acquires lock --HB-->
thread #2 reads a --HB-->
thread #2 writes a --HB-->
thread #1 reads a
Note that incrementA needs to be synchronized to avoid race conditions with other threads calling incrementA.
If neither of the above is true, we get this:
thread #2 acquires lock --HB-->
thread #2 reads a --HB-->
thread #2 writes a // No HB!!
thread #1 reads a
Since there is no HB between the write by thread #2 and the subsequent read by thread #1, the JLS does not guarantee that the latter will see the value written by the former.
Note that this is a simplified version of the rules. For the complete version, you need to read all of JLS Chapter 17.

Java synchronized keyword - Does it protect a class method from being executed at the same time?

I need some clarificaton on how the syncronized keyword works in java 6.
Using the following example class.
class Carrier {
private String[] _collection = new String[2];
public Carrier() {
this._collection[0] = "abc";
this._collection[1] = "123";
}
public syncronized void change(int cId) {
Thread.sleep(3000);
this._collection[cId] = "changed";
}
}
Now, some place in the application, referencing the same object instance of the Carrier class, the .change() method is called, possibly at the same time.
...carrier.change(1);
...
...carrier.change(1);
Will the syncronized keyword prevent asyncronized execution of the method? Will it simply queue the call to .change(), waiting for each one to complete?
Yes, the second thread will block while the first thread is executing. That's because they both try to acquire the monitor on the same object - via this, in this case. So your change method is equivalent to:
public void change(int cId) {
synchronized (this) {
Thread.sleep(3000);
this._collection[cId] = "changed";
}
}
Personally I don't like synchronizing on "this" as it means any other code with access to the object itself could acquire the same monitor. I prefer to create an object just for locking which only code within the class has access to:
private final Object lock = new Object();
public void change(int cId) {
synchronized (lock) {
Thread.sleep(3000);
this._collection[cId] = "changed";
}
}
That will still have the same effect in terms of two threads calling change on the same object, because both threads will still be acquiring the same monitor - just the one associated with the lock-specific object.
Yes, it will prevent the two method calls from executing at the same time.
That is really what the main use of the synchronized keyword is.
When one synchronized method runs, it obtains a 'lock' on the object it is called on. That means no other synchronized code can be run on that object while that method holds the lock. Once the method is done executing, it will release the lock, and other synchronized methods can obtain a lock on that object.
Yes, and yes. (Note that it's synchronized, not syncronized)
Yes - the synchronized keyword prevents simultaneous execution of the method by different threads.
Note: A "class method" is a static method. An "instance method" (ie non-static) is what you have in your question.
synchronized are used for preventing simultaneously accesses in cuncurrent programming.
For example when you have to client, one that reads and another that writes:
Lock here for an exhaustive explanation.

Java - threads + action

I'm new to Java so I have a simple question that I don't know where to start from -
I need to write a function that accepts an Action, at a multi-threads program , and only the first thread that enter the function do the action, and all the other threads wait for him to finish, and then return from the function without doing anything.
As I said - I don't know where to begin because,
first - there isn't a static var at the function (static like as in c / c++ ) so how do I make it that only the first thread would start the action, and the others do nothing ?
second - for the threads to wait, should I use
public synchronized void lala(Action doThis)
{....}
or should i write something like that inside the function
synchronized (this)
{
...
notify();
}
Thanks !
If you want all threads arriving at a method to wait for the first, then they must synchronize on a common object. It could be the same instance (this) on which the methods are invoked, or it could be any other object (an explicit lock object).
If you want to ensure that the first thread is the only one that will perform the action, then you must store this fact somewhere, for all other threads to read, for they will execute the same instructions.
Going by the previous two points, one could lock on this 'fact' variable to achieve the desired outcome
static final AtomicBoolean flag = new AtomicBoolean(false); // synchronize on this, and also store the fact. It is static so that if this is in a Runnable instance will not appear to reset the fact. Don't use the Boolean wrapper, for the value of the flag might be different in certain cases.
public void lala(Action doThis)
{
synchronized (flag) // synchronize on the flag so that other threads arriving here, will be forced to wait
{
if(!flag.get()) // This condition is true only for the first thread.
{
doX();
flag.set(true); //set the flag so that other threads will not invoke doX.
}
}
...
doCommonWork();
...
}
If you're doing threading in any recent version of Java, you really should be using the java.util.concurrent package instead of using Threads directly.
Here's one way you could do it:
private final ExecutorService executor = Executors.newCachedThreadPool();
private final Map<Runnable, Future<?>> submitted
= new HashMap<Runnable, Future<?>>();
public void executeOnlyOnce(Runnable action) {
Future<?> future = null;
// NOTE: I was tempted to use a ConcurrentHashMap here, but we don't want to
// get into a possible race with two threads both seeing that a value hasn't
// been computed yet and both starting a computation, so the synchronized
// block ensures that no other thread can be submitting the runnable to the
// executor while we are checking the map. If, on the other hand, it's not
// a problem for two threads to both create the same value (that is, this
// behavior is only intended for caching performance, not for correctness),
// then it should be safe to use a ConcurrentHashMap and use its
// putIfAbsent() method instead.
synchronized(submitted) {
future = submitted.get(action);
if(future == null) {
future = executor.submit(action);
submitted.put(action, future);
}
}
future.get(); // ignore return value because the runnable returns void
}
Note that this assumes that your Action class (I'm assuming you don't mean javax.swing.Action, right?) implements Runnable and also has a reasonable implementation of equals() and hashCode(). Otherwise, you may need to use a different Map implementation (for example, IdentityHashMap).
Also, this assumes that you may have multiple different actions that you want to execute only once. If that's not the case, then you can drop the Map entirely and do something like this:
private final ExecutorService executor = Executors.newCachedThreadPool();
private final Object lock = new Object();
private volatile Runnable action;
private volatile Future<?> future = null;
public void executeOnlyOnce(Runnable action) {
synchronized(lock) {
if(this.action == null) {
this.action = action;
this.future = executor.submit(action);
} else if(!this.action.equals(action)) {
throw new IllegalArgumentException("Unexpected action");
}
}
future.get();
}
public synchronized void foo()
{
...
}
is equivalent to
public void foo()
{
synchronized(this)
{
...
}
}
so either of the two options should work. I personally like the synchronized method option.
Synchronizing the whole method can sometimes be overkill if there is only a certain part of the code that deals with shared data (for example, a common variable that each thread is updating).
Best approach for performance is to only use the synchronized keyword just around the shared data. If you synchronized the whole method when it is not entirely necessarily then a lot of threads will be waiting when they can still do work within their own local scope.
When a thread enters the synchronize it acquires a lock (if you use the this object it locks on the object itself), the other will wait till the lock-acquiring thread has exited. You actually don't need a notify statement in this situation as the threads will release the lock when they exit the synchronize statement.

Categories

Resources