Returning data from a thread is similar to passing data to a thread. You can also use class members and callback functions to return data. However, class members have some differences between returning data and passing data. Let's take a look at the differences between them.
1. return data through class variables and Methods
To use this method to return data, you must call the start method to obtain data through class variables or methods. Let's take a look at the result of the routine 2-13.
Package mythread;
Public class MyThread extends Thread
{
Private String value1;
Private String value2;
Public void run ()
{
Value1 = "return data through member variables ";
Value2 = "Returning data through the 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 may output the following results:
Value1: null
Value2: null
The running result is abnormal. In the run method, values of value1 and value2 have been assigned, but null is returned. The reason for this is that the value1 and value2 values are output immediately after the start method is called, and the run method has not executed the statements that assign values to value1 and value2. To avoid this situation, you must wait until the run method is executed to execute the codes of value1 and value2. Therefore, we can think of using the sleep method to delay the main thread. For example, we can add the following statement after thread. start:
Sleep (1000 );
In this way, the main thread can be delayed for 1 second before execution, but there is a problem, that is, how do we know how long the delay will take. In the run method in this example, there are only two value assignment statements and only one thread is created. Therefore, the delay of 1 second is sufficient. However, if the statements in the run method are complex, this time is difficult to predict, so this method is not stable.
The purpose is to obtain the values of value1 and value2. Therefore, we only need to determine whether value1 and value2 are null. If none of them are null, the two values can be output. We can use the following code to achieve this purpose:
While (thread. value1 = null | thread. value2 = null );
The preceding statement can be used to avoid this situation stably, but this method is too costly for system resources. You can imagine that if the code in the run method is complex, it takes a long time for value1 and value2 to be assigned values, so that the while LOOP must be executed continuously, until neither value1 nor value2 is null. Therefore, we can make the following improvements to the preceding statement:
While (thread. value1 = null | thread. value2 = null)
Sleep (100 );
In the while loop, the values of value1 and value2 are determined for the first time and then sleep for 100 milliseconds. In this way, the occupied system resources will be smaller.
Although the above method can be well solved, the Java thread model provides us with a better solution, which is the join method. As discussed earlier, the function of join is to use threads to change from Asynchronous execution to synchronous execution. When the thread changes to synchronous execution, it is no different from getting the returned data from common methods. Therefore, you can use the following code to solve this problem more effectively:
Thread. start ();
Thread. join ();
After thread. join () is executed, the thread's run method has exited, that is, the thread has ended. Therefore, after thread. join (), you can use any resources of the MyThread class with confidence to obtain the returned data.
2. return data through the callback function
In fact, this method has been introduced in "three methods for passing data to a thread. In the example of "three methods for transmitting data to a thread", the Work-class process method transmits the computing results to the thread, the process method also obtains three random numbers from the thread. Therefore, this method can either pass data to the thread or obtain data from the thread.