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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:19:36+00:00 2026-06-18T20:19:36+00:00

I have a Java application which has worker threads to process jobs. A worker

  • 0

I have a Java application which has worker threads to process jobs. A worker produces a result object, say something like:

class WorkerResult{
    private final Set<ResultItems> items;
    public Worker(Set<ResultItems> pItems){
         items = pItems;
    }
}

When the worker finishes, it does this operation:

 ...
 final Set<ResultItems> items = new SomeNonThreadSafeSetImplSet<ResultItems>();
 for(Item producedItem : ...){
      items.add(item);
 }
 passToGatherThread(items);

The items set is kind of a “unit-of-work” here. The passToGatherThread method passes the items set to a gather thread, of which only one exists at runtime.

Synchronization is not needed here, since race conditions cannot occur because only one thread (Gather-thread) reads the items set. AFAICS, the Gather-thread may not see all items because the set is not thread-safe, right?

Assume I cannot make passToGatherThread synchronized, say because it is a 3rd party library. What I basically fear is that the gather thread does not see all items because of caching, VM optimizations, etc. So here comes the question: How to pass the items set in a thread-safe manner, such that the Gather thread “sees” the proper set of items?

  • 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-18T20:19:37+00:00Added an answer on June 18, 2026 at 8:19 pm

    I have thought about (and discussed) this question a lot and I have come up with another answer, which, I hope, will be the best solution.

    Passing a synchronized collection is not good in terms of efficiency, because each subsequent operation on that collection will be synchronized – if there are many operations, it may prove to be a handicap.

    To the point: let’s make some assumptions (which I do not agree with):

    • the mentioned passToGatherThread method is indeed unsafe, however improbable it seems
    • compiler can reorder the events in the code so that the passToGatherThread is called before the collection is filled

    The simplest, cleanest and possibly the most efficient way to ensure that the collection passed to gatherer method is ready and complete is to put the collection push in a synchronized block, like this:

    synchronized(items) {
      passToGatherThread(items);
    }
    

    That way we ensure a memory synchronization and a valid happen-before sequence before the collection is passed, thus making sure that all objects are passed correctly.

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

Sidebar

Related Questions

I have a Java Application which has to load an DLL with a few
I have an application which resides in ROOT. This application has a java class(in
I have a java application which has a GUI in both English and French,
I have a java application which has a tray icon in the top menu
I have an java EE application which has one message-driven bean and it runs
I have created a java application which has a JTabbedPane that contains three JPanels.
I am developing a Java application which has some threads. I print in the
I have a java swing application which has a form that I populate by
I have a standalone Java application which has: -Xmx1024m -Xms1024m -XX:MaxPermSize=256m -XX:PermSize=256m Over the
I have Java application which sends pointer to function (callback) to some native dll

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.