class MutableInteger {
private int value;
public synchronized void increment() {
value++;
}
public synchronized int getValue() {
return value;
}
public void nonSync(){
}
}
I am trying to understand how the synchronization keyword works.
I have a class with methods that are sychronized, this means that on that specific instance of the object, only one thread can call that method at a time? This only pertains to that method though? So if a thread A was calling incriment, thread B would have to wait until thread A was done executing the method? But this is only a method by method basis?
however, if I did
synchronized(this) {
//code
}
that would lock the entire instance of the object?
Does that make sense.. I get in essence what this is supposed to be doing, just trying to fill the gaps
You are right, synchronized methods are locking the instance itself.
So writing the followings:
synchronized myMethod() {
//code
}
Is essentially the same behavior as if you have written:
myMethod() {
synchronized(this) {
//code
}
}
Note, that this is simply an Object and is used as the lock as any other object would be used - the lock can be owned by only one thread at a time, the others must wait for it to enter a synchronized block using the same object. Since methods having the synchronized keyword behave such a way, they share the lock being the instance itself.
So, if you have an increment() and decrement() method both marked synchronized, then only either of the two can be used and by one thread at a time.
Meanwhile, other methods without the synchronized keyword remain completely unaffected and will function the same, whether there are synchronized methods around them or not.
Related
public class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
c++;
}
public synchronized void decrement() {
c--;
}
public synchronized int value() {
return c;
}
}
If there are two threads, each having the same instance of SynchronizedCounter, does this mean that if one thread is calling increment, the other can not call decrement. Is the above code equivalent to a synchronised object? i.e.
public void run(){
synchronised( objectReferenceSynchronisedCounter){
if(conditionToIncrement)
objectReference....Counter.increment();
else
objectReference....Counter.decrement();
}
}
There are 2 questions:
If there are two threads, each having the same instance of SynchronizedCounter, does this mean that if one thread is calling increment, the other can not call decrement.
That is correct. The call to decrement will be blocked while the other thread executes increment. And vice-versa.
Is the above code equivalent to a synchronised object? [code follows]
Your second example is slightly different because you include an if statement in the synchronized block. And generally speaking if a synchronization bloc includes multiple calls, it is not equivalent to synchronizing each individual call.
There is no such thing as a synchronized object in Java. You synchronize methods or code blocks.
However, and maybe that is what you meant, in both your examples the lock is held on the same object, namely the instance of the object whose methods are called. So apart from the slightly different scope, the 2 examples synchronize in the same way on the same object.
The answer is no.
the synchronized scope is the modified method
See http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html
It is exactly a synchronized object. "synchronized" on the method locks "this" once one of your methods is called the others cannot execute until the method id exited.
If I have something like below, so what does that mean inside synchronized block
synchronised (syncObject) {
Basically, it will means only one thread can be inside the above block and as soon as one thread is finished executing, second thread will enter that synchronized block synchronised (syncObject). Right?
Can anyone explain to me in a LayMan language so that I can get better picture?
private static final class Task implements Runnable {
{
private static Object syncObject = new Object();
public Task(Command command, BlockingQueue<Integer> pool1, BlockingQueue<Integer> pool2) {
this.command = command;
this.existPool = pool1;
this.newPool = pool2;
}
public void run()
{
synchronised (syncObject) {
if() {
existId = existPool.take();
attributeMethod(existId);
} else if() {
newId = newPool.take();
attributeMethod(newId);
}
}
}
}
// So I need to make this method synchronized or not? Currently I have made this synchronized
private synchronized void attributeMethod(int range) {
// And suppose If I am calling any other method here-
sampleMethod();
}
// What about this method, I need to make this synchronized as well? or not?
private synchronized void sampleMethod() {
}
Basically, it will means only one thread can be inside the above block and as soon as one thread is finished executing, second thread will enter that synchronized block synchronised (syncObject). Right?
Right!
So I need to make this method synchronized or not?
No you don't. Assuming that the method is only going to be called from within the synchronized block in the run() method, that block will already prevent multiple threads from executing the method simultaneously. So declaring the method to be synchronized is redundant.
However, I should point out some things:
When you declare an instance method as synchronized, it will synchronize on this; i.e. on the Task object. But your synchronized block is synchronizing on a different object ... the object in syncObject. In this case, this doesn't matter. However, if the synchronized block in the run() method wasn't there, you would find that the threads were attempting synchronizing on different objects ... and you would NOT get mutual exclusion.
By synchronizing at the top level of the run() method ... using a single shared syncObject for all threads that execute that task ... you are effectively making the tasks run one at a time. This completely negates any benefits of using threads.
It is good practice to declare the variable containing a private lock object (such as syncObject) to be final. This avoids the possibility that something might overwrite it ... resulting in a synchronization failure.
No, attributeMethod is already running within the scope of a synchronized block; no need to mark it as such, unless you intend to call it concurrently outside this block.
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.
All of you know the synchronization context in Java that they are can be
on the instance.
on the java.lang.Class instance for a certain loaded class.
on a given object
And I need to ask; When i write
Dimension d = new Dimension();
synchronized(d){
// critical atomic operation
}
synchronization of a given object equal to synchronization on the instance practically.
so when I write synchronized(d) where d is an instance of object, then the thread will gain the lock for all synchronized instance block of code.
Can you please give me more details about synchronization context.
Your replies will be appreciated.
The synchronized keyword provides serial access to the block of code (which could be an entire method) it introduces. Serialization of access is done by using an object as a mutex lock.
The three basic uses of synchronized are:
A. On a static method:
static synchronized void someStaticMethod() {
// The Class object - ie "MyClass.class" - is the lock object for this block of code, which is the whole method
}
B. On an instance method:
synchronized void someInstanceMethod() {
// The instance - ie "this" - is lock object for this block of code, which is the whole method
}
C. On an arbitrary block of code:
private Object lock = new Object();
void someMethod() {
synchronized (lock) {
// The specified object is the lock object for this block of code
}
}
In all cases, only one thread at a time may enter the synchronized block.
In all cases, if the same lock object is used for multiple blocks, only one thread may enter any of the synchronised blocks. For example, if there are two - other simultaneous threads calling the methods will block until the first thread exits the method.
Applying the synchronized keyword to a non-static method is shorthand for:
public void method() {
synchronized(this) {
// method
}
}
If you apply synchronized to a static method, then it locks the class object (MyClass.class) whilst the method is called.
I am having this doubt about multi-threading and I have faced lots of questions about in multi-threading in many of the interviews.
I speak a lot of about acquiring a lock on the object as such. My doubt is when you have two methods that are synchronized and there are two threads which wants to access those two methods, ThreadA wants to access MethodA and ThreadB wants to access MethodB.
Now both the methods are in the same object. But I use to say acquiring lock on an object and i have not heard acquiring lock on a method. Now Can both the threads parallely access MethodA and MethodB? My assumption is once you acquire lock on object, no other thread on work on it. Isnt it?
And what is the significance synchronized(XYZ.class)?
No, they cannot. If I understand you correctly then you mean:
class Foo {
public synchronized void methodA () {
doSmth ();
}
public synchronized void methodB () {
doSmthElse ();
}
}
In this case synchronized modifier is equal to:
class Foo {
public void methodA () {
synchronized (this) {
doSmth ();
}
}
public void methodB () {
synchronized (this) {
doSmthElse ();
}
}
}
This means that only 1 thread can work at the same time inside one of these 2 methods on each Foo-object.
And what is the significance synchronized(XYZ.class)?
That's what you have behind the scenes of
class XYZ {
public static synchronized void someMethod () { ... }
}
Declaring a method as synchronized is basically the same as containing the entire method body in a "synchronized(this) { ... }" block. So the lock on synchronized method is on the entire object instance, meaning that you are locking out others who want to use synchronized methods on that same object.
If you want per method synchronization instead of per object synchronization you will have to synchronize on something else, either by using synchronized(guardObject) in that particular method or by using one of the Lock objects in java (most of them added in 1.5).
And what is the significance synchronized(XYZ.class)?
This means that you are using the class as a guard object, it means that the lock in the class itself (not the instance) will be protecting access to the block. The difference to using the object instance as a guard is that if you have ten thousand objects you will only be able to access one of them at a time, compared to protecting access by more than one thread to an instance at a time.
If methodA and methodB use different data structures, and it's safe to call methodA while somebody else is calling methodB, then those two methods should use different locks.
Here's the skinny on synchronized(XYZ.class)
synchronized (XYZ.class) is how you implment the above pattern -- it's a synchronized block that:
acquires a lock on an object (in this class the instance of java.lang.class that represents XYZ)
does some work
releases the lock.
The key is to remember that every object in Java has a lock (the technical term is a monitor) that can be held by only one thread at a time. You can request the lock for any object that you have a reference to.
The synchronized keyword on a method:
public synchronized void foo() {
//do something
}
is just syntactic sugar for this:
public void foo() {
synchronized(this) {
//do something
}
}
There is one significant drawback to this approach: since anyone with a reference to an object can acquire its lock, synchronized methods won't work properly if an external caller acquires and holds the object lock.
(Incidentally, this is why locking on XYZ.class is also a bad idea: it's a globally accesible object, and you never know who might decide to acquire its lock)
To avoid these drawbacks, this pattern is usually used instead of the synchronized method:
private final Object LOCK = new Object();
public void foo() {
synchronized(LOCK) {
//do something
}
}
Since no external caller can have a reference to the LOCK object, only this class can acquire its lock, and the synchronized method will always work as expected.
When you have two different methods that need to be locked separately, the usual way to do that is for each method to lock on a different privately held object:
private final Object LOCKA = new Object();
private final Object LOCKB = new Object();
public void foo() {
synchronized(LOCKA) {
//do something
}
}
public void bar() {
synchronized(LOCKB) {
//do something else
}
}
Then it's possible for a thread to call foo() while another thread is calling bar(), but only one thread will be able to call foo() at a time, and only one thread will be able to call bar() at a time.
Using synchronized(XYZ.class) is very restrictive. This means whatever code is being guarded by this XYZ.class will be executed by one thread only at the same time. You should be aware of this as it could lead to severe starvation problem.
A thread entering into monitor on entrance to critical section defined as 'synchronized'
So you can define each method (not the whole class) as synchronized or even a code block within a method.
http://java.sun.com/docs/books/tutorial/essential/concurrency/locksync.html