Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the two ways to return data from a thread in Java multithreading

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the two methods of returning data from threads in Java multithreading? I believe many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Returning data from a thread is similar to passing data to a thread. You can also return data through class members and callback functions. But there are some differences between returning and passing data among class members, so let's take a look at the differences.

First, return data through class variables and methods

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

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 methods";} 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 is likely to output the following result:

Value1:null

Value2:null

From the above operation results, it is very abnormal. Value1 and value2 have been assigned values in the run method, but null is returned. The reason for this is that the values of value1 and value2 are output immediately after calling the start method, where the run method has not yet executed the statements that assign values to value1 and value2. To avoid this, you need to wait until the run method has been 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 so can delay the execution of the main thread by 1 second, but there is a problem with how we know how long the delay will be. In the run method of this example, there are only two assignment statements, 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, so this method is unstable.

Our goal is to get the values of value1 and value2, so just determine whether value1 and value2 are null. If neither of them is null, you can output these two values. We can use the following code to do this:

While (thread.value1 = = null | | thread.value2 = = null)

Using the above statement can stably avoid this situation, but this approach consumes too much 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, so the while loop must be executed until both value1 and value2 are not empty. Therefore, we can make the following improvements to the above statement:

While (thread.value1 = = null | | thread.value2 = = null) sleep

Sleep for 100 milliseconds after determining the values of value1 and value2 for the first time in the while loop, and then determine the two values. This will take up less system resources.

Although the above method can be solved well, Java's threading 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 a thread becomes synchronous, 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 the execution of thread.join (), the run method of the thread thread has exited, which means that the thread thread has ended. Therefore, after thread.join (), you can safely use any resource of the MyThread class to get the returned data.

Second, return data through callback function

The result of the calculation is passed to the thread through the process method of the Work class, but at the same time, three random numbers are obtained from the thread through the process method. Therefore, this method can not only pass data to the thread, but also obtain data from the thread.

After reading the above, have you mastered what are the two ways to return data from a thread in Java multithreading? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report