ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is Synchronization? How it work?

<p>How the synchronization concept work? How it lock and unlock the objects?</p>

user-image
تم إضافة السؤال من قبل Ankit Gomkar , Associate Software Engineer , TechNex Technologies Pvt Ltd
تاريخ النشر: 2014/10/12
Nihanth Christopher
من قبل Nihanth Christopher , Senior Software Engineer , Advanced Data Technologies (Pvt) Ltd, ADS Group

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.

pardeep kumar
من قبل pardeep kumar , Senior R & D Engineer , Mavenir Systems

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.

 

 

Shahin Syed
من قبل Shahin Syed , IT Administrator, Training Officer , Al-Hamad Enterprises

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.

Abbas Ali Patheria
من قبل Abbas Ali Patheria , Logistics Manager , Hindustan Machine Home

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

Abdulelah Fallatah
من قبل Abdulelah Fallatah , Computer Operator , شركة الحاسب الآلي العربي

I'll define it to mean "setting a process or more to work at a particular time or particular times".

المزيد من الأسئلة المماثلة