Three construction methods of Java multithreading

Three construction methods of Java multithreading

Inherit thread class to create thread class

Thread class already inherits object

The object class creates the name option and has its getname() setname() method

When used in a class that inherits thread, you only need to use this reference

The above two side threads and the main thread switch randomly, and because the class inheriting thread is used, the two side threads cannot share resources

After the start () method is called, it does not execute the multithreaded code immediately, but makes the thread programmable. When to run is determined by the operating system

Implement the runnable interface to create a thread class

The above result is that the two secondary threads and the main thread switch randomly, but they do not share resources because they have no resources to share at all.

After the start () method is called, it does not immediately execute the multithreaded code, but makes the thread programmable. When to run is determined by the operating system. Details of inheriting the thread class and creating shared resources of the runnable interface

When there are only resources that can be shared, that is, the same instantiated object is used. The two creation methods differ only when sharing resources. Otherwise, they will not share resources. Shared resources are usually decorated with the private static modifier.

Because of private int count = 5; In a word, there are shared resources, but this is a subclass of the thread class and cannot share resources

Similarly, it is precisely because of the common instantiation object private int count = 15 that classes implementing runnable can share resources

So why are subclasses that inherit the thread class and classes that implement the runable interface different when sharing resources?

Because Java can only support single inheritance, the single inheritance feature means that there can only be one subclass to inherit, and the runnabl interface can share a resource with many classes, so that multiple threads can operate

Creating threads using callable and future

Callable looks like an enhanced version of the runnable interface. Callable has a call () method, which is equivalent to the run () method of runnable, but its function is more powerful:

The call () method can have a return value, and the call () method can declare to throw an exception

The callable interface has generic restrictions. The generic parameter type in the callable interface is the same as the return value type of the call () method. Moreover, the callable interface is a functional interface, so you can use lambda expression to create callable objects. The runnable interface is also a functional interface, so you can also use lambda expression to create runnable objects

Threads created using callable and future of lambda expressions

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can 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
分享
二维码
< <上一篇
下一篇>>