1. Return data through class variables and methods
Using this method to return data requires calling the start method before the data can be obtained through class variables or methods. Let's first see what results we get from Example 2-13.
Copy the code code as follows:
package mythread;
public class MyThread extends Thread
{
private String value1;
private String value2;
public void run()
{
value1 = "Return data through member variables";
value2 = "Return data through 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:
Copy the code code as follows:
value1:null
value2:null
The above running results look very abnormal. Values have been assigned to value1 and value2 in the run method, but null is returned. The reason why this happens is that the values of value1 and value2 are output immediately after calling the start method, and the run method here has not yet executed the statements that assign values to value1 and value2. To avoid this situation, you need to wait until the run method is executed before executing the code that outputs 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);
Doing this can cause the main thread to delay execution for 1 second before continuing, but there is a problem with this, that is, how do we know how long to delay. In this example, there are only two assignment statements in the run method, and only one thread is created. Therefore, a delay of 1 second is enough, but if the statements in the run method are complex, this time is difficult to predict. Therefore, this method Not stable.
Our purpose is to get the values of value1 and value2, so we only need to determine whether value1 and value2 are null. If neither of them is null, these two values can be output. We can use the following code to achieve this purpose:
Copy the code code as follows:
while (thread.value1 == null || thread.value2 == null);
Using the above statement can avoid this situation very stably, but this method consumes too many system resources. You can imagine that if the code in the run method is very complex, value1 and value2 will take a long time to be assigned, so the while loop must continue to be executed until both value1 and value2 are not empty. Therefore, we can make the following improvements to the above statement:
Copy the code code as follows:
while (thread.value1 == null || thread.value2 == null)
sleep(100);
In the while loop, after judging the values of value1 and value2 for the first time, it sleeps for 100 milliseconds, and then judges these two values. This will occupy less system resources.
Although the above method can be solved very well, Java's threading model provides us with a better solution, which is the join method. As discussed before, the function of join is to use threads to change from asynchronous execution to synchronous execution. When the thread becomes synchronously executed, it is no different from getting the return data from an ordinary method. Therefore, this problem can be solved more efficiently using code like the following:
Copy the code code as follows:
thread.start();
thread.join();
After thread.join() is executed, the run method of the thread thread has exited, which means that the thread thread has ended. Therefore, you can safely use any resource of the MyThread class after thread.join() to get the returned data.
2. Return data through callback function
In fact, this method has been introduced in "Three Methods of Passing Data to Threads". In the example of the article "Three Methods of Passing Data to Threads", the calculation results are passed to the thread through the process method of the Work class, but at the same time, three random numbers are also obtained from the thread through the process method. Therefore, this method can both pass data to and obtain data from the thread.