开发者

Which thread does the variable belong to?

开发者 https://www.devze.com 2023-04-12 06:39 出处:网络
I hava some code like this: class LooperThread extends Thread { public Handler mHandler; public void run() {

I hava some code like this:

class LooperThread extends Thread {
    public Handler mHandler;

    public void run() {
        Looper.prepare();

        mHandler = new Handler() {
            public void handleMessage(Message msg) {
                  // process incoming messages here
            }
        };

        Looper.loop();
    }
}

I want to know which thread 开发者_如何学Pythonthe "mHandler" belong to?

Add: Is it different if I declare(or instance it) in different thread.


It doesn't belong to any thread. It is public so any thread can access it and modify. That being said it is crucial to make it thread safe (your code isn't - the variable isn't volatile).

The bottom line is - every object created using new operator is placed on the heap which is shared among all the threads. Other threads can access the object reference e.g. if it is not encapsulated like in your example or exposed on purpose.

Technically speaking - in Java any object does not belong to any other - all references to a given object are equal. And when the last reference to the object (huge oversimplification) are gone, it is garbage collected. So one can say: the last reference owns the object.


Threads can access all fields. Only local variables exist only in one thread.

If you want to access the thread in the out block you can use. LooperThread.this


Because Handler is an inner class, it can access the outer class using this syntax:

        public void handleMessage(Message msg) {
            // process incoming messages here
            Thread t = LooperThread.this;
        }

From there you should know what to do.

Please note: The instance of Thread is an object. But the thread in which the code is executed is a completely other thing. Don't confuse the representant Thread with the actual thing (CPU resource).


try using this inside your handleMessage method

Thread.currentThread().getName()


If what you're asking is "From which threads is it safe to access/modify the variable mHandle?", right now, your code does not do any locking around the assignment

mhandle = ...

so it would only be safe to access it from the looper thread. So if you had another piece of code on another thread checking

if (looperThread.mhandle == null) {
}

this would be not thread-safe. You'd have to lock around both the assignment and the access

synchronized (this) {
    mhandle = ...
}
....
synchronized (looperThread) {
    if (looperThread.mHandle == null) {
    }
}

On the other hand, if you are asking on which thread handleMessage is being called, it appears from the structure of the code that it is being called on the looper thread -- it looks like Looper.loop() goes into a loop, waits for messages and then calls handleMessage() when a message arrives. Just a guess, though.

Just remember that a variable doesn't actually belong to a thread -- it can be accessed from any thread. The real question is, when is it safe to access it from which threads?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号