Three ways and differences of creating threads in Java

In Java, there are three methods to create threads:

1. Inherit thread class

If you inherit the thread class, you must override the run method and define the tasks to be executed in the run method.

After creating your own thread class, you can create a thread object, and then start the thread through the start () method. Note that instead of calling the run () method to start the thread, the run method only defines the tasks to be executed. If you call the run method, it is equivalent to executing the run method in the main thread, which is no different from ordinary method calls. At this time, a new thread will not be created to execute the defined tasks.

In the above code, a new thread will be created by calling the start () method. To distinguish between the start () method call and the run () method call, take the following example:

Operation results:

The following conclusions can be drawn from the output results:

1) The thread IDs of thread1 and thread2 are different, and thread2 and the main thread ID are the same, which means that calling the run method will not create a new thread, but directly run the run method in the main thread, which is no different from ordinary method calls;

2) Although the start method call of thread1 is called before the run method of thread2, the first output is the relevant information of the run method call of thread2, indicating that the process of creating a new thread will not block the subsequent execution of the main thread.

2. Implement runnable interface

In addition to inheriting the thread class, creating a thread in Java can also realize similar functions by implementing the runnable interface. Implementing the runnable interface must override its run method.

Here is an example:

Runnable means "task" in Chinese. As the name suggests, we define a subtask by implementing the runnable interface, and then hand over the subtask to the thread for execution. Note that this method must take runnable as the parameter of the thread class, and then create a new thread through the start method of the thread to execute the subtask. If the run method of runnable is called, no new thread will be created. This ordinary method call is no different.

In fact, if you look at the implementation source code of the thread class, you will find that the thread class implements the runnable interface.

In Java, these two methods can be used to create threads to execute subtasks. The specific choice depends on your own needs. Inheriting the thread class directly may look more concise than implementing the runnable interface. However, since Java only allows single inheritance, if a custom class needs to inherit other classes, it can only choose to implement the runnable interface.

3. Create threads using callable and future

Unlike the runnable interface, the callable interface provides a call () method as the thread executor, and the call () method is more powerful than the run () method.

The steps to create and start a thread with a return value are as follows:

Here is an example:

Comparison of three thread creation methods:

The methods of implementing runnable and callable interfaces are basically the same, but the latter has a return value when executing call() method, and the latter has no return value when executing run() method. Therefore, these two methods can be classified as one. The difference between this method and the method inheriting thread class is as follows:

PS: it is generally recommended to create multithreading by implementing interfaces

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
分享
二维码
< <上一篇
下一篇>>