The three Java methods for passing parameters to multiple threads are described in detail

  • 2020-04-01 01:10:38
  • OfStack

In the traditional synchronous development mode, when we call a function, the data is passed in through the parameters of the function, and the final calculation is returned through the return value of the function. However, in the multi-threaded asynchronous development mode, the transfer and return of data is quite different from the synchronous development mode. Because threads run and end unpredictably, you cannot return data through function arguments and return statements like functions when passing and returning data. This article has introduced several methods for passing data to a thread for the above reasons, and the next article will cover methods for returning data from a thread.

He who would take it first must give it first. Generally, when using a thread, it is necessary to have some initialization data, and then the thread USES this data for processing and returns the result. The first thing to do in this process is pass the data to the thread.

1. Pass the data through the construction method
When you create a Thread, you must create an instance of the Thread class or its subclasses. Therefore, it is not difficult to imagine passing data into the thread through the thread class constructor before calling the start method. The incoming data is saved using class variables for the thread to use (which is essentially used in the run method). The following code demonstrates how to pass data through a constructor:
 
package mythread; 
public class MyThread1 extends Thread 
{ 
private String name; 
public MyThread1(String name) 
{ 
this.name = name; 
} 
public void run() 
{ 
System.out.println("hello " + name); 
} 
public static void main(String[] args) 
{ 
Thread thread = new MyThread1("world"); 
thread.start(); 
} 
} 

Because this method passes the data along with the creation of the thread object, the data is in place before the thread runs, so that the data is not passed in after the thread runs. If you want to pass more complex data, you can use data structures such as collections, classes, and so on. While it is safe to use constructors to pass data, it can cause a lot of inconvenience if you have a lot of data to pass. Since Java has no default parameters, overloading is required to achieve the same effect as default parameters, which not only makes the constructors themselves too complex, but also increases the number of constructors. Therefore, to avoid this situation, you have to pass data through class methods or class variables.

Passing data through variables and methods
There are usually two chances to pass data into an object. The first is to pass the data in through the constructor when the object is created, and the second is to define a series of public methods or variables (also known as fields) in the class. Then, after the objects are created, they are assigned one by one through the object instances. The following code is a revision of the MyThread1 class, using a setName method to set the name variable:
 
package mythread; 
public class MyThread2 implements Runnable 
{ 
private String name; 
public void setName(String name) 
{ 
this.name = name; 
} 
public void run() 
{ 
System.out.println("hello " + name); 
} 
public static void main(String[] args) 
{ 
MyThread2 myThread = new MyThread2(); 
myThread.setName("world"); 
Thread thread = new Thread(myThread); 
thread.start(); 
} 
} 

Data is passed through the callback function
The two methods discussed above for passing data to a thread are the most common. But both methods are actively passing data into the thread class from the main method. This is passively received by the thread. However, in some applications, Data needs to be acquired dynamically while the thread is running. For example, three random Numbers are generated in the run method of the following code, and then the sum of these three random Numbers is calculated by the process method of the Work class, and the result is returned by the value of the Data class. As you can see from this example, you must get three random Numbers before returning value. That is, the value cannot be passed into the thread class in advance.
 
package mythread; 
class Data 
{ 
public int value = 0; 
} 
class Work 
{ 
public void process(Data data, Integer numbers) 
{ 
for (int n : numbers) 
{ 
data.value += n; 
} 
} 
} 
public class MyThread3 extends Thread 
{ 
private Work work; 
public MyThread3(Work work) 
{ 
this.work = work; 
} 
public void run() 
{ 
java.util.Random random = new java.util.Random(); 
Data data = new Data(); 
int n1 = random.nextInt(1000); 
int n2 = random.nextInt(2000); 
int n3 = random.nextInt(3000); 
work.process(data, n1, n2, n3); //Use the callback function
System.out.println(String.valueOf(n1) + "+" + String.valueOf(n2) + "+" 
+ String.valueOf(n3) + "=" + data.value); 
} 
public static void main(String[] args) 
{ 
Thread thread = new MyThread3(new Work()); 
thread.start(); 
} 
} 

The process method in the above code is called a callback function. Essentially, a callback function is an event function. Callback functions are often used in Windows apis for data interaction with programs that invoke the API. Therefore, the process of calling the callback function is the original process of raising the event. Calling the process method to get the data in this example is equivalent to raising an event in the run method.

Related articles: