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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:13:10+00:00 2026-06-12T10:13:10+00:00

As title states I have problem with thread based structure. What I need to

  • 0

As title states I have problem with thread based structure. What I need to do is:

  • one thread running in loop and checking if there exists something in list and if so then performing some operation on object and then removing it from list

  • function which is called from ‘outside’ and adding new objects to this list.

here is my approach:

public class Queue implements Runnable {
    private List<X> listOfObjects = new ArrayList<X>;

    public void addToList(X toAdd){
        listOfObject.add(toAdd);
    }

    public void run() {
        while(true){
            synchronized(listOfObjects){
            if(!listOfObjects.isEmpty()){
                listOfObjects.get(0).doSth();
                listOfObjects.remove(0);
            }
            }
        }
    }
}

Is it proper approach? Should I also synchronize adding to this list?

  • 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-06-12T10:13:11+00:00Added an answer on June 12, 2026 at 10:13 am

    Looks like you should try an implementation of java.util.concurrent.BlockingQueue, rather than try and write your own! I suspect a LinkedBlockingQueue will work nicely for you. You can write entries into the queue from multiple sources, and your consumer thread will take each entry off and process it in turn, in a thread-safe fashion.

    Note that your consumer thread for a BlockingQueue will wait (by calling the take() method). Your implementation above, however, will spin and consume CPU whilst waiting for entries to be processed in the queue (this should be evident if you run it and monitor your CPU usage).

    Here’s an explanatory article.

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

Sidebar

Related Questions

i have a problem just as title, and the http-status is 302 Moved Temporarily.
As the title states, I have a C# console app which uses interop to
As the title states, I have a svg image, but I am not able
As the title states, is there a way to prevent extra elements from showing
I have 2 tables - posts and postmeta. posts ID title category post_status post_type
I have a Ruby hash: @tags = { project_status => { title => Project
I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
For edit home status of category I have a link: <span class=\ha\ title=\Active in
As the title states, I am using devise. I came upon this link that
As the title states, if I don't clean my project before building, neither the

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.