logo

Synchronisatie in Java

Synchronisatie in Java is de mogelijkheid om de toegang van meerdere threads tot een gedeelde bron te controleren.

Java-synchronisatie is een betere optie als we slechts één thread toegang willen geven tot de gedeelde bron.

Waarom synchronisatie gebruiken?

De synchronisatie wordt vooral gebruikt

  1. Om schroefdraadinterferentie te voorkomen.
  2. Om consistentieproblemen te voorkomen.

Soorten synchronisatie

Er zijn twee soorten synchronisatie

  1. Processynchronisatie
  2. Synchronisatie van threads

Hier bespreken we alleen threadsynchronisatie.

Synchronisatie van threads

Er zijn twee soorten threadsynchronisatie die elkaar wederzijds uitsluiten en communicatie tussen threads.

  1. Wederzijds exclusief
    1. Gesynchroniseerde methode.
    2. Gesynchroniseerd blok.
    3. Statische synchronisatie.
  2. Samenwerking (communicatie tussen threads in Java)

Wederzijds exclusief

Mutual Exclusive zorgt ervoor dat threads elkaar niet hinderen tijdens het delen van gegevens. Dit kan worden bereikt door de volgende drie manieren te gebruiken:

  1. Door gebruik te maken van de gesynchroniseerde methode
  2. Door gesynchroniseerd blok te gebruiken
  3. Door gebruik te maken van statische synchronisatie

Concept van slot op Java

Synchronisatie is opgebouwd rond een interne entiteit die bekend staat als het slot of de monitor. Aan elk object is een slot gekoppeld. Volgens afspraak moet een thread die consistente toegang tot de velden van een object nodig heeft, de vergrendeling van het object verwerven voordat hij er toegang toe krijgt, en vervolgens de vergrendeling vrijgeven als hij er klaar mee is.

Vanaf Java 5 bevat het pakket java.util.concurrent.locks verschillende lock-implementaties.

Het probleem begrijpen zonder synchronisatie

In dit voorbeeld is er geen synchronisatie, dus de uitvoer is inconsistent. Laten we het voorbeeld bekijken:

TestSynchronisatie1.java

 class Table{ void printTable(int n){//method not synchronized for(int i=1;i<=5;i++){ system.out.println(n*i); try{ thread.sleep(400); }catch(exception e){system.out.println(e);} } class mythread1 extends thread{ table t; mythread1(table t){ this.t="t;" public void run(){ t.printtable(5); mythread2 mythread2(table t.printtable(100); testsynchronization1{ static main(string args[]){ obj="new" table(); only one object t1="new" mythread1(obj); t2="new" mythread2(obj); t1.start(); t2.start(); < pre> <p> <strong>Output:</strong> </p> <pre> 5 100 10 200 15 300 20 400 25 500 </pre> <h3>Java Synchronized Method</h3> <p>If you declare any method as synchronized, it is known as synchronized method.</p> <p>Synchronized method is used to lock an object for any shared resource.</p> <p>When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.</p> <p> <strong>TestSynchronization2.java</strong> </p> <pre> //example of java synchronized method class Table{ synchronized void printTable(int n){//synchronized method for(int i=1;i<=5;i++){ system.out.println(n*i); try{ thread.sleep(400); }catch(exception e){system.out.println(e);} } class mythread1 extends thread{ table t; mythread1(table t){ this.t="t;" public void run(){ t.printtable(5); mythread2 mythread2(table t.printtable(100); testsynchronization2{ static main(string args[]){ obj="new" table(); only one object t1="new" mythread1(obj); t2="new" mythread2(obj); t1.start(); t2.start(); < pre> <p> <strong>Output:</strong> </p> <pre> 5 10 15 20 25 100 200 300 400 500 </pre> <h3>Example of synchronized method by using annonymous class</h3> <p>In this program, we have created the two threads by using the anonymous class, so less coding is required.</p> <p> <strong>TestSynchronization3.java</strong> </p> <pre> //Program of synchronized method by using annonymous class class Table{ synchronized void printTable(int n){//synchronized method for(int i=1;i<=5;i++){ system.out.println(n*i); try{ thread.sleep(400); }catch(exception e){system.out.println(e);} } public class testsynchronization3{ static void main(string args[]){ final table obj="new" table(); only one object thread t1="new" thread(){ run(){ obj.printtable(5); }; t2="new" obj.printtable(100); t1.start(); t2.start(); < pre> <p> <strong>Output:</strong> </p> <pre> 5 10 15 20 25 100 200 300 400 500 </pre> <hr></=5;i++){></pre></=5;i++){></pre></=5;i++){>

Java-gesynchroniseerde methode

Als u een methode als gesynchroniseerd declareert, wordt deze gesynchroniseerde methode genoemd.

De gesynchroniseerde methode wordt gebruikt om een ​​object voor een gedeelde bron te vergrendelen.

Wanneer een thread een gesynchroniseerde methode aanroept, verkrijgt deze automatisch de vergrendeling voor dat object en geeft deze vrij wanneer de thread zijn taak heeft voltooid.

TestSynchronisatie2.java

 //example of java synchronized method class Table{ synchronized void printTable(int n){//synchronized method for(int i=1;i<=5;i++){ system.out.println(n*i); try{ thread.sleep(400); }catch(exception e){system.out.println(e);} } class mythread1 extends thread{ table t; mythread1(table t){ this.t="t;" public void run(){ t.printtable(5); mythread2 mythread2(table t.printtable(100); testsynchronization2{ static main(string args[]){ obj="new" table(); only one object t1="new" mythread1(obj); t2="new" mythread2(obj); t1.start(); t2.start(); < pre> <p> <strong>Output:</strong> </p> <pre> 5 10 15 20 25 100 200 300 400 500 </pre> <h3>Example of synchronized method by using annonymous class</h3> <p>In this program, we have created the two threads by using the anonymous class, so less coding is required.</p> <p> <strong>TestSynchronization3.java</strong> </p> <pre> //Program of synchronized method by using annonymous class class Table{ synchronized void printTable(int n){//synchronized method for(int i=1;i<=5;i++){ system.out.println(n*i); try{ thread.sleep(400); }catch(exception e){system.out.println(e);} } public class testsynchronization3{ static void main(string args[]){ final table obj="new" table(); only one object thread t1="new" thread(){ run(){ obj.printtable(5); }; t2="new" obj.printtable(100); t1.start(); t2.start(); < pre> <p> <strong>Output:</strong> </p> <pre> 5 10 15 20 25 100 200 300 400 500 </pre> <hr></=5;i++){></pre></=5;i++){>

Voorbeeld van een gesynchroniseerde methode met behulp van een anonieme klasse

In dit programma hebben we de twee threads gemaakt met behulp van de anonieme klasse, dus er is minder codering vereist.

TestSynchronisatie3.java

 //Program of synchronized method by using annonymous class class Table{ synchronized void printTable(int n){//synchronized method for(int i=1;i<=5;i++){ system.out.println(n*i); try{ thread.sleep(400); }catch(exception e){system.out.println(e);} } public class testsynchronization3{ static void main(string args[]){ final table obj="new" table(); only one object thread t1="new" thread(){ run(){ obj.printtable(5); }; t2="new" obj.printtable(100); t1.start(); t2.start(); < pre> <p> <strong>Output:</strong> </p> <pre> 5 10 15 20 25 100 200 300 400 500 </pre> <hr></=5;i++){>