From 49bcf8c967d9b9e6217bca3c37e4edc131353ce5 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 19 Jan 2017 21:35:23 +0100 Subject: [PATCH 1/5] BAEL-382 BlocknigQueue producer consumer program --- .../concurrency/BlockingQueueUsage.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java diff --git a/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java b/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java new file mode 100644 index 0000000000..e5637a724b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java @@ -0,0 +1,81 @@ +package com.baeldung.concurrency; + + +import java.util.Random; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class BlockingQueueUsage { + public static void main(String[] args) { + int BOUND = 10; + int N_PRODUCERS = 4; + int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); + + + BlockingQueue queue = new LinkedBlockingQueue<>(BOUND); + + for (int i = 0; i < N_PRODUCERS; i++) { + System.out.println("new Producer"); + new Thread(new NumbersProducer(queue)).start(); + } + + for (int j = 0; j < N_CONSUMERS; j++) { + System.out.println("new Consumer"); + new Thread(new NumbersConsumer(queue)).start(); + } + } + + + //producer + static class NumbersProducer implements Runnable { + private final Random random = new Random(); + private final BlockingQueue numbersQueue; + + + public NumbersProducer(BlockingQueue numbersQueue) { + this.numbersQueue = numbersQueue; + } + + + public void run() { + try { + generateNumbers(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + private void generateNumbers() throws InterruptedException { + for (int i = 0; i < 100; i++) { + numbersQueue.put(random.nextInt(100)); + } + } + } + + //consumer + static class NumbersConsumer implements Runnable { + private final BlockingQueue queue; + + public NumbersConsumer(BlockingQueue queue) { + this.queue = queue; + } + + public void run() { + try { + while (true) { + Integer number = queue.take(); + int result = fibonacci(number); + System.out.println("fibonacci result: " + result); + + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + public int fibonacci(Integer number) { + if (number <= 1) return number; + else return fibonacci(number - 1) + fibonacci(number - 2); + } + } +} \ No newline at end of file From 5013f7be99fad050c09ff8488cf910d6bb3bee2d Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Wed, 25 Jan 2017 19:07:48 +0100 Subject: [PATCH 2/5] BAEL-614 without a fibonacci --- .../java/com/baeldung/concurrency/BlockingQueueUsage.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java b/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java index e5637a724b..a8ffc2be72 100644 --- a/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java +++ b/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java @@ -64,8 +64,8 @@ public class BlockingQueueUsage { try { while (true) { Integer number = queue.take(); - int result = fibonacci(number); - System.out.println("fibonacci result: " + result); + String result = number.toString(); + System.out.println(Thread.currentThread().getName() + " result: " + result); } } catch (InterruptedException e) { @@ -73,9 +73,5 @@ public class BlockingQueueUsage { } } - public int fibonacci(Integer number) { - if (number <= 1) return number; - else return fibonacci(number - 1) + fibonacci(number - 2); - } } } \ No newline at end of file From ea7e3db1549cb42da8db6c125e3c7c13a5588e38 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Wed, 25 Jan 2017 19:10:49 +0100 Subject: [PATCH 3/5] BAEL-614 remove println --- .../main/java/com/baeldung/concurrency/BlockingQueueUsage.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java b/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java index a8ffc2be72..0a38715245 100644 --- a/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java +++ b/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java @@ -15,12 +15,10 @@ public class BlockingQueueUsage { BlockingQueue queue = new LinkedBlockingQueue<>(BOUND); for (int i = 0; i < N_PRODUCERS; i++) { - System.out.println("new Producer"); new Thread(new NumbersProducer(queue)).start(); } for (int j = 0; j < N_CONSUMERS; j++) { - System.out.println("new Consumer"); new Thread(new NumbersConsumer(queue)).start(); } } From c9a1fb1b398ad0517d9c3d39a2e3a456f265681a Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 26 Jan 2017 18:41:29 +0100 Subject: [PATCH 4/5] BAEL-614 move to proper package, move inner classes to separate classes --- .../concurrency/BlockingQueueUsage.java | 75 ------------------- .../blockingqueue/BlockingQueueUsage.java | 24 ++++++ .../blockingqueue/NumbersConsumer.java | 25 +++++++ .../blockingqueue/NumbersProducer.java | 31 ++++++++ 4 files changed, 80 insertions(+), 75 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java diff --git a/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java b/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java deleted file mode 100644 index 0a38715245..0000000000 --- a/core-java/src/main/java/com/baeldung/concurrency/BlockingQueueUsage.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.baeldung.concurrency; - - -import java.util.Random; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; - -public class BlockingQueueUsage { - public static void main(String[] args) { - int BOUND = 10; - int N_PRODUCERS = 4; - int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); - - - BlockingQueue queue = new LinkedBlockingQueue<>(BOUND); - - for (int i = 0; i < N_PRODUCERS; i++) { - new Thread(new NumbersProducer(queue)).start(); - } - - for (int j = 0; j < N_CONSUMERS; j++) { - new Thread(new NumbersConsumer(queue)).start(); - } - } - - - //producer - static class NumbersProducer implements Runnable { - private final Random random = new Random(); - private final BlockingQueue numbersQueue; - - - public NumbersProducer(BlockingQueue numbersQueue) { - this.numbersQueue = numbersQueue; - } - - - public void run() { - try { - generateNumbers(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - private void generateNumbers() throws InterruptedException { - for (int i = 0; i < 100; i++) { - numbersQueue.put(random.nextInt(100)); - } - } - } - - //consumer - static class NumbersConsumer implements Runnable { - private final BlockingQueue queue; - - public NumbersConsumer(BlockingQueue queue) { - this.queue = queue; - } - - public void run() { - try { - while (true) { - Integer number = queue.take(); - String result = number.toString(); - System.out.println(Thread.currentThread().getName() + " result: " + result); - - } - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - } -} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java new file mode 100644 index 0000000000..63c6cc4460 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java @@ -0,0 +1,24 @@ +package com.baeldung.concurrent.blockingqueue; + + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class BlockingQueueUsage { + public static void main(String[] args) { + int BOUND = 10; + int N_PRODUCERS = 4; + int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); + + + BlockingQueue queue = new LinkedBlockingQueue<>(BOUND); + + for (int i = 0; i < N_PRODUCERS; i++) { + new Thread(new NumbersProducer(queue)).start(); + } + + for (int j = 0; j < N_CONSUMERS; j++) { + new Thread(new NumbersConsumer(queue)).start(); + } + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java new file mode 100644 index 0000000000..110a801acc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.blockingqueue; + + +import java.util.concurrent.BlockingQueue; + +class NumbersConsumer implements Runnable { + private final BlockingQueue queue; + + public NumbersConsumer(BlockingQueue queue) { + this.queue = queue; + } + + public void run() { + try { + while (true) { + Integer number = queue.take(); + String result = number.toString(); + System.out.println(Thread.currentThread().getName() + " result: " + result); + + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java new file mode 100644 index 0000000000..c9e91cd077 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java @@ -0,0 +1,31 @@ +package com.baeldung.concurrent.blockingqueue; + + +import java.util.Random; +import java.util.concurrent.BlockingQueue; + + +class NumbersProducer implements Runnable { + private final Random random = new Random(); + private final BlockingQueue numbersQueue; + + + public NumbersProducer(BlockingQueue numbersQueue) { + this.numbersQueue = numbersQueue; + } + + + public void run() { + try { + generateNumbers(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + private void generateNumbers() throws InterruptedException { + for (int i = 0; i < 100; i++) { + numbersQueue.put(random.nextInt(100)); + } + } +} \ No newline at end of file From 4ff894095c4a7fe14df390a8a75b1ad81c58129b Mon Sep 17 00:00:00 2001 From: Pedja Date: Fri, 27 Jan 2017 11:52:17 +0100 Subject: [PATCH 5/5] BAEL-614 Added "public" to class definitions; Formatted code --- .../concurrent/blockingqueue/BlockingQueueUsage.java | 2 -- .../baeldung/concurrent/blockingqueue/NumbersConsumer.java | 4 +--- .../baeldung/concurrent/blockingqueue/NumbersProducer.java | 6 +----- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java index 63c6cc4460..801dc4e35c 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java @@ -1,6 +1,5 @@ package com.baeldung.concurrent.blockingqueue; - import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -10,7 +9,6 @@ public class BlockingQueueUsage { int N_PRODUCERS = 4; int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); - BlockingQueue queue = new LinkedBlockingQueue<>(BOUND); for (int i = 0; i < N_PRODUCERS; i++) { diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java index 110a801acc..774c263a3b 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java @@ -1,9 +1,8 @@ package com.baeldung.concurrent.blockingqueue; - import java.util.concurrent.BlockingQueue; -class NumbersConsumer implements Runnable { +public class NumbersConsumer implements Runnable { private final BlockingQueue queue; public NumbersConsumer(BlockingQueue queue) { @@ -16,7 +15,6 @@ class NumbersConsumer implements Runnable { Integer number = queue.take(); String result = number.toString(); System.out.println(Thread.currentThread().getName() + " result: " + result); - } } catch (InterruptedException e) { Thread.currentThread().interrupt(); diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java index c9e91cd077..dcc0b48453 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java @@ -1,20 +1,16 @@ package com.baeldung.concurrent.blockingqueue; - import java.util.Random; import java.util.concurrent.BlockingQueue; - -class NumbersProducer implements Runnable { +public class NumbersProducer implements Runnable { private final Random random = new Random(); private final BlockingQueue numbersQueue; - public NumbersProducer(BlockingQueue numbersQueue) { this.numbersQueue = numbersQueue; } - public void run() { try { generateNumbers();