Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:44:03+00:00 2026-05-26T12:44:03+00:00

I have a Situation where I wrote a simple Producer Consumer model for reading

  • 0

I have a Situation where I wrote a simple Producer Consumer model for reading in chunks of data from Bluetooth then every 10k bytes I write that to file. I used a standard P-C Model using a Vector as my message holder. So how do I change this so that multiple Thread consumers can read the same messages, I think the term would be Multicaster? I am actually using this on an Android phone so JMS is probably not an option.

static final int MAXQUEUE = 50000; 
private Vector<byte[]> messages = new Vector<byte[]>(); 

/**
 * Put the message in the queue for the Consumer Thread
 */
private synchronized void putMessage(byte[] send) throws InterruptedException { 

    while ( messages.size() == MAXQUEUE ) 
        wait(); 
    messages.addElement( send ); 
    notify(); 
} 


/**
 * This method is called by the consumer to see if any messages in the queue
 */
public synchronized byte[] getMessage()throws InterruptedException { 
    notify(); 
    while ( messages.size() == 0 && !Thread.interrupted()) {
        wait(1); 
    }
    byte[] message = messages.firstElement(); 
    messages.removeElement( message ); 
    return message; 
} 

I am referencing code from an Oreilly book Message Parser section

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:44:04+00:00Added an answer on May 26, 2026 at 12:44 pm

    This is what I came up with as an example when digging through some code and modifiying some existing examples.

    package test.messaging;
    
    import java.util.ArrayList;
    import java.util.concurrent.LinkedBlockingQueue;
    
    public class TestProducerConsumers {
    
        static Broker broker;
    
        public TestProducerConsumers(int maxSize) {
            broker = new Broker(maxSize);
            Producer p = new Producer();
            Consumer c1 = new Consumer("One");
            broker.consumers.add(c1);
            c1.start();
    
            Consumer c2 = new Consumer("Two");
            broker.consumers.add(c2);
            c2.start();
    
            p.start();
        }
    
        // Test Producer, use your own message producer on a thread to call up
        // broker.insert() possibly passing it the message instead.
        class Producer extends Thread {
    
            @Override
            public void run() {
                while (true) {
                    try {
                        broker.insert();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        class Consumer extends Thread {
            String myName;
            LinkedBlockingQueue<String> queue;
    
            Consumer(String m) {
                this.myName = m;
                queue = new LinkedBlockingQueue<String>();
            }
    
            @Override
            public void run() {
                while(!Thread.interrupted()) {
                    try {
                        while (queue.size() == 0 && !Thread.interrupted()) {
                            ;
                        }
                        while (queue.peek() == null && !Thread.interrupted()) {
                            ;
                        }
                        System.out.println("" + myName + " Consumer: " + queue.poll());
                    } catch (Exception e) { }
                }
            }
        }
    
        class Broker {
            public ArrayList<Consumer> consumers = new ArrayList<Consumer>();
    
            int n;
            int maxSize;
    
            public Broker(int maxSize) {
                n = 0;
                this.maxSize = maxSize;
            }
    
            synchronized void insert() throws InterruptedException {
                        // only here for testing don't want it to runaway and 
                        //memory leak, only testing first 100 samples.
                if (n == maxSize)
                    wait();
                System.out.println("Producer: " + n++);
                for (Consumer c : consumers) {
                    c.queue.add("Message " + n);
                }
            }
    
        }
    
        public static void main(String[] args) {
            TestProducerConsumers pc = new TestProducerConsumers(100);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a fairly simple situation: I have a high speed producer of data
I have situation in which I read a record from a database. And if
I have situation where a user can manipulate a large set of data (presented
I have a partial view that inherits from ViewUserControl<Guid?> - i.e. it's model is
Situation: I am running my website on wordpress. I have installed the Simple Facebook
Simple situation(VS2005, .NET2): I have a textBox1 on a panel1. I need to track
I wrote a simple file parser and writer, but then I came across an
Here is the situation, i have a website that can be accessed from multiple
This should be relatively simple, here is my situation; I have copied the trunk
The Situation I have a very compressed time schedule to write a simple (basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.