diff --git a/async/pom.xml b/async/pom.xml new file mode 100644 index 0000000..ce6d56a --- /dev/null +++ b/async/pom.xml @@ -0,0 +1,15 @@ + + + + java-cookbook + fr.gasser + 1.0-SNAPSHOT + + 4.0.0 + + async + + + \ No newline at end of file diff --git a/async/src/main/java/threadexamples/producerconsumer/App.java b/async/src/main/java/threadexamples/producerconsumer/App.java index fcfc904..e74b0f7 100644 --- a/async/src/main/java/threadexamples/producerconsumer/App.java +++ b/async/src/main/java/threadexamples/producerconsumer/App.java @@ -21,7 +21,7 @@ public class App { for (int i = 0; i < 3; i++) { final Consumer consumer = new Consumer("Consumer_" + i, itemQueue); executorService.submit(() -> { - while(true) { + while (true) { consumer.consume(); } }); diff --git a/async/src/main/java/threadexamples/producerconsumer/Consumer.java b/async/src/main/java/threadexamples/producerconsumer/Consumer.java index 49b32a7..c210c3e 100644 --- a/async/src/main/java/threadexamples/producerconsumer/Consumer.java +++ b/async/src/main/java/threadexamples/producerconsumer/Consumer.java @@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory; import java.util.concurrent.BlockingQueue; -public class Consumer { +class Consumer { private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class); @@ -13,12 +13,12 @@ public class Consumer { private String name; private int itemId; - public Consumer(String name, BlockingQueue itemQueue) { + Consumer(String name, BlockingQueue itemQueue) { this.itemQueue = itemQueue; this.name = name; } - public void consume() throws InterruptedException { + void consume() throws InterruptedException { final Item item = itemQueue.take(); LOGGER.info("Consumer [{}] consume item [{}] produced by [{}]", name, item.getId(), item.getProducer()); } diff --git a/async/src/main/java/threadexamples/producerconsumer/Item.java b/async/src/main/java/threadexamples/producerconsumer/Item.java index 9b300b3..72be41e 100644 --- a/async/src/main/java/threadexamples/producerconsumer/Item.java +++ b/async/src/main/java/threadexamples/producerconsumer/Item.java @@ -1,20 +1,20 @@ package threadexamples.producerconsumer; -public class Item { +class Item { private String producer; private int id; - public Item(String producer, int id) { + Item(String producer, int id) { this.producer = producer; this.id = id; } - public String getProducer() { + String getProducer() { return producer; } - public int getId() { + int getId() { return id; } diff --git a/async/src/main/java/threadexamples/producerconsumer/Producer.java b/async/src/main/java/threadexamples/producerconsumer/Producer.java index aad2efd..4fabf7d 100644 --- a/async/src/main/java/threadexamples/producerconsumer/Producer.java +++ b/async/src/main/java/threadexamples/producerconsumer/Producer.java @@ -3,18 +3,18 @@ package threadexamples.producerconsumer; import java.util.Random; import java.util.concurrent.BlockingQueue; -public class Producer { +class Producer { private BlockingQueue itemQueue; private String name; private int itemId; - public Producer(String name, BlockingQueue itemQueue) { + Producer(String name, BlockingQueue itemQueue) { this.itemQueue = itemQueue; this.name = name; } - public void produce() throws InterruptedException { + void produce() throws InterruptedException { itemQueue.put(new Item(name, itemId++)); Thread.sleep(new Random().nextInt(2000)); } diff --git a/pom.xml b/pom.xml index 3f97cb9..0263028 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,7 @@ reflection gui + async