Joke Collection Website - Blessing messages - What are the implementation methods of multi-threading in Java and how to use it~~~~~~~~~~~~~~~~~Urgent

What are the implementation methods of multi-threading in Java and how to use it~~~~~~~~~~~~~~~~~Urgent

1. Understand Thread and Runnable

There are two ways to implement multi-threading in Java: inherit the Thread class or implement the Runnable interface. Runnable is an interface. It is recommended to use interfaces to generate threads, because interfaces can implement multiple inheritance, and Runnable has only one run method, which is very suitable for inheritance. When using Thread, you only need to inherit Thread, create a new instance, and call the start() method to start a thread.

Thread Test = new Thread();

Test.start();

When using Runnable, you need to create a new instance that implements Runnable first, and then Just start Thread.

Test impelements Runnable;

Test t = new Test();

Thread test = new Thread(t);

test .start();

Summary: Thread and Runnable are two ways to implement Java multi-threading. Runable is an interface and thread is a class. It is recommended to use runable to implement Java multi-threading. No matter what, you will eventually need to pass thread.start() to make the thread in a runnable state.

2. Understand the start and run of Thread

1) start:

Use the start method to start the thread, truly realizing multi-thread operation. At this time, no need Wait for the run method body code to be executed and continue to execute the following code directly. Start a thread by calling the start() method of the Thread class. At this time, the thread is in the ready (runnable) state and is not running. Once the SPU time slice is obtained, the run() method starts to be executed. The method run() here is called It is the thread body, which contains the content of the thread to be executed. When the Run method ends, the thread terminates immediately.

2) run:

The run() method is just an ordinary method of the class. If the Run method is called directly, there is still only one thread in the program, the main thread, and its program execution path There is still only one, and it still has to be executed sequentially, or it still has to wait for the run method body to be executed before continuing to execute the following code, so the purpose of writing threads is not achieved.

Summary: Call the start method to start the thread, and the run method is just a normal method call of thread, which is still executed in the main thread.

3. Thread status description

From a large perspective, the thread status can be summarized as: initial state, runnable state, non-runnable state and death state, which can be subdivided. The 7 states shown in the figure above are explained as follows:

1) There are two ways to implement threads. One is to inherit the Thread class, and the other is to implement the Runnable interface. But no matter what, when we create a new thread After the instance, the thread enters the initial state;

2) When the object calls the start() method, it enters the runnable state;

3) After entering the runnable state, When the object is selected by the operating system and obtains the CPU time slice, it will enter the running state;

4) After entering the running state, there will be more cases, roughly as follows:

·run After the () method or the main() method ends, the thread enters the termination state;

·When the thread calls its own sleep() method or the join() method of other threads, it will enter the blocking state ( This state stops the current thread but does not release the occupied resources). When sleep() ends or join() ends, the thread enters the runnable state and continues to wait for the OS to allocate a time slice;

·When the thread just enters the runnable state (note that it is not running yet), it is found that The resource to be called is locked (synchroniza, lock) and will immediately enter the lock pool state, waiting to obtain the lock mark (there may already be other threads in the lock pool waiting to obtain the lock mark, and they are in the queue state at this time) , first come first served), once the thread obtains the lock mark, it will enter the runnable state and wait for the OS to allocate the CPU time slice;

·When the thread calls the wait() method, it will enter the waiting queue ( Entering this state will release all the resources occupied, which is different from the blocking state). After entering this state, it cannot be awakened automatically. It must rely on other threads to call the notify() or notifyAll() method to be awakened (because notify() is only Wake up a thread, but we cannot be sure which thread is woken up. Maybe the thread we need to wake up cannot be woken up. Therefore, in actual use, the notifyAll() method is generally used to wake up all threads). The thread is After waking up, it will enter the lock pool and wait to obtain the lock mark.

·When the thread calls the stop method, the thread can enter the death state. However, because the stop method is unsafe, its use is discouraged. You can stop the thread by changing the conditions in the run method.