Java creates a multithreaded approach

Source: Internet
Author: User

First, there are three main ways of using multithreading in Java:

First type: Inherit the thread class

Step: ① Define a class, inherit the thread class, and override the Run method of the Thead class, the contents of the Run method are the tasks that the thread will perform. The Run method is also referred to as the execution body.

② creates an instance of the Thead subclass, that is, the thread object is created.

③ uses the thread's Start method to start the thread.

The second type: Implementing the Runnable method

Step: ① Define a class, implement the Runnable interface, and override the Run method of the interface, the Run method body is still the execution body.

② creates an instance of the Thead class and passes an instance of the class that implements the Runnable interface into the constructor of the Thead class as a parameter.

③ calls the Start method of the thread class instance to launch the thread.

Third: Implementing the Callable Interface (Implementation class mates using the Future interface)

Step: ① Define a Class A, implement the callable interface, and override the call method of the interface, the call method body is still the execution body, the callmethod has a return value .

② creates instance B of Class A that implements the callable interface, and uses the implementation class of the future interface Futuretask the subclass wrapper B,futuretask object encapsulates the return value of the call method of B.

③ creates an instance of the Thead class and passes an instance of the Futuretask class into the constructor of the Thead class as a parameter.

④ calls the Start method of the thread class instance to launch the thread.

When ⑤ is required, the Get method of the instance of the Futuretask class is called to get the return value of the call method.

Ii. comparison of three ways to create threads

When creating multiple threads in a way that implements Runnable, callable interfaces, the advantages are:

The thread class simply implements the Runnable interface or callable interface and can inherit other classes (Java classes can implement multiple interfaces, but inherit only one parent).

In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to handle the same resource, so that the CPU, code and data can be separated to form a clear model, which is a good embodiment of object-oriented thinking.

Disadvantages are:

Programming is slightly more complex, and you must use the Thread.CurrentThread () method if you want to access the current thread.

The advantage of creating multiple threads with the inherited thread class is:

Writing is simple and if you need access to the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly using this.

Disadvantages are:

The thread class has inherited the thread class, so it is no longer possible to inherit other parent classes.

Iii. using thread pooling to execute threads

Many of the thread pool implementations are available in the new Java.util.concurrent package after jdk1.5:

You can use executors to create a line, simply create a thread object, put in the Execute method that calls Executorservice, and pass in the created thread object, without requiring the user to manually start the thread.

You can also increase the utilization of threads.

Java creates a multithreaded approach

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.