简体   繁体   English

另一个线程的代码如何在主线程上运行?

[英]How could another thread's code run on the main thread?

Lately, I was confused by some Android API. 最近,我对某些Android API感到困惑。 Here is some simplified explanation, cause the code is a little long which is totally right, but just confuse me. 这是一些简化的说明,因为代码有点长,这是完全正确的,但是让我感到困惑。

I have two thread: UI thread and another HandlerThread called AThread . 我有两个线程:UI线程和另一个称为AThread HandlerThread

mResponseHander is a Handler created in UI thread which obviously associtated to the UI thread's looper. mResponseHander是在UI线程中创建的Handler ,它显然与UI线程的循环程序相关联。 Then I pass the mResponseHandler to the AThread . 然后,我将mResponseHandler传递给AThread

AThread is a HandlerThread that does some image downloading task. AThread是一个HandlerThread ,它执行一些图像下载任务。 In AThread , I wrote some code like this: AThread ,我编写了如下代码:

mResponseHandler.post(new Runnable() {
        @Override
        public void run() {

            if (mRequestMap.get(target) != url) {
                return;
            }

            mRequestMap.remove(target);
            mThumbnailDownloadListener.onThumbnailDownloaded(target, bitmap);
        }
    });

Besides, the variables mRequestMap and mThumbnailDownloadListener and others are only defined in the AThread . 此外,变量mRequestMapmThumbnailDownloadListener等仅在AThread定义。

I know when I call mResponseHandler.post(new Runnable) , the Runnable will later run on the UI thread cause the mResponseHandler is associtated to the UI thread's looper. 我知道当我调用mResponseHandler.post(new Runnable)Runnable将稍后在UI线程上运行,因为mResponseHandlermResponseHandler到UI线程的mResponseHandler程序。

Here is the question: why the code above running in the UI thread is still right when the variables mRequestMap and mThumbnailDownloadListener and others are not defined in the UI thread but only defined in the AThread ? 问题是:当变量mRequestMapmThumbnailDownloadListener和其他变量未在UI线程中定义而仅在AThread定义时,为什么在UI线程中运行的上述代码仍然正确?

why the code above running in the UI thread is still right when the variables "mRequestMap" and "mThumbnailDownloadListener" and others are not defined in the UI thread but only defined in the AThread? 当变量“ mRequestMap”和“ mThumbnailDownloadListener”以及其他变量未在UI线程中定义而仅在AThread中定义时,为什么在UI线程中运行的上述代码仍然正确?

Any class instance (lets use "object" further) resides in the JVM heap (not "in threads" or anything else). 任何类实例(让它们进一步使用“对象”)都驻留在JVM heap (而不是“在线程中”或其他任何东西)。 When applying the new operator as new运算符应用为

Type variableName = new Type();

a piece of memory is allocated on the heap and reference to the memory is stored as value of variableName . 在堆上分配了一块内存,对该内存的引用存储为variableName值。 From now on, any object (eg a Thread object or an object that implements Runnable , like in your case) that has the reference ( variableName or its copy) to the Type object, can "operate" on it. 从现在开始,对Type对象具有引用( variableName或其副本)的任何对象(例如Thread对象或实现Runnable的对象,例如您的情况)都可以对其进行“操作”。

With that said, by posting the Runnable to the handler associated with the Looper of the UI-thread you tell the thread what job should be done with the objects that reside in the heap and can be referred by mRequestMap and mThumbnailDownloadListener . 话虽如此,通过将Runnable发布到与UI线程的Looper关联的处理程序,您可以告诉线程应使用驻留在堆中的对象完成哪些工作,并可以通过mRequestMapmThumbnailDownloadListener

For the reference: Java Memory Model . 供参考: Java内存模型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM