Producer.java 710 Bytes
Newer Older
Luca Arrotta's avatar
Luca Arrotta committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
package producerConsumer;

public class Producer implements Runnable {

    private final String id;
    private final Queue queue;

    public Producer(String id, Queue q) { this.id = id; queue = q; }

    public void run() {
        while (true) {
            String message = produce();
            System.out.println("Prod. " + id + ": inserisco " + message);
            queue.put(message);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private int counter = 0;

    public String produce() {
        counter++;
        return "Messaggio da " + id + " n. " + counter;
    }

}