uniquenamehere
uniquenamehere

Reputation: 1949

Calling a child thread method from parent thread

I have started a child thread from my parent thread (main/UI thread). And from this child thread I start another thread (grandchild?). Now I want to be able to pass a string from my main thread to my grandchild thread so I can call a method in the grandchild thread using this string.

As I am new to Java and threads this confuses me. I have looked at message handlers etc. but don't know if that is the way to go or not as I can't find an example that I understand.

Upvotes: 0

Views: 2486

Answers (2)

veritas
veritas

Reputation: 2444

Normally we don’t and cannot pass values among threads in java. We share values and objects among threads. There is a very subtle difference among sharing and passing values among threads. If You somehow pass a value to a Thread then that Thread will have exclusive right over it i.e thread will have its own copy of the value and we don’t need to worry about unsynchronized code or thread safety of that variable. We normally use this type of concept in concurrent system using message passing. Please see: https://en.wikipedia.org/wiki/Message_passing

But in Java We normally share values among multiple threads. And there is no relationships among threads i.e is there is no child or grand child threads. There are only deamon and non-deamon threads (http://www.javaworld.com/jw-04-1996/jw-04-threads.html). So if you have to share some value between grand child and main thread. you have to make a single Object which is available/share among them. Please see the below example :

    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);
                        }
                    });
                }
            });
        }
    }

Upvotes: 1

Nikola Despotoski
Nikola Despotoski

Reputation: 50538

Here is the simplest example. Using setters and getters can come handy.

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
    }
}

Here we inherit the Thread class to our custom class that will have setters and getters and argumented constructor. Thats pretty straight forward. We will use the setter to change the value of the string at any time we want.

@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.");
            }});

        };
    }

Is it helpful?

Upvotes: 1

Related Questions