1. Inherit the thread class and rewrite the Run method
A thread is a thread of execution in a program. A Java virtual machine allows an application to run multiple execution threads concurrently.
Each thread has a priority, and the execution of the high-priority thread takes precedence over the low-priority thread. Each thread can or cannot be marked as a daemon. When code running in a thread creates a new Thread
object, the initial priority of the new thread is set to the priority of the thread being created, and the new thread is the daemon only if the creation thread is the daemon thread.
2. Implement the Runnable interface and implement the run () method
Runnable
Interfaces should be implemented by classes that intend to execute their instances through a thread. The class must define a run
parameterless method called.
This interface is designed to provide a common protocol for objects that want to execute code at the time of the activity. For example, the Thread
class implements the Runnable
. Activation means that a thread has started and has not stopped.
Additionally, Runnable
classes that are not Thread
subclasses are provided with an activation method. By instantiating an Thread
instance and running itself as a target, you can run the implemented Runnable
class without having to create Thread
subclasses. In most cases, you should use an interface if you only want to override the run()
method without overriding other Thread
methods Runnable
. This is important because unless the programmer intends to modify or enhance the basic behavior of the class, you should not create subclasses for that class.
3. Implement the callable interface and implement the call () method
A task that returns the result and may throw an exception. The implementing person defines a method called call without any parameters .
The callable interface is similar in Runnable
that both are designed for classes whose instances might be executed by another thread. However, Runnable does not return a result and cannot throw a checked exception.
Inheriting the features of the thread class implementation
Single-inheritance reason, because threads have inherited the thread class, so they can no longer inherit other classes;
Because each thread is an instance of the thread subclass, it is possible to separate the execution flow code of multiple lines from the business data;
If you need access to the current thread, use this directly.
Implement Runnable interface and callable interface features
The thread class simply implements the Runnable interface or callable interface, and can inherit other classes and implement other interfaces.
Resource sharing, in this way, multiple threads can share a target object, so it is well suited for multiple threads to handle the same resource situation.
If you need access to the current thread, you need to use the Thread.CurrentThread method.
Compared to the Runnable interface, the callable interface is just the callable Interface call () method that can return a value and declare a throw exception, which is implemented by the call () method, which implements the run () method.
Multithreading (thread, Runnable, callable)