Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package domain;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- /**
- *
- * @author crownedzero
- */
- public class Producer implements Runnable {
- protected final static List<Message> sharedQueue = new ArrayList<>();
- protected final int MAX_SIZE = 10;
- @Override
- public void run() {
- while (true) {
- synchronized (sharedQueue) {
- while (sharedQueue.size() == MAX_SIZE) {
- try {
- sharedQueue.wait();
- } catch (InterruptedException e) {
- }
- }
- sharedQueue.add(new Message(Utility.getRandomProduct(), new Date(), Utility.regionLookup(Utility.getState())));
- System.out.println(Thread.currentThread().getName() + " adding. Queue size: " + sharedQueue.size());
- }
- }
- }
- public static Message messageConsume(String region) {
- synchronized(sharedQueue) {
- while (sharedQueue.isEmpty()) {
- try {
- sharedQueue.wait();
- } catch (InterruptedException e) {
- }
- }
- if (sharedQueue.get(0).getRegion().equalsIgnoreCase(region)) {
- Message tempMessage = sharedQueue.get(0);
- sharedQueue.remove(0);
- return tempMessage;
- } else {
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement