Multithreading
1. A thread is an execution path in a process. A process is a program that is running in the system. (Access is a static concept, and threading is a dynamic concept.) )
2. How to open a new thread:
A, inherit the thread class, and override the Run method in thread.
b, implement the Runnable interface.
Note: Opening a new thread is by calling the Start method, and calling the Run method directly is only a generic method call.
3, the transition of the thread state:
4, the basic method of threading control
IsAlive () Determines whether the thread is still alive, that is, whether the thread has not been terminated.
GetPriority () Gets the priority number of the thread.
SetPriority () Sets the priority number of the thread.
Thread.Sleep () See the current thread sleep for a specified number of milliseconds.
Join () calls the method of a thread, merges the current thread with the thread, waits for the thread to end, and then replies to the current thread's run.
Yield () yields the CPU, and the current thread enters the ready queue for scheduling. (Let only once)
Wait () but its thread enters the object's wait pool.
Notify ()/notifyall () wakes up one/all waiting threads in the wait pool of the object.
Note: The priority of a thread is a range of 1 to 10, and the default is 5.
5, thread synchronization is to prevent multiple threads from accessing the same data object, the data damage.
Ways to implement thread synchronization:
A, code block lock:
Synchronized (this) {...}
B, Method Lock:
Public synchronised void XXX (...) {......}
6, Thread deadlock: a rare, and difficult to debug error, when two thread objects have a circular dependency on two synchronization objects, there will be a deadlock phenomenon.