-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hero - Multithreading - Consumer Producer problem
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.hero.multithreading.producerconsumer; | ||
|
||
public class Message { | ||
|
||
private String message; | ||
|
||
public Message(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/com/hero/multithreading/producerconsumer/MessageConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.hero.multithreading.producerconsumer; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
|
||
public class MessageConsumer implements Runnable { | ||
|
||
private BlockingQueue<Message> queue; | ||
|
||
public MessageConsumer(BlockingQueue<Message> queue) { | ||
this.queue = queue; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
Message message; | ||
while (!"exit".equals((message = queue.take()).getMessage())) { | ||
Thread.sleep(100); | ||
System.out.println("Message is consumed: " + message.getMessage()); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
|
||
|
||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/com/hero/multithreading/producerconsumer/MessageProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.hero.multithreading.producerconsumer; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
|
||
public class MessageProducer implements Runnable { | ||
|
||
private BlockingQueue<Message> queue; | ||
|
||
public MessageProducer(BlockingQueue<Message> queue) { | ||
this.queue = queue; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
for (int i = 0; i < 100; i++) { | ||
Message message = new Message("" + i); | ||
try { | ||
queue.put(message); | ||
System.out.println("Message added to queue: " + message.getMessage()); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
try { | ||
queue.put(new Message("exit")); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/com/hero/multithreading/producerconsumer/ProducerConsumerApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.hero.multithreading.producerconsumer; | ||
|
||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.BlockingQueue; | ||
|
||
public class ProducerConsumerApp { | ||
|
||
public static void main(String[] args) { | ||
BlockingQueue<Message> queue = new ArrayBlockingQueue<>(5); | ||
MessageProducer producer = new MessageProducer(queue); | ||
MessageConsumer consumer = new MessageConsumer(queue); | ||
|
||
new Thread(producer).start(); | ||
new Thread(consumer).start(); | ||
|
||
} | ||
} |