Priority of Threads
The priority value is 1-10 (the higher the value, the higher the priority).
Public final int getpriority (); Gets the value of the thread priority.
Public final void setpriority (int newpriority), and modify the priority of the thread.
Note: High priority does not necessarily mean that the thread must run first, it can only represent the likelihood that the thread will run first.
Common methods for controlling thread cycles
Wait () Frees the CPU's execution power and releases the lock.
Notify () back to the state before the wait.
Yied () lets the thread temporarily pause. (Let the thread release the resource)
Join () to force the thread to join the execution.
Setdaemon (TRUE) sets the thread to be a background thread (the background thread is bound to end together when the current thread ends).
Note: The end thread principle is to let the Run method end, so just control the run process.
Why to Thread Sync
Sharing code and data between threads can save system overhead and improve efficiency. However, it can also cause data access conflicts. How to implement an organic interaction between threads, and make sure that the shared resource can only be accessed by one thread at a time, that is, thread synchronization.
Data that is shared between multiple threads is called a critical resource.
Mutual exclusion Lock
Each object corresponds to a mutex token, guaranteeing that only one thread at a time can access the object.
The keyword synchronized of a mutex can be written on a method (an object that invokes the method on behalf of the lock), and can be enclosed outside the statement to be locked.
Benefits: Resolves thread-safe issues
Disadvantages: Reduced operating efficiency (to determine the lock, and can not share information);
Deadlock:
Two-thread a,b uses the same object s (s) as a shared resource, and thread A creates the conditions after B runs in execution. In this premise a starts running, enters the synchronized block, the object S is locked, then thread a waits for the end of B to run into the blocking state, then B starts to run, but because cannot access the object s, thread B also enters the blocking state, waits for s to be unlocked by thread A. The final result: two threads are waiting for each other to run.