-\r
-\r
-/**\r
- * implementation du producteur consommateur avec un buffer circulaire\r
- */\r
-public class BufferCirc {\r
-\r
- private Object[] tampon;\r
- private int taille;\r
- private int prem, der, nbObj;\r
-\r
-\r
- public BufferCirc (int t) {\r
- taille = t;\r
- tampon = new Object[taille];\r
- prem = 0;\r
- der = 0;\r
- nbObj = 0;\r
- }\r
-\r
-\r
- public boolean isEmpty() {\r
- return nbObj == 0;\r
- }\r
-\r
-\r
- public boolean isFull() {\r
- return nbObj == taille;\r
- }\r
-\r
-\r
- public synchronized void depose(Object obj) {\r
- while(isFull()) {\r
- try {\r
- System.out.println("Buffer is full: " + Thread.currentThread().getName()\r
- + " is waiting, size: " + nbObj);\r
-\r
- wait();\r
- }\r
- catch (InterruptedException e) {}\r
- }\r
- nbObj++;\r
- tampon[prem] = obj;\r
- prem = (prem + 1) % taille;\r
- //System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj);\r
- notifyAll();\r
- }\r
-\r
-\r
- public synchronized Object preleve() {\r
- while(isEmpty()) {\r
- try {\r
- System.out.println("Buffer is empty: " + Thread.currentThread().getName()\r
- + " is waiting, size: " + nbObj);\r
- wait();\r
- }\r
- catch (InterruptedException e) {}\r
- }\r
- Object outObj = null;\r
- nbObj--;\r
- outObj = tampon[der];\r
- der = (der + 1) % taille;\r
- //System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj);\r
- notifyAll();\r
- return outObj;\r
- }\r
-\r
-} // fin class BufferCirc\r
+
+
+/**
+ * implementation du producteur consommateur avec un buffer circulaire
+ */
+public class BufferCirc {
+
+ private Object[] tampon;
+ private int taille;
+ private int prem, der, nbObj;
+
+
+ public BufferCirc (int t) {
+ taille = t;
+ tampon = new Object[taille];
+ prem = 0;
+ der = 0;
+ nbObj = 0;
+ }
+
+
+ public boolean isEmpty() {
+ return nbObj == 0;
+ }
+
+
+ public boolean isFull() {
+ return nbObj == taille;
+ }
+
+
+ public synchronized void depose(Object obj) {
+ while(isFull()) {
+ try {
+ System.out.println("Buffer is full: " + Thread.currentThread().getName()
+ + " is waiting, size: " + nbObj);
+
+ wait();
+ }
+ catch (InterruptedException e) {}
+ }
+ nbObj++;
+ tampon[prem] = obj;
+ prem = (prem + 1) % taille;
+ //System.out.println(Thread.currentThread().getName() + " a depose " + (Integer)obj);
+ notifyAll();
+ }
+
+
+ public synchronized Object preleve() {
+ while(isEmpty()) {
+ try {
+ System.out.println("Buffer is empty: " + Thread.currentThread().getName()
+ + " is waiting, size: " + nbObj);
+ wait();
+ }
+ catch (InterruptedException e) {}
+ }
+ Object outObj = null;
+ nbObj--;
+ outObj = tampon[der];
+ der = (der + 1) % taille;
+ //System.out.println(Thread.currentThread().getName() + " a preleve " + (Integer)outObj);
+ notifyAll();
+ return outObj;
+ }
+
+} // fin class BufferCirc
-import java.util.concurrent.ThreadLocalRandom;\r
-\r
-\r
-public class Consommateur implements Runnable {\r
-\r
- private BufferCirc buffer;\r
-\r
- public Consommateur(BufferCirc b) {\r
- buffer = b;\r
- }\r
-\r
- public Consommateur(BufferCirc b, String name) {\r
- buffer = b;\r
- setThName(name);\r
- }\r
-\r
- public void setThName(String name) {\r
- Thread.currentThread().setName(name);\r
- }\r
-\r
- public void run() {\r
- Integer val;\r
- while (true) {\r
- val = (Integer)buffer.preleve();\r
- System.out.println (Thread.currentThread().getName() + " a preleve " + val);\r
- try {\r
- Thread.sleep(ThreadLocalRandom.current().nextInt(1001));\r
- }\r
- catch (InterruptedException e) {}\r
- }\r
- }\r
-\r
-} // fin classe Consommateur\r
+import java.util.concurrent.ThreadLocalRandom;
+
+
+public class Consommateur implements Runnable {
+
+ private BufferCirc buffer;
+
+ public Consommateur(BufferCirc b) {
+ buffer = b;
+ }
+
+ public Consommateur(BufferCirc b, String name) {
+ buffer = b;
+ setThName(name);
+ }
+
+ public void setThName(String name) {
+ Thread.currentThread().setName(name);
+ }
+
+ public void run() {
+ Integer val;
+ while (true) {
+ val = (Integer)buffer.preleve();
+ System.out.println (Thread.currentThread().getName() + " a preleve " + val);
+ try {
+ Thread.sleep(ThreadLocalRandom.current().nextInt(1001));
+ }
+ catch (InterruptedException e) {}
+ }
+ }
+
+} // fin classe Consommateur
-\r
-import java.util.ArrayList;\r
-\r
-\r
-public class Main {\r
-\r
-\r
- public static void main (String[] args) {\r
- final int BUFFER_SIZE = 1;\r
- final int PROD_NUMBER = 20;\r
- final int CONS_NUMBER = 20;\r
- BufferCirc b = new BufferCirc(BUFFER_SIZE);\r
- Thread[] P = new Thread[PROD_NUMBER];\r
- Thread[] C = new Thread[CONS_NUMBER];\r
- for (int i = 0; i < P.length; i++) {\r
- P[i] = new Thread(new Producteur(b));\r
- P[i].setName("P" + i);\r
- P[i].start();\r
- }\r
- for (int i = 0; i < C.length; i++) {\r
- C[i] = new Thread(new Consommateur(b));\r
- C[i].setName("C" + i);\r
- C[i].start();\r
- }\r
- }\r
-\r
-}\r
+
+import java.util.ArrayList;
+
+
+public class Main {
+
+
+ public static void main (String[] args) {
+ final int BUFFER_SIZE = 1;
+ final int PROD_NUMBER = 20;
+ final int CONS_NUMBER = 20;
+ BufferCirc b = new BufferCirc(BUFFER_SIZE);
+ Thread[] P = new Thread[PROD_NUMBER];
+ Thread[] C = new Thread[CONS_NUMBER];
+ for (int i = 0; i < P.length; i++) {
+ P[i] = new Thread(new Producteur(b));
+ P[i].setName("P" + i);
+ P[i].start();
+ }
+ for (int i = 0; i < C.length; i++) {
+ C[i] = new Thread(new Consommateur(b));
+ C[i].setName("C" + i);
+ C[i].start();
+ }
+ }
+
+}
-import java.util.concurrent.ThreadLocalRandom;\r
-\r
-\r
-public class Producteur implements Runnable {\r
-\r
- private BufferCirc buffer;\r
- private int val;\r
-\r
-\r
- public Producteur(BufferCirc b) {\r
- buffer = b;\r
- }\r
-\r
- public Producteur(BufferCirc b, String name) {\r
- buffer = b;\r
- setThName(name);\r
- }\r
-\r
- public void setThName(String name) {\r
- Thread.currentThread().setName(name);\r
- }\r
-\r
- public void run() {\r
- while (true) {\r
- buffer.depose(new Integer(val));\r
- System.out.println (Thread.currentThread().getName() + " a depose " + val);\r
- val++;\r
- try {\r
- Thread.sleep(ThreadLocalRandom.current().nextInt(101));\r
- }\r
- catch (InterruptedException e) {}\r
- }\r
- }\r
-\r
-} // fin classe Producteur\r
+import java.util.concurrent.ThreadLocalRandom;
+
+
+public class Producteur implements Runnable {
+
+ private BufferCirc buffer;
+ private int val;
+
+
+ public Producteur(BufferCirc b) {
+ buffer = b;
+ }
+
+ public Producteur(BufferCirc b, String name) {
+ buffer = b;
+ setThName(name);
+ }
+
+ public void setThName(String name) {
+ Thread.currentThread().setName(name);
+ }
+
+ public void run() {
+ while (true) {
+ buffer.depose(new Integer(val));
+ System.out.println (Thread.currentThread().getName() + " a depose " + val);
+ val++;
+ try {
+ Thread.sleep(ThreadLocalRandom.current().nextInt(101));
+ }
+ catch (InterruptedException e) {}
+ }
+ }
+
+} // fin classe Producteur
-import java.io.*;\r
-\r
-\r
-public class Main {\r
-\r
-\r
- public static void main (String[] args) {\r
-\r
- ClientSimplifie client = new ClientSimplifie();\r
-\r
- client.sendMsg("Line 1 Line 2");\r
- String msg = client.receiveMsg();\r
- System.out.println(msg);\r
-\r
- client.closeRWIO();\r
-\r
- }\r
-\r
-}\r
+import java.io.*;
+
+
+public class Main {
+
+
+ public static void main (String[] args) {
+
+ ClientSimplifie client = new ClientSimplifie();
+
+ client.sendMsg("Line 1 Line 2");
+ String msg = client.receiveMsg();
+ System.out.println(msg);
+
+ client.closeRWIO();
+
+ }
+
+}