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

  • Home
  • SEARCH
  • 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 6700753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:51:09+00:00 2026-05-26T06:51:09+00:00

I have one thread that add() s objects into a LinkedList queue, and another

  • 0

I have one thread that add()s objects into a LinkedList queue, and another thread that poll()s the queue for objects to process. These re the only two methods I use on my queue. I never iterate through the queue, nor add nor remove objects in the middle of the list. I cannot think of a scenario when the two threads step on each other and corrupt the data somehow, but perhaps my imagination is simply lacking.

The pushing is infrequent (several times per second) but the polling is very frequent (a couple thousand times per second). I wonder how much of a penalty I get for synchronizing the add() and the poll(). This is running on Android.

Edit: I am not looking for a BlockingQueue; I am blocking on I/O, not on objects in the queue:

The run() method on the polling thread blocks waiting for space to become available in an output buffer. When space becomes available, it looks to see if it has any objects waiting on the queue. If one is available, it serializes it into the output buffer. If the queue is empty (i.e. poll() returns null), it poll()s other, lower-priority queues, and if all are empty, serializes a “no data available now” message.

  • 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-26T06:51:10+00:00Added an answer on May 26, 2026 at 6:51 am

    Without synchronization, you may get the case where the reading thread is polling the an object that has JUST been added to the queue, and that the list has not quite finished exiting out of the add() method.

    If you look into the source, it’s possible to screw something up with “expectedModCount = l.modCount;” because the latch for the poll is based on a line below it before modCount is actually modified.

    Essentially, your remove is happening at the perfect time, probably with 1 element, while adding another at the same time, and getting something wrong.

    To prevent this, you can wrap your accesses in synchronized(lst){} blocks or you can use the concurrent classes. I would prefer the concurrent queues because poll doesn’t have to spin — you can have a blocking take().

    You’re probably looking for: java.util.concurrent.ArrayBlockingQueue

    ArrayBlockingQueue<String> que = new ArrayBlockingQueue<String>(100);
        que.add("");
        que.take();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one thread that writes results into a Queue. In another thread (GUI),
I have an application that has two threads. The first one (the main thread)
I have two threads, one thread processes a queue and the other thread adds
I have a collection of objects that is modified by one thread and read
I have one thread that is receiving data over a socket like this: while
Let's say that I have the following code that's run in one thread of
I have one std::list<> container and these threads: One writer thread which adds elements
I have 2 threads and global Queue, one thread (t1) push the data and
I have a Perl script that launches 2 threads,one for each processor. I need
I am writing code in VS2005 using its STL. I have one UI thread

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.