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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:35:31+00:00 2026-06-17T00:35:31+00:00

I am new to multi-threading and would like to avoid a race condition that

  • 0

I am new to multi-threading and would like to avoid a race condition that is occurring in the below code. In the release() method there is a line available.add(resource) and in the remove() method there is a line available.remove(resource). So my question is how do I synchronize the ‘resource’ variable to avoid this race condition?

    package threadpool;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.concurrent.ConcurrentMap;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.LinkedBlockingQueue;
    import java.util.concurrent.TimeUnit;

    public class ResourcePoolImpl<R> implements ResourcePool<R> {

    private static final String CLOSED_POOL_EXCEPTION = "Pool is closed,cannot aquire resource.";

    private static final String  RELEASE_EXCEPTION = "Unaquired resource, cannot release it.";

    private volatile boolean open = false;

    private final BlockingQueue<R> available = new LinkedBlockingQueue<R>();

    private final ConcurrentMap<R, CountDownLatch> aquired = new ConcurrentHashMap<R,  CountDownLatch>();

    public R acquire() throws InterruptedException {
    if ( !open ) { throw new IllegalStateException( CLOSED_POOL_EXCEPTION ); }
    final R resource = available.take();
    if ( resource != null ) {
        aquired.put( resource, new CountDownLatch( 1 ) );
    }
    return resource;
    }


   public R acquire( final long timeout, final TimeUnit timeUnit ) throws InterruptedException {
    if ( !open ) { throw new IllegalStateException( CLOSED_POOL_EXCEPTION ); }

    final R resource = available.poll( timeout, timeUnit );
    if ( resource != null ) {
        aquired.put( resource, new CountDownLatch( 1 ) );
    }
    return resource;
    }


    public boolean add( final R resource ) 
    {
    return available.add( resource );
    } 

    public void close() throws InterruptedException {
    open = false;
    for ( final CountDownLatch latch : aquired.values() ) {
        latch.await();
    }
    }

    public void closeNow() {
    open = false;
    }

    public boolean isOpen() {
    return open;
    }

    public void open() {
    open = true;
    }

    public void release( final R resource ) 
    {
    final CountDownLatch latch = aquired.get( resource );
    if ( latch == null ) { throw new IllegalArgumentException( RELEASE_EXCEPTION ); }
    available.add( resource );
    latch.countDown();
    }

    public boolean remove( final R resource ) throws InterruptedException 
    {   

    final CountDownLatch latch = aquired.get( resource );
    if ( latch != null ) {
        latch.await();
    }
    return available.remove( resource );
    }


    public boolean removeNow( final R resource ) {
    return available.remove( resource );
    }

 }
  • 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-17T00:35:32+00:00Added an answer on June 17, 2026 at 12:35 am

    Declare a

    final Object mutex = new Object();
    

    and have all methods that do read/write operations on shared collections aquire the mutex before doing the operations, or make decisions based on shared data, execute within a synchronized block:

    synchronized (mutex) {
         // .. guaranteed single-threaded access here
         //   (for instance, contents of aquire() or release(); 
         //      also add() or any other collection access)
    }
    

    You can then use the simpler non-concurrent collection classes, as within the mutex-guarded areas, there cannot be any multi-threaded access.

    Concurrent collections simply wrap their accesses within their own internal mutual-exclusion locks — but the problem, as you explain in your comments, is that aquired and available may be updated independently of each other, which you definitely do not want.

    Therefore: simplify your code by declaring and using a single mutex for all critical-area accesses.

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

Sidebar

Related Questions

I am pretty new to multi-threading in general and I would like to know
I am new to multi threading in xcode for iphone. i would like to
I am using Glib to develop a multi-threading C software. I would like to
I am relatively new to multi-threading and want to execute a background task using
I'm new to MonoTouch development and I would like to embed some PDF Viewing
I would like to design a simple application (without j2ee and jms) that can
I am quite new to multi-threading, I have a single threaded data analysis app
My multi-threading knowledge is still pretty rudimentary, so would really appreciate some pointers here.
I am new to Java and I would like to know which would be
I'm kinda new to Multi-Threading and have only played around with it in 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.