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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:27:08+00:00 2026-06-03T01:27:08+00:00

I want to know if I understand correctly how to use synchronized I will

  • 0

I want to know if I understand correctly how to use synchronized I will write down some pseudo code, which mimics what I am trying to do.

public MyApp extends Application {

    public Object mLock = new Object();

    private final ArrayList<String> MyList = new ArrayList<String>();

    public ArrayList<String> getMyList(){
        return MyList;
    }
}


private class MyTask extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... url) {
        final ArrayList<String> BgList = MyApp.getMyList();
        synchronized (mLock) {
            while(somefinishcondition) {
                try {
                    // Get some stuff from the web
                    BgList.add(newstuff);
                    mLock.notifyAll();
                }
                catch (IOException e) {
                }
            }
        }
        return null;
    }
}


public class DisplayActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewer);

        final ArrayList<String> FgList = MyApp.getMyList();

        synchronized (mLock) {
            while(somecondition) {
                mLock.wait();
                if(FgList.size()>lastSize) {
                    n = FgList.size();
                    for(i=lastSize+1; i<=n ; i++){
                        Display(Fglist.get(i));
                    }
                    lastSize = n;
                }
            }
        }
}

Here is what I think I am doing: I am running a data gathering task in the background, putting the data in a global list. Each time I have new data I notify the DisplayActivity, which goes and displays all new data since the last time.
The AsyncTask locks the data until new data arrives. The DisplayActivity waits on the lock until notified to proceed and display.
Is this right? Is this the usual 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-03T01:27:11+00:00Added an answer on June 3, 2026 at 1:27 am

    No, this way you can block the UI thread in onCreate and you will get Application Not Responding error.

    Instead, whenever you have some new data, collect the data in doInBackground() then call updateProgress() in doInBackground(). This will result in AsyncTask.onProgress() call from the UI thread. In onProgress() do whatever you need with the new data.

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

Sidebar

Related Questions

I'm trying to understand javascript scopes, and I want to know if in the
I'm trying to figure out if I understand correctly what modular code really is.
I want to know which php architecture strategies developers use in complex php applications
I don't understand garbage collection so good, then I want to know, why it's
Want to know ways to store OBJECTS of a Class in some persistent storage.
I want to know if there is any way by which I can paste
I'm trying to understand the syntax for calling use to load a library: (use
I'm trying to understand better how Windows sessions work, so if I have some
I'm trying to understand how to use GIT with Xcode 4.2. I haven't been
I want to know how does the code below work to do because I

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.