Java multithreaded programming three ways to pass data to a thread _java

Source: Internet
Author: User
Tags data structures integer numbers thread class

In the traditional synchronous development mode, when we call a function, we pass the data through the parameter of the function, and return the final calculation result through the return value of the function. However, in the asynchronous development mode of multithreading, there is a great difference between the data transferring and returning and the synchronous development mode. Because the running and ending of threads are unpredictable, it is not possible to return data as functions through function arguments and return statements when data is passed and returned. This article describes several ways to pass data to threads, and in the next article, describes methods for returning data from threads.

If you want to take it first, you must give it first. Typically, you need some initialization data when using a thread, and then the thread uses that data for processing and returns the result. The first thing to do in this process is to pass data to the thread.

I. Passing data through the construction method
When you create a thread, you must establish an instance of the thread class or its subclasses. Therefore, it is not difficult to think of passing data to a thread by constructing the thread class before calling the Start method. and save the incoming data using the class variable for use by the thread (which is actually used in the Run method). The following code shows how to pass data by constructing a method:

Copy Code code 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 ();
}
}

Since this method is to pass data at the same time as the thread object, the data is already in place before it is run, so that it does not result in the data being passed in after the threads are run. If you want to pass more complex data, you can use data structures such as collections, classes, and so on. Using a construction method to pass data is relatively safe, but if the data to be passed is relatively long, it can cause a lot of inconvenience. Because Java does not have default parameters, if you want to implement an effect similar to the default parameters, you have to use overloads, which not only complicate the construction method itself, but also increase the number of construction methods. Therefore, to avoid this situation, you have to pass the data through class methods or class variables.

Ii. passing data through variables and methods

Passing data to an object typically has two opportunities, the first opportunity is to pass the data through the construction method when the object is created, and another opportunity is to define a series of public methods or variables (also called fields) in the class. Then, after the object is established, the object instance is assigned one by one. The following code is a revision of the MyThread1 class, using a SetName method to set the name variable:

Copy Code code 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 ();
}
}

Passing data by callback function

The two methods discussed above for passing data to threads are most commonly used. Both of these methods, however, are actively passing data into the thread class in the Main method. This is what the thread says, is passively receiving the data. However, in some applications it is necessary to obtain data dynamically during a thread run, such as generating 3 random numbers in the Run method of the following code, and then using the work class's process method to find the three random numbers and return the results through the value of the data class. As you can see from this example, you must get three random numbers before returning value. In other words, this value cannot be passed into the thread class beforehand.

Copy Code code 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 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); Using Callback functions
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 code above is called a callback function. In essence, a callback function is an event function. Data interaction between the callback function and the program that invokes the API is often used in the Windows API. Therefore, the process of invoking a callback function is the most primitive procedure for raising an event. Invoking the Process method in this example to obtain data is equivalent to raising 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.