X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=TD1%2Fexo3%2FConsommateur.java;h=43c811ab6895e80e7f1cfac5dc092aeadadc04c3;hb=7293fe6dc670d5164859159aab339fa3a4750773;hp=5a548813f2add051d965d812369aba355b257d04;hpb=396bc7433aa7fc3c404f2d175d1c93b82791cdde;p=TD_SR.git diff --git a/TD1/exo3/Consommateur.java b/TD1/exo3/Consommateur.java index 5a54881..43c811a 100644 --- a/TD1/exo3/Consommateur.java +++ b/TD1/exo3/Consommateur.java @@ -1,25 +1,35 @@ - - -public class Consommateur implements Runnable { - - private BufferCirc buffer; - - - public Consommateur(BufferCirc b) { - buffer = b; - } - - - public void run() { - Integer val; - while (true) { - val = (Integer)buffer.preleve(); - System.out.println (Thread.currentThread().getName() + " a preleve " + val); - try { - Thread.sleep((int)(Math.random()*1000)); - } - catch (InterruptedException e) {} - } - } - -} // fin classe Consommateur +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) { + System.out.println("InterruptedException: " + e); + } + } + } + +} // fin classe Consommateur