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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:27:20+00:00 2026-06-07T15:27:20+00:00

This is hopefully an easy question regarding the thread-safety of the View.getWidth() and View.getHeight()

  • 0

This is hopefully an easy question regarding the thread-safety of the View.getWidth() and View.getHeight() methods.

I’m writing a background task that performs an operation given a View object. The only part of the view that is actually necessary is its dimensions. However, there is a possibility that the view hasn’t gone through the layout process when my task is initiated and so its dimensions are set to 0. The task is initiated on the UI but runs a long operation in the background before returning to the UI thread to post the result (much like an AsyncTask).

I was wondering what the repercussions are of calling those getter methods from a background thread. I know that the UI toolkit is designed to be single threaded and so I’m afraid that I may be running the risk of an unsafe publication.

Kcoppock has a great solution here about avoiding this problem all together by receiving a callback on the UI thread when the dimensions are known.

I wrote a small test to see when those dimensions would be visible from the background thread (attached below) and it seems that they are available at the same time as the callback.

Of course I understand that this doesn’t prove anything so I would be grateful if someone with a greater knowledge of the UI framework could chime in.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final ImageView view = new ImageView(this);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Log.d("TAG", Thread.currentThread().getName() + ": onGlobalLayout()");
            Log.d("TAG", Thread.currentThread().getName() + ": view dimensions: " + view.getWidth() + "x" + view.getHeight());
        }
    });

    // Bad code follows
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                Log.d("TAG", Thread.currentThread().getName() + ": view dimensions: " + view.getWidth() + "x" + view.getHeight());
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ignored) {}
            }
        }
    }).start();

    setContentView(view);
    Log.v("TAG", "Set view as content");
}
  • 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-07T15:27:21+00:00Added an answer on June 7, 2026 at 3:27 pm

    Since Android UI framework is designed to work in single-threaded environment it doesn’t have any internal synchronization and all View classes are not thread-safe. It means that there is no guarantee that width and height values will be visible from your background thread after they were set from UI thread. You may receive stale values or not receive any positive values at all in your background thread. There is a Java memory model which describes how threads in the Java interact through memory and it states what you need to do to make your variables visible through different threads.

    I suppose you code will work on uniprocessor systems but may possibly fail on SMP. I can’t say that this a severe issue but anyway this code is not correct in respect of Java memory model. To fix it you need to publish width and height values safely using volatile modifier or synchronized block or AtomicInteger. Your code will looks something like that:

    ...
    private volatile int width;
    private volatile int height;
    ...
    @Override
    public void onGlobalLayout() {
        // here values are published safely
        width = view.getWidth();
        height = view.getHeight();
    }
    ...
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                // and here they are read
                Log.d("TAG", Thread.currentThread().getName() + ": view dimensions: " + width + "x" + height);
                try {
                    Thread.sleep(1);
                } catch (InterruptedException ignored) {}
            }
        }
    }).start();
    

    volatile modifier guarantees that any changes in width and height variables made from UI thread will be seen immediately in background thread and this background thread will receive most recent values.

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

Sidebar

Related Questions

Ok hopefully this is an easy question: I have a main viewcontroller that is
Hopefully an easy question, I have a thread running in a class that just
This is hopefully an easy question to answer for someone familiar with Mozilla, but
I just started WPF this morning so this is (hopefully) an easy question to
Hopefully this is a quick and easy question to answer. Do I need the
Hopefully this is an easy question. How can I define an XML type such
Hopefully this is an easy/dumb question. I am redirecting program input and output to
This question should hopefully be easy to answer. I created a few buttons in
Hopefully this will be an easy question to answer. Under LatLngBounds ( https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds ),
I have a (hopefully easy) question regarding Amazon S3 and Cloudfront. I'm using S3Fox,

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.