logo

المزامنة في جافا

المزامنة في Java هي القدرة على التحكم في وصول سلاسل رسائل متعددة إلى أي مورد مشترك.

تعد مزامنة Java خيارًا أفضل حيث نريد السماح لموضوع واحد فقط بالوصول إلى المورد المشترك.

لماذا استخدام المزامنة؟

يتم استخدام المزامنة بشكل أساسي لـ

  1. لمنع تداخل الخيط.
  2. لمنع مشكلة الاتساق.

أنواع المزامنة

هناك نوعان من المزامنة

  1. مزامنة العملية
  2. مزامنة الموضوع

سنناقش هنا مزامنة الخيط فقط.

مزامنة الموضوع

هناك نوعان من الاتصالات الحصرية المتبادلة والمزامنة بين الخيوط.

  1. الحصري المتبادل
    1. طريقة متزامنة.
    2. كتلة متزامنة
    3. مزامنة ثابتة.
  2. التعاون (الاتصال بين الخيوط في جافا)

الحصري المتبادل

يساعد برنامج Mutual Exclusive على منع تداخل سلاسل الرسائل مع بعضها البعض أثناء مشاركة البيانات. ويمكن تحقيق ذلك باستخدام الطرق الثلاث التالية:

  1. باستخدام الطريقة المتزامنة
  2. باستخدام الكتلة المتزامنة
  3. باستخدام المزامنة الثابتة

مفهوم القفل في جافا

تعتمد المزامنة على كيان داخلي يُعرف باسم القفل أو الشاشة. كل كائن له قفل مرتبط به. وفقًا للاتفاقية، يجب على الخيط الذي يحتاج إلى وصول متسق إلى حقول الكائن أن يحصل على قفل الكائن قبل الوصول إليه، ثم تحرير القفل عند الانتهاء منه.

من Java 5، تحتوي الحزمة java.util.concurrent.locks على العديد من تطبيقات القفل.

فهم المشكلة دون المزامنة

في هذا المثال، لا يوجد أي تزامن، لذا فإن الإخراج غير متناسق. دعونا نرى المثال:

TestSynchronization1.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++){>

طريقة جافا المتزامنة

إذا أعلنت أن أي طريقة متزامنة، فإنها تُعرف باسم الطريقة المتزامنة.

يتم استخدام الطريقة المتزامنة لقفل كائن لأي مورد مشترك.

عندما يستدعي خيط طريقة متزامنة، فإنه يحصل تلقائيًا على القفل لهذا الكائن ويحرره عندما يكمل الخيط مهمته.

TestSynchronization2.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++){>

مثال على الطريقة المتزامنة باستخدام فئة مجهولة

في هذا البرنامج، قمنا بإنشاء الخيطين باستخدام الفئة المجهولة، لذلك يتطلب الأمر ترميزًا أقل.

TestSynchronization3.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++){>