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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:57:02+00:00 2026-06-15T10:57:02+00:00

// is this atomic? public int size() { return count; } Note that count

  • 0
// is this atomic? 
public int size() {
    return count;
}

Note that count can be changed by other methods in other threads.

I know integer reads and writes are atomic, but I am not sure about return.

What got me alarmed is that for some reason ArrayBlockingQueue locks it’s size() method.

  • 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-15T10:57:03+00:00Added an answer on June 15, 2026 at 10:57 am

    Reads and writes to primitive int are atomic as you already know. Returning is basically reading and placing in some other place in memory. Since reading is atomic, no race condition will occur. You either return previous or next value of int.

    Using lock in ArrayBlockingQueue might be due to visibility reasons. count variable is not volatile so if the queue was modified in the meantime, without some sort of locking you are not guaranteed to see the most recent value of count. But since read and writes are atomic, at least you’ll never see youngest 16 bits of old value and oldest 16 bits of new value.

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

Sidebar

Related Questions

I know that C++11 provides <atomic> , but this isn't implemented by any compiler
I have a class called Atomic which is basically an _Atomic_word plus methods that
Are java primitive integers (int) atomic at all, for that matter? Some experimentation with
I've got some (Entity Framework 4) code that looks like this: class QueueItem{ public
This is the code that I got some where, I can not find the
I have a method with a single atomic operation, like this one int value;
I'm using an invocation described in this question: Synchronization accross threads / atomic checks?
I have a unique type integer. I use it like this: int unique=0; public
Can this object pool cause visibility problems with multiple threads? I'm specifically wondering about
I have a class that has a decimal property accessible from multiple threads public

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.