Come to Java multi-thread design mode
1. Wait
(1) To call the wait method, you must first obtain the lock of the object instance;
(2) You can use the synchronized method, synchronized block, or other methods called by both;
(3) The thread executing the wait method will enter the wait set of OBJ, And the OBJ lock will be released after it enters;
(4) The thread awakened by Y or notifyall advances to the next wait statement.
2. nofify and policyall
(1) Wake up one or all threads from the wait set of OBJ (they try to get the OBJ lock), but after waking it (s), the thread has not released the lock
(2) Wait, policy, and policyall are methods of the object class to operate the instance's wait set.
3. Sleep
(1) It is a thread-type method that enables the thread to enter the blocking state without the CPU time, and the time passes through to re-enter the executable state. However, the sleep method does not release the object lock.
(2) Only the currently running threads can be controlled.
4. Yield
(1) pause the currently running thread object and execute other threads to bring the currently running thread back to the running state, and allow other threads with the same priority to get the running opportunity.
(2) but it may be rescheduled without concessionProgramSelected.
5. Join
(1) Add thread B to the tail of thread.
(2) B cannot work before execution of.
Ii. Impact of calling interrupt:
1. For sleep
Any thread can call the interrupt method of other threads at any time. After interrupt is used, the thread stops the pause and throws an exception.
2. For wait
If obj is already in the wait set, the interrupt method causes the thread to obtain the lock again and throw an exception. If the lock is not obtained, an exception cannot be thrown.
When executing interrupt, you do not need to get the lock on this thread.
3. Join
Call the joion method and jump to the Catch Block immediately without getting the lock.
3. Calling the interrupt method may not necessarily throw interruptexception:
(1) If you do not execute sleep, wait, or join during the call, no exception is thrown, but the interruption status is changed;
(2) If sleep, wait, or join is executed, an exception is thrown.
// JobPrivate VoidDowork ()ThrowsInterruptedexception {counter ++; system. Out. println ("Dowork: Counter ="+ Counter );Try{Thread. Sleep (500 );}Catch(Interruptedexception e ){}}
If you call interrupt, it cannot be interrupted.
System. Out. println ("Main: shutdownrequest"); T. shutdownrequest ();
It must be as follows:
// JobPrivate VoidDowork ()ThrowsInterruptedexception {counter ++; system. Out. println ("Dowork: Counter =& Quot; + counter); thread. Sleep (500 );}
It can be terminated normally.
Capture where dowork is called:
// ActionPublic VoidRun (){Try{While(! Isinterrupted () {dowork ();}}Catch(Interruptedexception e ){}Finally{Doshutdown ();}}
// Terminate the requestPublic VoidShutdownrequest () {interrupt ();}
The above caught exception is not processed, so it cannot be captured in the run, and the doshutdown method cannot be executed to stop.
Comparison:
Public SynchronizedRequest getrequest ()ThrowsInterruptedexception {While(Queue. Size () <= 0) {Wait ();}Return(Request) queue. removefirst ();}
An exception is thrown here, captured at the place of the call, and exit run.
Public VoidRun (){Try{For(IntI = 0; I <10000; I ++) {request =NewRequest ("No."+ I); system. Out. println (thread. currentthread (). getname () +"Requests"+ Request); requestqueue. putrequest (request); thread. Sleep (random. nextint (1000 ));}}Catch(Interruptedexception e ){}}
Interrupt:
// call the interrupt method system. out. println (" ***** calling interrupt ***** "); Alice. interrupt (); Bobby. interrupt ();