This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

This article is from chaoCode. Please send a personal message and link to the author and the original address at the beginning of the article.

Violators, the author reserves the right to pursue.

preface

Concurrent programming in our daily development is in every moment of the day have to use, but most of the code underlying has helped us to do some safe disposal of concurrent programming, but there are still many cases need to go our own control, so we need to understand learning concurrent programming, step by step then we began to learn.

Know threads in Java

Java programs are inherently multithreaded

A Java program starts with the main() method and then follows the code logic. It looks like there are no other threads involved, but Java programs are inherently multithreaded because the main() method is executed by a thread named main.

[5] Attach Listener // Memory dump, thread dump, class information statistics, [4] Signal Dispatcher // Thread that sends signals to JVM [3] Finalizer // Thread that calls finalize method of object [2] Reference Handler// Clear Reference thread [1] main // Main thread, user program entry

How Java threads are created

Thread source code has a description of Java Thread creation, there are two.

1. Inherit the Thread class

2. Implement the Runable interface

There is also a third way to create threads by implementing Callable and Future interfaces.

In the Thread class source code, gives the description and Thread creation method example:

Thread is the only abstraction of threads in Java. Runable, Callable, and Future are abstractions of tasks (business logic). As a result, there is talk of multiple ways to create threads.

1. The Thread

  • [1] Define a subclass of Thread and override the run() method of that class. The run() method is called the Thread execution body.
  • [2] Create an instance of the subclass Thread, that is, create a Thread object.
  • [3] Start the thread by calling the start() method of the thread object.

Thread provides a constructor that takes a parameter. By implementing the Runnable interface and borrowing this constructor, we can obtain Thread objects based on our custom tasks

public Thread(Runnable target) {
    init(null, target, "Thread-" + nextThreadNum(), 0);
}
Copy the code

  • [1] Define the implementation class of the Runnable interface and overwrite the run() method of the interface.
  • [2] Create an instance of the Runnable implementation class and use that instance as the target of the Thread to create a Thread object. This Thread object is the actual Thread object.
  • [3] Start the thread by calling the start() method on the thread object.

3. Use Callable and Future

Using the Thread(Runnable Target) constructor, we implement a task with a return value by implementing the Callable interface, and then wrap the Callable object with the FutureTask class. The FutureTask class implements the RunnableFuture interface, but the RunnableFuture interface inherits from the Runnable interface, so we can use this constructor to get the Thread object for our custom task.

  • [1] Create an implementation class for the Callable interface and implement the call() method, which acts as the body of the thread execution and which returns a value.
  • [2] Create an instance of the Callable implementation class and use the FutureTask class to wrap the Callable object. This FutureTask encapsulates the return value of the Callable object’s call() method.
  • [3] Create a Thread object using the Future Task object as the target of the Thread object.
  • [4] Start the thread by calling the start() method on the thread object.
  • [5] Call the Get () method on the Future Task to get the return value when the child thread finishes executing.

Compare the three ways to create a thread

The Runnable interface is implemented in much the same way as the Callable interface, except that the Callable interface defines methods that have a return value and can declare that an exception is thrown. The main difference between this approach and the Thread approach is to create multiple threads by implementing Runnable and Callable interfaces:

The thread class only implements the Runnable or Callable interfaces. It can also implement other interfaces. It can also inherit from other classes. 2. Multiple threads can share the same Runnable object, which is ideal for multiple threads working on the same resource (object-oriented). To access the currentThread, you must use thread.currentthread (), which is more complicated.

Create a Thread by inheriting the Thread class:

The Thread class inherits from the Thread class, so it cannot inherit from any other parent class. 2, if you want to access the current thread, use this directly.

From the above analysis we found that the essence of creating threads is as written in the source code there are two ways. One is to inherit from Thread, and the other is to implement the Runnable interface.

By comparison, we find that creating threads through interfaces will make our code more fault-tolerant, more flexible and more flexible, and also reduce the coupling degree between programs. After all, the central idea of the dependency inversion principle among the six principles is interface oriented programming. Therefore, it is recommended to create the thread by implementing the interface.

Thank you for watching, and feel free to let us know in the comments section if you have any mistakes. If this post has helped you, feel free to like it at 👍.