Producer.java 704 Bytes
Newer Older
Luca Arrotta's avatar
Luca Arrotta committed
1 2 3 4 5 6 7 8 9 10 11 12
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();
Michele Fiori's avatar
Michele Fiori committed
13
            System.out.println("Prod. " + id + ": " + message);
Luca Arrotta's avatar
Luca Arrotta committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
            queue.put(message);

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

    private int counter = 0;

    public String produce() {
        counter++;
Michele Fiori's avatar
Michele Fiori committed
28
        return "Message number " + counter + " from " + id;
Luca Arrotta's avatar
Luca Arrotta committed
29 30 31
    }

}