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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:44:26+00:00 2026-06-04T11:44:26+00:00

I think this is not an easy question. I’ll be brief and give a

  • 0

I think this is not an easy question.
I’ll be brief and give a little example of what is happening.
Let’s say we have a source of data in file Byron.txt:

SHE walks in beauty, like the night
Of cloudless climes and starry skies;
And all that ‘s best of dark and bright
Meet in her aspect and her eyes:
Thus mellow’d to that tender light
Which heaven to gaudy day denies.

And this code execute inside an AsyncTask:

final ArrayList<Record> poem = new ArrayList<Record>();
final Object objectLock = new Object();
private Record rec = new Record();

@Override
protected Void doInBackground(Void... args) {
    String line = null;
    int i;
    int last;

    try {
        process = Runtime.getRuntime().exec("cat Byron.txt");
        bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()), 8192);

        synchronized (objectLock) {
            poem.clear();
            last = i = poem.size() - 1;
        }
        while(line = bufferedReader.readLine()) != null) {
            rec.setString(line);
            synchronized (objectLock) {
                last++;
                poem.add(last, rec);
            }

            while(!bPause && i < last) {
                i++;
                publishProgress(poem.get(i));
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected synchronized void onProgressUpdate(Record... m) {
    if(m.length > 0) {
        mContext.mTable.appendRow(m[0]);
    }

}

where there is a TableLayout in the UI and each time we get a new line we add a new TableRow to it.
And this is the output we see in the UI:

SHE walks in beauty, like the night
Of cloudless climes and starry skies;
Of cloudless climes and starry skies;
Of cloudless climes and starry skies;
Thus mellow’d to that tender light
Thus mellow’d to that tender light

And we go into the debugger and we see why it happens.
Sometimes the synchronized (objectLock) is skipped and the loop continues.
There is no publishing because i already catched last.
Later the block is executed as many times as it was skipped,
, but the original line is lost and the current line is added instead to poem several times
Then, all the new lines are published until i catches last again.

So you see that I followed the code and I can explain what’s happening, the question here is: Why the block is skipped?, Why?
I expected the synchronized block to stall until it can be executed.
At least this how I understood the function of synchronized (objectLock)
even without using wait() and notify()

I don’t pretend to open a discussion here (although if you want we can open one in the chat area)
If you see some fault in the code, then, answer the question to let me know.

NOTES:

  1. synchronized is needed because somewhere else in the app, the user may want to email the lines he got so far.
  2. The user may pause the publishing (bpause); that’s the while loop and i follows last only when bPause is false.
  • 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-04T11:44:28+00:00Added an answer on June 4, 2026 at 11:44 am

    I decided to publish the answer. Even though I feel very embarrassed by its simplicity.
    I discovered it only after I had already dug deep into AsyncTask class and message handling and whatnot.

    I publish it in hope it will help people to check the basic things before jumping to
    conclusions, and that someone out there will save himself half a day debugging because of
    this post.

    The Record rec was the same one each time. The poem ArrayList had the same element id for each entry. And the content changed on all of them at once, since they were all the same.
    When the progress was published immediately it printed the right string, the last one. But if some delay cause the progress to publish later, then retrieving the poem.get(i) records retrieved a different entry but with the same pointer, thus, the same content.

    The solution was to create a new Record each loop.

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

Sidebar

Related Questions

Easy question, it is valid to have overlapping spans in html? Example: <span id=1>This
I think this is an easy question, but I am not able to find
I think this is a simple question, but I can not find the answer
[I hope that this question is not too broad, I think that the subject
I have tried every way I can think of with this, but just not
This is a really short question I think but I'm not sure I understand
I think this is very easy question for someone who is more experienced in
This feels like a too easy question to be found with google, I think/hope
i think this question is very easy but I don't know if i am
this is probably a fairly easy question and I think it's more amusing than

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.