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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:31:10+00:00 2026-06-01T08:31:10+00:00

my code : private AtomicBoolean fetched1 = new AtomicBoolean(false); private int rowCount; public int

  • 0

my code :

    private  AtomicBoolean fetched1 = new AtomicBoolean(false);

    private int  rowCount;

    public int getRowCount() {
            data.getRealm().exec(new Runnable(){
                @Override
                public  void run() {
                    rowCount = data.size();
                    fetched1.set(true);
                }
            });
            while(!fetched1.get()){
            }
            fetched1.set(false);
            return rowCount;
        }

it seems work for me right now, but I am not familiar with threading(it always confuse me), should I do it like above code any way?

  • 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-01T08:31:11+00:00Added an answer on June 1, 2026 at 8:31 am

    should I do it like above code any way?

    This looks to be a spin loop which will use unnecessary CPU. It is better to use wait and notify to signal that the data has been fetched. Something like:

       private final Object lock = new Object();
       private volatile Integer rowCount = null;
       ...
    
          public void run() {
             rowCount = data.size();
             synchronized (lock) {
                lock.notify();
             }
          }
    
      synchronized (lock) {
         // we loop here in case of race conditions or spurious interrupts
         while (rowCount == null) {
            lock.wait();
         }
      }
      ...
    

    I don’t think you need a fetched AtomicBoolean at all. You should make the rowCount be volatile and then you can test its value. The while loop is a good pattern to follow because of producer/consumer race conditions and spurious interrupts.

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

Sidebar

Related Questions

I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
I have the following code: private Dictionary<int, Entity> m_allEntities; private Dictionary<Entity, List<IComponent>> m_entityComponents; public
Code ...{ private void createRadioButton() { final RadioButton[] rb = new RadioButton[5]; for(int i=0;
I have the following code: private Models.mediamanagerEntities dataModel = new Models.mediamanagerEntities(); public ActionResult Index(FormCollection
Here is my code: private Button.OnClickListener gotologinpage3 = new Button.OnClickListener() { public void onClick(View
Consider this code private MailMessage GetMailMessageFromMailItem(Data.SystemX.MailItem mailItem) { var msg = new MailMessage(); foreach
The code is like this: private DatabaseHelper mOpenHelper; @Override public boolean onCreate() { mOpenHelper
this is my code private void sendToClient(Dictionary<string, string> reportDic) { Response.Clear(); Response.BufferOutput = false;
Code: private final int A0 = 0; private final int A1 = 1; .....
I have this code private void saveImage() { Bitmap bmp1 = new Bitmap(pictureBox.Image); bmp1.Save(c:\\t.jpg,

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.