Detailed explanation of java thread (thread, runnable, callable) examples

Java thread vs. thread, runnable, callable

Java uses thread class to represent threads. All field objects must be instances of thread class or its subclasses. The function of each thread is to complete a certain task. In fact, it is to execute a program flow. Java uses thread executors to represent this program flow.

1. Inherit thread class to create thread

The steps to start multithreading are as follows:

(1) Define the subclass of thread class and override the run () method of this class. The method body of the run () method represents the tasks that class threads need to complete. Therefore, the run () method is called the thread executor. (2) Create an instance of thread subclass, that is, create a thread object. (3) call the star () method of the thread to start the thread.

Relevant codes are as follows:

Operation results:

2. Implement the runnable interface to create threads

Note: public class thread implements runnable

(1) Define the implementation class of the runnable interface and override the run () method of the interface. The method body of the run () method is also the thread execution body of the thread. (2) Create an instance of the runnable instance class. This instance is used as the target of the thread to create the thread object, which is the real object.

Relevant codes are as follows:

Operation results:

@H_ 502_ 55@

3. Create threads using callable and future

Starting from Java 5, Java provides the callable interface, which is an enhanced version of runnable. The callable class provides a call () method that can be used as thread executor, but the call () method is more powerful.

(1) The call () method can have a return value. (2) the call () method can declare to throw an exception

Therefore, we can provide a callable object as the target of the thread, and the execution body of the thread is the call () method of the callable object. At the same time, Java 5 provides a future interface to represent the return value of the call () method in the callable interface, and provides an implementation class of futuretask, which implements the future interface and the runnable interface, which can be used as the target of the thread class

The startup steps are as follows:

(1) Create the implementation class of the callable interface and implement the call () method. The call () method will be used as the execution body of the thread, and the call () method has a return value. (2) Create an instance of the callable implementation class, and use the futuretask class to wrap the callable object, which encapsulates the return value of the call () method. (3) Use futuretask object as the target of thread object to create and start a new thread. (4) call get () method of futuretask object to get the return value after the execution of child thread.

Relevant codes are as follows:

Operation results:

4. Comparison of three methods

Create multiple threads by inheriting the thread class

Disadvantages: thread class has been inherited and cannot inherit other parent classes.

Advantages: easy to write

Multithreading is created by inheriting runnable and callable interfaces

Disadvantages: programming is a little complicated. If you need to access the current thread, you must use thread currentThread()

Advantages:

(1) It can also inherit other classes. (2) multiple threads can share a target object, so it is very suitable for multiple same threads to process the same resource, so as to separate CPU, code and data, form a clear model, and better reflect the idea of class object-oriented.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>