The previous blog introduced three ways to create threads, so what are the different places and the same places in the three ways? Today we'll talk a little bit about the difference between these three ways.
First, multiple threads can be implemented by inheriting the thread class or implementing runnable, callable interfaces, but implementing the Runnable interface is essentially the same as implementing the callable interface, except that the method defined in the callable interface has a return value. You can declare an exception to be thrown. Therefore, the implementation of runnable interface and implementation of the callable interface can be classified as a way. The main differences between this approach and the inheritance thread approach are as follows:
1 advantages and disadvantages of creating multithreading by implementing runnable and callable interfaces
The thread class only implements the Runnable interface or the callable interface, and can also inherit other classes.
In this way, multiple threads can share the same target object, so it is ideal for multiple threads of the same thread to handle the same resource's application scenario, thus separating the CPU, code, and data to form a clear model that better embodies the object-oriented idea.
The bad part: programming is slightly more complex, and if you need to access the current thread, you must use the Thread.CurrentThread () method.
2 advantages and disadvantages of creating multithreading in a way that inherits the thread class:
The advantage is that writing is simple, if you need to access the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread using this directly.
The disadvantage is that because the thread class has inherited the thread class, it is no longer possible to inherit other parent classes, which cannot be extended and enhanced. In general, multithreading program development is commonly used to implement runnable and callable interfaces.