1. Know thread and runnable
There are two ways to implement multithreading in Java: Inherit the Thread class or implement the runnable interface. Runnable is an interface. We recommend that you use the interface to generate a thread because the interface can implement multi-inheritance. Besides, runnable has only one run method, which is suitable for inheritance. When using a thread, you only need to inherit the thread and create a new instance. by calling the START () method, you can start a thread.
Thread test = new thread ();
Test. Start ();
When using runnable, you must first create a new runnable instance, and then start the thread.
Test impelements runnable;
Test T = new test ();
Thread test = new thread (t );
Test. Start ();
Conclusion: thread and runnable are two ways to implement Java multithreading. runable is an interface and thread is a class. We recommend that you use runable to implement Java multithreading. start () to make the thread run.
2. Know the start and run of thread.
1) start:
Use the start method to start the thread and realize multi-thread running. In this case, you do not need to wait for the run method body.CodeAfter the execution is complete, execute the following code directly. Start a thread by calling the START () method of the thread class. The thread is in the ready (runable) State and is not running. Once the SPU time slice is obtained, run () is started () the run () method is called the thread body. It contains the content of the thread to be executed. The run method ends and the thread ends immediately.
2) run:
The run () method is only a common method of the class. If you call the run method directly,ProgramThere is still only one main thread, and there is only one program execution path. It must be executed in sequence, or the following code can be executed after the run method is completed, in this way, the write thread is not achieved.
Conclusion: only the start method can be called to start the thread. The run method is only a common method call of the thread and is still executed in the main thread.
3. thread status description
The thread state can be divided into seven States, namely the initial state, the runable state, the unrunable state, and the extinction state, which are described as follows:
1) There are two ways to implement the thread. One is to inherit the Thread class and the other is to implement the runnable interface. However, when we create a new thread instance, the thread enters the initial state;
2) When the object calls the START () method, it enters the running state;
3) after the instance enters the "runnable" status, when the instance is selected by the operating system, the instance enters the running status after obtaining the CPU time slice;
4) There are many cases after entering the running status, which are roughly as follows:
. After the run () method or 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 resources occupied ). When sleep () ends or join () ends, the thread enters the runable state and continues waiting for the OS to allocate time slices;
. When the thread enters the runable status (note that it is not running yet) and the resource to be called is locked (synchroniza, lock), it will immediately enter the lock pool status, wait for the lock mark to be obtained (other threads may already be waiting for the lock mark to be obtained in the lock pool, and they are in the queue status, first come, first served). Once the thread obtains the lock mark, it is transferred to the operational status, waiting for the OS to allocate CPU time slices;
. When the thread calls the wait () method, it enters the waiting queue (entering this status will release all the resources occupied, different from the blocking Status). After entering this status, automatic wake-up is not allowed. You must rely on other threads to call the Y () or notifyall () method to wake up (because notify () is only to wake up a thread, however, we cannot determine which thread to wake up. The thread we need to wake up may not be able to wake up. Therefore, we generally use the yyall () method in actual use, when a thread is awakened, it enters the lock pool and waits for the lock mark to be obtained.
. When the thread calls the stop method, the thread enters the extinction State. However, the stop method is not safe and is not encouraged, you can use the conditions in the run method to change the thread stop.