X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD1%2Fexo3%2FProducteur.java;h=5e09d5062cf9ebb1cad47860e89dfe53aa59ec58;hb=2ef76f09b55d5e1d5ad9535603688b42e91c0783;hp=10eab6d55ec99bc5e25278842a1b1a5e30a6a8a6;hpb=396bc7433aa7fc3c404f2d175d1c93b82791cdde;p=TD_SR.git diff --git a/TD1/exo3/Producteur.java b/TD1/exo3/Producteur.java index 10eab6d..5e09d50 100644 --- a/TD1/exo3/Producteur.java +++ b/TD1/exo3/Producteur.java @@ -1,26 +1,37 @@ - - -public class Producteur implements Runnable { - - private BufferCirc buffer; - private int val; - - - public Producteur(BufferCirc b) { - buffer = b; - } - - - public void run() { - while (true) { - buffer.depose(new Integer(val)); - System.out.println (Thread.currentThread().getName() + " a depose " + val); - val++; - try { - Thread.sleep((int)(Math.random()*100)); - } - catch (InterruptedException e) {} - } - } - -} // fin classe Producteur +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, Thread th, String name) { + buffer = b; + setThName(th, name); + } + + public void setThName(Thread th, String name) { + th.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) { + System.err.println("InterruptedException: " + e); + } + } + } + +} // fin classe Producteur