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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:52:53+00:00 2026-05-19T01:52:53+00:00

I currently have a concurrent queue implementation that uses a BlockingQueue as the data

  • 0

I currently have a concurrent queue implementation that uses a BlockingQueue as the data store. I now need to introduce a second type of object that has a higher priority, leading me towards a starvation/priority queue for the original queue. So we’re working with objects of type A and type B being produced from multiple threads. Any objects of type B should be processed before those of type A, but other than that FIFO order MUST be maintained. So if { 1A, 1B, 2A, 3A, 2B } are inserted the order should be {1B, 2B, 1A, 2A, 3A}

I tried a single PriorityBlockingQueue to push type Bs to the front, but I couldn’t maintain the FIFO requirement (there’s no natural order between items of the same type).

My next thought is to use two concurrent queues. I’m looking for common gotchas or considerations when coordinating access between the two queues. Ideally, I’d want to do something like this:

   public void add(A a)
   {
       aQueue.add(a);
   }
   public void add(B b)
   {
       bQueue.add(b);
   }

   private void consume() 
   {
       if(!bQueue.isEmpty())
          process(bQueue.poll());
       else if(!aQueue.isEmpty())
          process(aQueue.poll());
   }

Would I need any synchronization or locks if both queues are ConcurrentLinkedQueue (or insert more appropriate structure here)? Note I have many producers, but only one consumer (single threaded ThreadPoolExecutor).

EDIT: If a B comes in after the isEmpty() check, it’s ok to process an A and handle it on the next consume() call.

  • 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-19T01:52:53+00:00Added an answer on May 19, 2026 at 1:52 am

    I am not sure if I got your situation right, but I think it should be possible to solve this using a single queue.

    You said your objects (in the queue) should be comparable by natural order and type. If there is no natural order, simply have a sequence generator (i.e. AtomicLong) that will give your objects unique, always-incrementing queue ID. Getting data from AtomicLong should take no time, unless you’re in the world of nanoseconds.

    So your Comparator.compare should be like this:

    1) check object type. If it is different (A VS B), return 1/-1. Otherwise, see below
    2) check id. It is guaranteed to be different.

    If you cannot change objects (A and B), you can still wrap them into another object which would contain that ID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a DetailsView in ASP.NET that gets data from the database based
I currently have an MS Access application that connects to a PostgreSQL database via
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView
I currently have speakers set up both in my office and in my living
I currently have an existing database and I am using the LINQtoSQL generator tool
We currently have a company email server with Exchange, and a bulk email processing
I currently have a fairly robust server-side validation system in place, but I'm looking
I currently have heavily multi-threaded server application, and I'm shopping around for a good
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have a class and I'm trying to create an easy GUI to

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.