Guest User

Untitled

a guest
Jan 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. class ZonePartage extends Thread {
  2.     int nLecteurs=0;
  3.     int nEcrivainsEnAttente=0;
  4.     Object accesslock;
  5.     boolean exclusion=false;
  6.  
  7.     ZonePartage() {
  8.         accesslock = new Object();
  9.     }
  10.  
  11.     public void accesPartage() {
  12.         synchronized(accesslock) {
  13.             while(nEcrivainsEnAttente>0 && exclusion) attente();
  14.             nLecteurs++;
  15.         }
  16.     }
  17.    
  18.     public void retourPartage() {
  19.         synchronized(accesslock) {
  20.             nLecteurs--;
  21.             if(nLecteurs==0) accesslock.notifyAll();
  22.         }
  23.     }
  24.    
  25.     public void accesExclusif() {
  26.         synchronized(accesslock) {
  27.             exclusion=true;
  28.             while(nLecteurs!=0 && nEcrivainsEnAttente>0) attente();
  29.             nEcrivainsEnAttente++;
  30.         }
  31.     }
  32.    
  33.     public void retourExclusif() {
  34.         synchronized(accesslock) {
  35.             nEcrivainsEnAttente--;
  36.             exclusion=false;
  37.             accesslock.notifyAll();
  38.         }
  39.     }
  40.    
  41.     public void attente() {
  42.         try {
  43.             accesslock.wait(); // bloque jusqu'a un notify()  
  44.         } catch(InterruptedException e) {};
  45.     }
  46. }    
  47.  
  48. class Reader extends Thread {
  49.     ZonePartage z;
  50.     String name;
  51.     Reader (ZonePartage z, String name) {
  52.         super(name);
  53.         this.z = z;
  54.         this.name=name;
  55.     }
  56.     public void run() {
  57.         while (true) {
  58.             lecture();
  59.         }
  60.     }
  61.     public void lecture() {
  62.         z.accesPartage();
  63.         System.out.println("" +name +" Lecture (lecteur=" +z.nLecteurs +" / ecrivain=" +z.nEcrivainsEnAttente +")");
  64.         z.retourPartage();
  65.     }
  66.    
  67. }
  68.  
  69. class Writer extends Thread {
  70.     ZonePartage z;
  71.     String name;
  72.     Writer (ZonePartage z, String name) {
  73.         super(name);
  74.         this.z = z;
  75.         this.name=name;
  76.     }
  77.     public void run() {
  78.         while (true) {
  79.             ecriture();
  80.         }
  81.     }
  82.     public void ecriture() {
  83.         z.accesExclusif();
  84.         System.out.println("" +name +" Ecriture (lecteur=" +z.nLecteurs +" / ecrivain=" +z.nEcrivainsEnAttente +")");
  85.         z.retourExclusif();
  86.     }
  87. }
  88.  
  89. public class LectEcrivain {
  90.     public static void main(String args[]) {
  91.     ZonePartage z = new ZonePartage();
  92.         Thread r1 = new Reader(z,"r1");
  93.         Thread r2 = new Reader(z,"r2");
  94.         Thread r3 = new Reader(z,"r3");
  95.         Thread r4 = new Reader(z,"r4");
  96.         Thread w1 = new Writer(z,"w1");
  97.         Thread w2 = new Writer(z,"w2");
  98.         r1.start();
  99.         r2.start();
  100.         r3.start();
  101.         r4.start();
  102.         w1.start();
  103.         w2.start();
  104.     }
  105. }
Add Comment
Please, Sign In to add comment