Java introduces three methods for passing parameters to multiple threads in detail, and java introduces three methods.

Source: Internet
Author: User
Tags integer numbers

Java introduces three methods for passing parameters to multiple threads in detail, and java introduces three methods.
In the traditional synchronous development mode, when we call a function, the data is transmitted through this function parameter, and the final calculation result is returned through the return value of this function. However, in the multi-thread asynchronous development mode, data transmission and return differ greatly from the synchronous development mode. Because the running and end of a thread are unpredictable, data cannot be returned through function parameters and return statements like a function during data transmission and return. This article introduces several methods for passing data to the thread for the above reasons. In the next article, we will introduce the methods for returning data from the thread.

If you want to obtain it first, you must give it first. Generally, some initialization data is required when a thread is used, and then the thread processes the data and returns the result. In this process, the first thing to do is to pass data to the thread.

1. Transmit data through Constructor
When creating a Thread, you must create an instance of the Thread class or its subclass. Therefore, it is not difficult to pass data into the thread through the construction method of the thread class before calling the start method. And saves the incoming data using class variables for the thread to use (actually used in the run method ). The following code demonstrates how to transmit data through constructor:
Copy codeThe Code is as follows:
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 transmits data while creating a thread object, the data is ready before the thread runs, this will not cause data to be passed in after the thread is running. To transmit more complex data, you can use a collection, class, and other data structures. Although it is safer to use constructor methods to transmit data, it may cause a lot of inconvenience if there are too many data to be transmitted. Since Java does not have default parameters, to achieve effects similar to default parameters, you must use overload. This not only makes the constructor itself too complex, but also increases the number of constructor methods. Therefore, to avoid this situation, you must pass data through class methods or class variables.

2. Transmit data through variables and Methods
There are usually two chances to pass data into an object. The first chance is to pass data through the constructor when an object is created, another opportunity is to define a series of public methods or variables (also called fields) in the class ). After the object is created, assign values one by one through the object instance. The following code is a revision of the MyThread1 class and uses a setName method to set the name variable:
Copy codeThe Code is as follows:
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 ();
}
}

3. pass data through the callback function
The two methods discussed above to pass data to a thread are the most common. However, both methods actively pass data into the Thread class in the main method. This means that the thread passively receives the data. However, in some applications, data needs to be dynamically obtained during thread running. For example, three random numbers are generated in the run method of the following code, calculate the sum of the three random numbers through the process method of the Work class, and return the result through the value of the Data class. From this example, we can see that three random numbers must be obtained before the value is returned. That is to say, this value cannot be passed into the Thread class in advance.
Copy codeThe Code is as follows:
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;
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. In essence, a callback function is an event function. In Windows APIs, callback functions and APIs are often used for data interaction. Therefore, the process of calling the callback function is the most primitive process of triggering events. In this example, the process method is called to obtain data, which is equivalent to triggering an event in the run method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.