Register now or log in to join your professional community.
<p>How the synchronization concept work? How it lock and unlock the objects?</p>
Synchronization means one thread at a time,
Synchronization in java is done through acquiring the lock on some specific Object. Therefore, if you do this:
class TestClass {
SomeClass someVariable;
public void myMethod () {
synchronized (someVariable) {
...
}
}
public void myOtherMethod() {
synchronized (someVariable) {
...
}
}
}
Then those two blocks will be protected by execution of2 different threads at any time while someVariable is not modified. Basically, it's said that those two blocks are synchronized against the variable someVariable.
When you put synchronized on the method, it basically means the same as synchronized (this), that is, a synchronization on the object this method is executed on.
That is:
public synchronized void myMethod() {
...
}
Means the same as:
public void myMethod() {
synchronized (this) {
...
}
}
Therefore, to answer your question - yes, threads won't be able to simultaneously call those methods in different threads, as they are both holding a reference to the same monitor, the monitor of this object.
In Simple term, synchronization means controlling the access to a particular resources such that it will be used by only one at a time.
How it works? Let's say, there is only one bathroom. In order to use this, one person go inside and lock it until he finished and others will wait.
Similarly, consider thread as a person in java that can access the code of block one at a time and other will wail until that thread complete its processing.
How it lock and unlock? There are many techniques to implement this concept in java. Just to give a simple example. Take a variable x and set its value1.
When some thread try to enter a block and see x=1, it will enter that block and set its value0 until it is executing its task. If other thread try to enter and see x=0, they will wait until previous thread leave the block and set x value to1.
Synchronization is process of coordinating events in a timely fashion. The Concept is mainly implemented in Programming for all kinds of applications which share different resources.
synchronization is used when more then one thread is trying to gain access before the completion of the previous thread called through the same reference/object.
synchronization can be used in two different ways:-
1.Using synchronized keyword before the method being called, that is:-
synchronized void get(){
//method body
}
2.Writing the calling message in synchronized block with passing reference/object as an argument in synchronized keyword, that is:-
synchronized(reference/object){
//calling method;
}
can you name in which area you want the definition of synchronization?
Because there are many in the computer field like :
synchronization tasks.
timing of acquisitions,
file synchronization.
Sync audio video
I'll define it to mean "setting a process or more to work at a particular time or particular times".