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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:10:33+00:00 2026-06-02T22:10:33+00:00

Can this object pool cause visibility problems with multiple threads? I’m specifically wondering about

  • 0

Can this object pool cause visibility problems with multiple threads? I’m specifically wondering about this kind of execution sequence:

  • Thread A – obtainObject()
  • Thread A – modifies object (lets say visibleState = 42)
  • Thread A – releaseObject()
  • Thread B – obtainObject() (get the object just released by A)
  • Thread A – does something unrelated or dies
  • Thread B – modifies object (lets say visibleState = 1)
  • Thread B – print objects visibleState
  • Thread B – releaseObject()

Could the modification from Thread A possibly become visible to Thread B after B has modified the state itself? (I know it doesn’t occur in practice, but I can’t figure out if and how the JLS/Javadoc guarantees this).

Here’s the code, stripped down to only show the essentials. I left out generification and a factory to create objects.

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class ObjectPool {

/** Maximum number of objects to be kept in pool */
int maxPoolSize = 10;

/** First object in pool (LIFO queue) */
final AtomicReference<PoolableObject> firstObject = new AtomicReference<PoolableObject>();

/** How many objects are currently in the pool */
final AtomicInteger poolSize = new AtomicInteger();

/** Gets an object from the pool. If no object is available
 * from the pool, a new object is created and returned */
public PoolableObject obtainObject() {
    while (true) {
        PoolableObject object = firstObject.get();
        if (object == null)
            break;
        if (firstObject.compareAndSet(object, object.next)) {
            poolSize.decrementAndGet();
            return object;
        }
    }
    // no more objects in pool, create a new object
    return new PoolableObject();
}

/** Returns an object to the pool. */
public void releaseObject(final PoolableObject object) {
    while (true) {
        if (poolSize.get() >= maxPoolSize)
            break;
        final PoolableObject first = firstObject.get();
        object.next = first;
        if (firstObject.compareAndSet(first, object)) {
            poolSize.incrementAndGet();
            break;
        }
    }
}

}

The objects managed by the pool are supposed to inherit from this class:

public class PoolableObject {

/** Links objects in pool in single linked list. */
PoolableObject next;

public int visibleState;

}
  • 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-02T22:10:35+00:00Added an answer on June 2, 2026 at 10:10 pm

    As far as visibility goes, AtomicReference has the same memory barrier properties as a volatile variable.

    Among those properties are that all actions of a thread before a write to a volatile variable “happen-before” that write. Or, to put it another way, if source code shows an action happening before a volatile write, that action can’t be re-ordered by the JITC to occur after the volatile write. Thus, the scenario cited in the question won’t be a problem; changes to the object made before it is released will be visible to other threads that subsequently capture the object.

    However, I don’t completely follow the way this pool is supposed to work, and there could be other problems. In particular, I’m not convinced that the lack of atomicity between firstObject and poolSize is safe. Threads can definitely see these variables in an inconsistent state, because they are not synchronized; I can’t tell if that matters though.

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

Sidebar

Related Questions

Trying to do an image caption but can't figure the this object selector right
If I'm using a Hashtable , I can write code like this: object item
How can I format the following line to eliminate StyleCop warnings: this.BeginInvoke(this.updateHandler,new object[]{this.tmpbuf}); Now,
I have an object config which has some properties. I can export this ok,
In javascript on a browser, I can do this to see if an object
Can I do this without reference to the object in the constructor? In other
Hello can anybody solve this please I'm creating the object in the action class
Using the C# object initializer syntax I can instantiate an anonymous object like this:
When you have an object instance in C#, you can use the this keyword
How can I extract the groups from this regex from a file object (data.txt)?

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.