Android 从父线程调用子线程方法

Android 从父线程调用子线程方法,android,multithreading,Android,Multithreading,我已经从父线程(主线程/UI线程)启动了一个子线程。从这个子线程我开始另一个线程(孙子?)。现在,我希望能够将一个字符串从主线程传递到我的孙子线程,这样我就可以使用这个字符串调用孙子线程中的一个方法 由于我对Java和线程还不熟悉,这让我感到困惑。我看过消息处理程序等,但不知道这是不是一种方法,因为我找不到一个我理解的示例。这里是最简单的示例。使用setter和getter很方便 import android.os.Bundle; import android.util.Log; public

我已经从父线程(主线程/UI线程)启动了一个子线程。从这个子线程我开始另一个线程(孙子?)。现在,我希望能够将一个字符串从主线程传递到我的孙子线程,这样我就可以使用这个字符串调用孙子线程中的一个方法


由于我对Java和线程还不熟悉,这让我感到困惑。我看过消息处理程序等,但不知道这是不是一种方法,因为我找不到一个我理解的示例。

这里是最简单的示例。使用setter和getter很方便

import android.os.Bundle;
import android.util.Log;

public class MyExtendedThread extends Thread{

    private String mData;
    public MyExtendedThread(String dataStringIwantToPass){
        this.mData = dataStringIwantToPass;
    }
    @Override
    public void run() {
        //DO SOMETHING WITH mData
        //for example:
        while(true){
            if(mData != null)
                Log.i("mData: ", mData);
            Thread.sleep(2000);//sleep it few seconds :) 
        }
    }
    public String getData() {
        return mData;
    }
    public void setData(String mData) {
        this.mData = mData; //you might want to change the value at some point of time
    }
}
在这里,我们将线程类继承到自定义类,该类将具有setter和getter以及参数化构造函数。这很直截了当。我们将使用setter随时更改字符串的值

@Override
    public void onCreate(Bundle savedInstanceState ) {
        final String data = "this is string";
        Thread theThreadStartedFromUIthread = new Thread(new Runnable(){
            MyExtendedThread myOtherThread = new MyExtendedThread(data);
            @Override
            public void run() {
                // Do some stuff 
                myOtherThread.start();// the other (grandchild) thread has started
                //here i want to change the value of mData, assuming the thread is still running
                myOtherThread.setData("Alright, I've changed you.");
            }});

        };
    }

有帮助吗

在java中,我们通常不会也不能在线程之间传递值。我们在线程之间共享值和对象。在线程之间共享和传递值之间存在着非常细微的差别。如果您以某种方式将一个值传递给一个线程,那么该线程将拥有对该值的独占权限,即线程将拥有自己的值副本,我们不需要担心该变量的非同步代码或线程安全性。我们通常使用这种概念 在并发系统中使用消息传递。 请参阅:

但是在Java中,我们通常在多个线程之间共享值。线程之间没有关系,即没有子线程或孙子线程。只有deamon和非deamon线程()。 所以,如果您必须在grand child和主线程之间共享一些值。您必须创建一个可供他们使用/共享的对象。请参见以下示例:

    public class GranDChildThread {
        /**
         * Please note there are no relation ships among thread.  
         * Simply put, a thread is a program's path of execution.
         * All the three threads have access to shared String
         */
        public static String sharedString = new String("its a wonderfull life");

        public static void main(String[] args) {
            // this is my main Thread 
            System.out.println("Main Thread: I have access to 
                sharedString : " + sharedString);
            Thread childThread = new Thread(new Runnable() {

            @Override
            public void run() {
                // this is child thread
                System.out.println("Child Thread: 
I have access to sharedString : " + sharedString);
                Thread grandChildThread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // this grand Child 
                    System.out.println("Grand Child Thread: 
I have access to sharedString : " + sharedString);
                        }
                    });
                }
            });
        }
    }