One figure reads Java Multithreading
1. view the picture with questions
1) the lock, CPU resources, and competition of competitors are awakened.
2) under which circumstances the lock is obtained and under which circumstances the lock will be released
2. The figure.
3. detailed illustration
1) Thread t = new Thread (), initializing a Thread is actually a common object, and its status is New
2) t. start (); The thread is ready to run), that is, waiting for running at any time. Don't underestimate this start. This start determines whether it is a real thread instance, because start has prepared a thread environment for it. If you only call the run method normally, this is a normal method. At this time, the thread will compete for CPU resources. Therefore, whoever is competing for CPU resources, that is, Scheduler scheduling, then he can go from the runable status to the real running status.
3) when the thread obtains the CPU resources, the thread will go from the Running status to the real Running status, that is, Running. No doubt, it is Running now.
4) if this thread is waiting for the customer to input and learn, that is, the IO exception, and other blocking events, it may also be that it calls sleep and Other blocking events, the thread changes from the running status to the blocking status. This status will not happen!
5) once the blocking event is cleared, for example, the user has already completed the input, the IO stream has been closed, and the sleep has timed out, the thread has changed from the blocking status to the ready status, and the thread has returned to the running status again, competing resources with other threads at any time, waiting for running!
6) a running thread may encounter a synchronous method or synchronization block during the running process, that is, the method or block marked by synchronized. At this time, the thread gets the object lock, other threads cannot access the synchronization method. What should I do if these threads cannot be executed? All of them are blocked here, waiting for the lock to be released and competing for Lock resources from the new, because only the thread with the lock is eligible to continue to run, these threads are blocked in the Lock Pool ).
7) once the thread in the lock pool is blocked and competing to the lock, the thread runs normally or the previous thread runs abnormally internally, or wait is called, will release the thread lock), then this thread will change from the blocking state to the ready state, do not think this thread will be executed immediately, this is impossible, you have to think that all thread execution needs to obtain CPU resources. If there is no operating system scheduling, they are not qualified to run!
8) a running thread may enter the synchronization method or synchronization block during the running process. At this time, the thread has the object lock and is supreme, however, because the current environment may make it unnecessary for him to continue to execute the job, he will let out the lock resource by himself so that other threads can continue to execute the job, therefore, this thread may call the wait method of the object within synchronized. Once called, the current thread gets out of the lock resource and enters the wait pool by itself) until it is awakened by another thread! If it is not awakened, it will always be in the waiting pool and blocked by threads. So what they want at this time is to be awakened, because it is possible to continue running only after being awakened!
9) once the thread waiting for the pool is blocked, it may be that a synchronized thread calls policy or policyall, or an internal exception is thrown when interrupt is called externally, will also get the lock), then this thread will switch from the wait pool to the lock pool, continue blocking, so do not think that the thread will continue to run after being awakened, this is not possible, they also need to compete to lock resources.
10) if an exception is thrown during the thread running process or the thread is actually running, the thread ends, that is, the termination period. You cannot start a new thread after running it. There will be a lifecycle.