Summary of several implementation methods of Java multithreading
Summary of several implementation methods of Java multithreading
1. How many ways to implement multithreading? How to implement synchronization?
There are two ways to implement multithreading: inheriting the thread class and implementing the runnable interface @ H_ 404_ 9@
There are two ways to realize synchronization: synchronized, wait and notify@H_404_9 @
Wait (): put a thread in a waiting state and release the lock of the held object@ H_ 404_ 9 @ sleep(): it is a static method to make a running thread sleep. Call this method to catch the interruptedexception exception@ H_ 404_ 9 @ notify(): wake up a thread in a waiting state. Note that when this method is called, it cannot wake up a thread in a waiting state exactly, but the JVM determines which thread to wake up, not by priority@ H_ 404_ 9@
Allnotity(): wake up all the threads in the waiting state. Note that instead of giving all the wake-up threads an object lock, they are allowed to compete@ H_ 404_ 9@
2. Whether to start a thread with run() or start()
Starting a thread is to call the start () method to make the thread ready and can be scheduled to run in the future. A thread must be associated with some specific execution code, and the run () method is the execution code associated with the thread@ H_ 404_ 9@
3. When a thread enters a synchronized method of an object, can other threads enter other methods of the object?
In several cases: @ h_ 404_ 9@
1. Whether the synchronized keyword is added before other methods. If not, it can be used@ H_ 404_ 9@ 2. If wait is called inside this method, you can enter other synchronized methods@ H_ 404_ 9@ 3. If the synchronized keyword is added to other methods and wait is not called internally, it cannot be used@ H_ 404_ 9@ 4. If the other method is static, its synchronization lock is the bytecode of the current class. It cannot be synchronized with the non static method because the non static method uses this@ H_ 404_ 9@
4. The basic concept of thread, the basic state of thread and the relationship between states
A program can have multiple execution threads executing at the same time. A thread is an execution thread in the program. Each thread is associated with code to be executed, that is, multiple pieces of program code can run at the same time. Each program has at least one thread, that is, the thread executed by the main method. If it's just a CPU, how can it execute multiple programs at the same time? From a macro point of view, the CPU will execute a clue and B clue. The switching time is very fast. It gives the impression that a and B are executing at the same time. For example, when you surf the Internet in the same office, there is only one link to the external network cable. In fact, this network cable will transmit data for a and data for B. because the switching time is very short, so, Everyone feels that they are surfing the Internet at the same time@ H_ 404_ 9@
Status: ready, running, synchronize blocked, wait and sleep suspended, end. Wait must be called inside synchronized@ H_ 404_ 9@
After calling the start method of the thread, the thread enters the ready state. The thread scheduling system turns the thread in the ready state into the running state. When it encounters the synchronized statement, it changes from the running state to blocking. When the synchronized obtains the lock, it changes from blocking to running. In this case, you can call the wait method to change to the suspended state. When the code associated with the thread is executed, The thread changes to the end state@ H_ 404_ 9@
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!