1. Each object is associated with a monitor. Each monitor corresponds to a wait pool, and the wait pool stores more than N call threads. All threads that call the synchronize (OBJ) Method, if other threads have called the synchronize (OBJ) method to obtain the monitor, and the monitor has not been released, the current thread enters the wait pool and knows that there are methods for obtaining the monitor to release mnitor, or the object is called. policy (), object. notyfyall () method. Monitor can be considered as mutex locks.
2Object. Wait (long), object. Wait () method:
Object. Wait ()EquivalentObject. Wait (0)After the specified time interval, the current thread automatically wakes up and competes with other threads for the object monitor. After obtaining the object Monitor, the current thread continues to run forward.
3Object. y method:
Wake up a single thread waiting for the monitor of this object. If multiple threads are waiting for the object monitor, select one of them to wake up. This option is arbitrary. After a thread calls the wait method, it waits for the object monitor. The wake-up thread can continue execution only after the current thread gives up the object lock. It has no reliable privilege or disadvantage in competing with the object lock.
The synchronization and mutex operations between threads are not directly dealing with critical blocks, but through object objects:
Thread wait:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // perform action appropriate to condition }
Wake-up thread: Wake up the waiting thread in the wait pool of the OBJ monitor,
(1) The first step is to wait for the thread in the pool to get a chance to get the lock. If the lock is obtained, the condition judgment in while is executed. If the lock is not obtained, wait in wait pool
(2) Step 2: determine whether the conditions are met. If you continue to wait in the wait pool
It can be seen that to join the wait pool in the monitor of the object, the first is that the synchronize (OBJ) method does not get the lock, and the second is obj. Wait (), the condition does not meet and then waits.
synchronized (obj) { obj.notifyAll(); }
4Object. policyall method:
At this time, all the waiting threads compete for mutex, while the mutex will only be obtained by one of the threads, and the remaining threads will continue to wait on the mutex. When the execution of the thread that obtains mutex ends, all threads compete for mutex again. At this time, a thread continues to execute because it obtains mutex.
5 thread. Sleep (long), thread.Sleep () method
First, sleep () calls sleep (0 ~
Secondly, sleep has nothing to do with the synchronization and mutual exclusion of these threads. It only manages the internal affairs of its own thread, and runs it on its own and then falls asleep, giving the CPU to other threads.
Third, how can we wake up the sleeping thread and never let him stay asleep. The first is to wake up and execute the code. The second is to use the thread. Interrupt () method to interrupt the sleep status,
6Thread.Interrupt ()
1. How many things can be interrupted?
Object. Wait (long)
Thread. Join ()
Thread. Sleep ()
Blockingqueue. Take (long)
Selector. Select ()
2 is interrutexception an exception?
Interrupuexception is called exception. I am not satisfied, probably because the exception mechanism is used to handle this situation. Just like the hardware interruption of the CPU, the CPU is working. At this time, a peripheral tells the CPU that I have finished the work (or I interrupt you), and the CPU will put down the work in the hand, you can see what the problem is. You don't need to handle it yourself. Instead, you don't need to deal with the original job. If you need to handle it, you need to deal with the new job ~
The same is true for Java interruptexception. After this exception is caught, you only get a chance to check the conditions related to the current thread (such as the value of the variable or the global variable, it does not matter if the thread is terminated.
I have another question: the stop, suspend, and resume methods are not recommended for Java. What is the situation of C and CPP?