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 7513431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:11:08+00:00 2026-05-30T00:11:08+00:00

I have a LinkedBlockingQueue in a Mediator in my Producer-Mediator-Consumer model. Producer first updates

  • 0

I have a LinkedBlockingQueue in a Mediator in my “Producer-Mediator-Consumer” model. Producer first updates the Mediator adding to the activityQueue. Next the Consumer/Activity waits/listens on the queue and grabs the next item.

I want an Activity to see the queue size has changed and grab the next item. The Mediator has no visibility into the activity only the activity can see the Mediator. So how do I create this listener mechanism i want?

Here is my mediator class that holds the Queue and the Activity will somehow look at the queue and get informed if it needs to update. Data coming into the queue can be spotty and random at times so a polling mechanism will not work.

public class MediatorData {

    /** Queue for the Activity */
    LinkedBlockingQueue <byte[]> queueConsumer = new LinkedBlockingQueue <byte[]>();

    /**
     * Add data to a queue(s) for consumption
     */
    public void put(byte[] data) throws InterruptedException {
        queueConsumer.add(data);
    }

    /**
     * Return data from the queue for the Feature calculations
     */
    public byte[] getFeatureData() throws InterruptedException {
        return queueConsumer.poll(100, TimeUnit.MILLISECONDS);
    }

}

Example of my activity class, its a graphing class so the queue listener has to be efficient and fast.

public class DisplayGraph extends Activity {

    // populated from Application Class where its created
    pirvate MediatorData md;

    public void onCreate(Bundle savedInstanceState) {
        md = getMediator();  // This comes from the custom Application class

        ... some type of listener to queue 
    }

    private void getQueueData() {
        byte[] tv = md.queueConsumer.poll();
        // can't update textview  get exception CalledFromWrongThreadException
        ((TextView) DisplayGraph.this.findViewById(R.id.tv)).setText("TV " + tv[0]);
    }
}
  • 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-30T00:11:10+00:00Added an answer on May 30, 2026 at 12:11 am

    Best way to handle this situation is to do this: Previous answer has errors in it.

    public class MediatorData extends Observable {
    
        /** Queue for the Activity */
        LinkedBlockingQueue <byte[]> queueConsumer = new LinkedBlockingQueue <byte[]>();
    
        /**
         * Add data to a queue(s) for consumption
         */
        public void put(byte[] data) throws InterruptedException {
            queueConsumer.add(data);
            notifyObservers();
        }
    
        /**
         * Return data from the queue for the Feature calculations
         */
        public byte[] getFeatureData() throws InterruptedException {
            return queueConsumer.poll(100, TimeUnit.MILLISECONDS);
        }
    }
    

    Display activity needs to run the update on the UI Thread so use the runOnUiThread method.

    public class DisplayGraph extends Activity implements Observer {
    
        // populated from Application Class where its created
        private MediatorData md;
    
        byte[] tv;
    
        public void onCreate(Bundle savedInstanceState) {
            md = getMediator();  // This comes from the custom Application class
            md.addObserver(this);
        }
    
        private void getQueueData() {
            tv = md.queueConsumer.poll();
            runOnUiThread(setRunnable);
        }
    
        public void update(Observable arg0, Object arg1) {
            getQueueData();
        }
    
        // Need to do this to update the data to the UI.
        final Runnable setImageRunnable = new Runnable() {
            public void run() {
                ((TextView) DisplayGraph.this.findViewById(R.id.tv)).setText("TV " + tv[0]);
            }
        };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a consumer thread taking elements from a LinkedBlockingQueue , and I make
I have a producer-consumer like scenario. Class A produces objects of type E. I
Have a SQL problem, adding this model all works correctly, the problem is in
I have a ThreadPoolExecutor that is constructed with an unbounded queue (LinkedBlockingQueue) and a
i have this scenario: class MyClass { Producer p; Consumer c; public static void
I have a class A with a LinkedBlockingQueue as property. In one method of
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have 3 threads: 2 consumers, ConsumerA and ConsumerB , and a Producer .
I have the following code to declare a queue: Connection connection = RabbitConnection.getConnection(); Channel
I have a LinkedBlockingQueue called tasks , but when I call tasks.take() and wait

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.