S
Stanimir Stamenkov
I've read statements that using a separate object to lock on, rather
than using a synchronized method, to synchronize an operation on
other object is better, i.e.:
public class Pencho {
private int mField;
private Object mLock = new Object();
public int makeSomething(int pNumber) {
syncrhonized (mLock) {
mFiled = (mField + pMagicNumber) / 2;
return mField;
}
}
}
and:
public class Pencho {
private int mField;
public synchronized int makeSomething(int pNumber) {
mFiled = (mField + pMagicNumber) / 2;
return mField;
}
}
Is there benefit of using the first approach over the second one
(and specifically for the above simple example)?
than using a synchronized method, to synchronize an operation on
other object is better, i.e.:
public class Pencho {
private int mField;
private Object mLock = new Object();
public int makeSomething(int pNumber) {
syncrhonized (mLock) {
mFiled = (mField + pMagicNumber) / 2;
return mField;
}
}
}
and:
public class Pencho {
private int mField;
public synchronized int makeSomething(int pNumber) {
mFiled = (mField + pMagicNumber) / 2;
return mField;
}
}
Is there benefit of using the first approach over the second one
(and specifically for the above simple example)?