Two methods of returning data from threads in Java multithreaded programming

  • 2020-04-01 02:50:14
  • OfStack

Returns data through class variables and methods

Using this method to return data requires a call to the start method to get the data through a class variable or method. Let's first see what the routine 2-13 gives us.


package mythread;
public class MyThread extends Thread
{
    private String value1;
    private String value2;
    public void run()
    {
        value1 = " Returns data through a member variable ";
        value2 = " Returns data through a member method ";
    }
    public static void main(String[] args) throws Exception
    {
        MyThread thread = new MyThread();
        thread.start();
        System.out.println("value1:" + thread.value1);
        System.out.println("value2:" + thread.value2);
    }
}


Running the above code might produce the following results:

value1:null
value2:null

The results from the above operation look very abnormal. Value1 and value2 have been assigned values in the run method, and null is returned. This happens because the values of value1 and value2 are printed immediately after the start method is called, and the run method has not yet executed into the statement assigned to value1 and value2. To avoid this, wait for the run method to complete before executing the code that outputs value1 and value2. Therefore, we can think of using the sleep method to delay the main thread, such as adding the following statement after thread.start() :
Sleep (1000);
This can delay the main thread by one second before it executes, but one problem with this is how do we know how long to delay. There are only two assignment statements in the run method in this example, and only one thread is created, so a delay of 1 second is sufficient, but if the statements in the run method are complex, the time is difficult to predict, and therefore the method is unstable.
Our goal is to get the values of value1 and value2, so we just need to determine whether value1 and value2 are null. If neither of them is null, you can output both values. We can do this using the following code:


while (thread.value1 == null || thread.value2 == null);

Using the statements above can stably prevent this from happening, but this approach is too resource-intensive. You can imagine that if the code in the run method is complex and value1 and value2 take a long time to be assigned, then the while loop must continue until neither value1 nor value2 is empty. Therefore, we can make the following improvements to the above statement:


while (thread.value1 == null || thread.value2 == null)
    sleep(100);

After the first value1 and value2 values are judged in the while loop, sleep for 100 milliseconds, and then judge the values again. This takes up less system resources.
The above approach works fine, but Java's threading model provides a better solution, the join method. As discussed earlier, the function of a join is to use threads to move from asynchronous to synchronous execution. When a thread becomes synchronized, it is no different from getting the returned data from a normal method. Therefore, the following code can be used to solve this problem more effectively:


thread.start();
thread.join();

After thread.join() completes, the thread's run method exits, meaning that the thread has ended. Therefore, you can safely use any resource of the MyThread class to get the returned data after thread.join().

Return data via callback function
This approach is covered in the three ways to pass data to a thread. In the example of "three ways to pass data to a thread", the calculation results are passed to a thread through the process method of the Work class, but at the same time, three random Numbers are obtained from a thread through the process method. Therefore, this method can either pass data to or get data from a thread.


Related articles: