-1

I have one doubt. IF we talk with respect to Java Multithreading, excluding concurrency framework, then how can we return a value from the run() method.

I was asked this question in some interview and was asked how to do this without using callable or concurrency framework ie how to tell main thread that Thread1 has completed some execution(run() completed) and you can update the UI.

EDIT: This is different from the question marked as duplicate, the current question asks about how will you design/code using runnable to return value. This is not possible as per the run() function but what is asked is how to achieve this behaviour via code for runnable so that main thread can be notified/updated?

I suppose these are different questions. Please correct me if I am wrong.

learner
  • 1,095
  • 2
  • 18
  • 41
  • 2
    Hint: run() is an instance method. Your thread doesn't merely call run(), it calls the run() method of _some object_. – Solomon Slow May 27 '14 at 19:12
  • Do you mean if we change the variables of this object to share data? But the problem is how to tell main thread that ThreadT1 has finished and data is now updated? – learner May 28 '14 at 06:25
  • The main thread can join() the other thread. If you have a thread, t, then t.join() will not return until the run() method in t returns. – Solomon Slow May 28 '14 at 12:55
  • @James, but I dont want to join the main thread. I want to notify the main thread that Event X has completed and you can now update the UI.( Consider it like onPostExecute() of android which runs after doInBackground() completes.). – learner May 31 '14 at 11:21
  • You said, "return a value from the run() method." Well, when a thread's run() method returns, the thread is done. I assumed that you were asking about a way to generalize the idea of method call and return across thread boundaries. There are many ways to communicate values back and forth between continuously _running_ threads. One of the most versatile, and most popular ways is to use a Queue (e.g., any class that implements java.util.Queue). – Solomon Slow Jun 02 '14 at 13:49

1 Answers1

0

One way to do that, is to set a value in your thread, wait for the thread to end and then get the value. Here is a simple, not very good coded example, but you can just execute it. Note the synchronized blocks which use the same object (thread).

public class Test { 

    private  int result;

    public static void main( String[] args) throws InterruptedException { 

        new Test().runTest();
    }

    private void runTest() throws InterruptedException {

        Thread thread = new Thread() {

            public void run() {

                System.out.println("Thread starts");

                try {
                    // some long operation in your thread
                    sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println("Thread ends");

                // set the value you want to "produce" with your thread
                setResult(42);

                // notify waiting threads
                synchronized (this) {
                    notify();
                }
            }
        };

        // start your thread
        thread.start();

        // wait for it
        synchronized (thread) {
            thread.wait();
        }

        // now you got your result
        System.out.println("result = " + result);
    }

    void setResult(int value) {
        this.result = value;
    } 
}
Thomas Uhrig
  • 30,811
  • 12
  • 60
  • 80
  • Hi Thomas, This will notify all the waiting thread but what about notifying the Main thread? I want Main thread to get notified after ThreadT1 completion. – learner May 28 '14 at 06:16
  • As you can See in my example, I only make one thread and wait for it in my Main thread. So it dies notify the Main thread. – Thomas Uhrig May 28 '14 at 06:30