Guest User

Untitled

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