Lock releasing scenarios for lock-holding threads
- LockedCodeBlock execution completed
- An exception occurs during the execution of the lock code block. The thread is terminated.
- Object # WaitWill release the lock
lock not released
- sleep ()/join ()
- sleep () is to pause a thread for a specified time. After the time, the thread continues to run.
- join () the purpose is to allow the thread to run only after the specified thread has completed its execution
Thread # interrupt
- Application and interruption servicesBlocking
MethodStatus thread
- Blocking MethodIncludingWait ()/join ()/sleep ()
- AvailableIsinterrpted ()Access thread interruption ID
- CallInterruptMay throwInterruptedexceptionException
- ProcessingInterruptedexceptionWhen an exception occurs, you must reset the thread interrupt mark.
Example
Simple stopwatch
Package Org. ybygjy. thread3th. minutes; import Java. AWT. borderlayout; import Java. AWT. flowlayout; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import javax. swing. jbutton; import javax. swing. jframe; import javax. swing. jpanel; import javax. swing. jtextfield; import javax. swing. swingutilities; /*** stopwatch main panel ** <p> * display time * <p> * responsible for * @ author wangyancheng * @ version 2012-10-1 */public class stopwatch {/** for rendering time */private jtextfield timervalue; /** time build thread */private timethread timerthread;/*** constructor */Public stopwatch () {This. timervalue = new jtextfield ("00:18:22 516"); timerthread = new timethread (this); timerthread. start ();}/***** logical processing entry * <p> * creates a jframe * <p> * organizes component relations */Public void dowork () {jframe = new jframe ("Stopwatch"); jframe. setdefaclocloseoperation (jframe. exit_on_close); jframe. setcontentpane (this. createcontentpanel (); jframe. setsize (300,100); jframe. setresizable (false); jframe. setvisible (true);}/*** main panel, filled as a component container * @ return jpanel {@ link jpanel} */private jpanel createcontentpanel () {jpanel = new jpanel (); jpanel. setlayout (New borderlayout (); jpanel. add (this. createopbar (), borderlayout. north); jpanel. add (this. timervalue, borderlayout. center); Return jpanel;}/*** control panel, control button container * @ return rtnpanel {@ link jpanel} */private jpanel createopbar () {jpanel rtnpanel = new jpanel (New flowlayout (flowlayout. center, 5, 5); Final jbutton startbtn = new jbutton ("START"); Final jbutton suspendbtn = new jbutton ("pause"); rtnpanel. add (startbtn); rtnpanel. add (suspendbtn); startbtn. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {timerthread. setdone (false) ;}}); suspendbtn. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {timerthread. setdone (true) ;}}); Return rtnpanel;}/*** re-paint time * @ Param timevalue time string */Public void repainttimevalue (final string timevalue) {swingutilities. invokelater (New runnable () {public void run () {timervalue. settext (timevalue) ;}}) ;}/ *** test entry * @ Param ARGs parameter list */public static void main (string [] ARGs) {new stopwatch (). dowork ();}}
Package Org. ybygjy. thread3th. minutes; import Java. text. simpledateformat; import Java. util. calendar;/*** implement stopwatch time * @ author wangyancheng * @ version 2012-10-1 */public class timethread extends thread {/** pause mark */private Boolean isdone; /** stopwatch Rendering component */private stopwatch swinst;/*** constructor * @ Param swinst {@ link stopwatch} */Public timethread (stopwatch swinst) {This. swinst = swinst; // This is paused by default. isdone = true ;}@ override public synchronized void run () {While (true) {try {If (isdone) {Wait ();} // generates a time string swinst. repainttimevalue (gettimestr (); // sleep (10); wait (10);} catch (interruptedexception e) {e. printstacktrace () ;}}/ *** pause mark * @ Param isdone {true: Pause; false: Continue} */Public synchronized void setdone (Boolean isdone) {If (this. isdone = isdone) {return;} This. isdone = isdone; Policy ();}/*** generate time string * @ return rtnstr time string */private string gettimestr () {return sdfinst. format (calendar. getinstance (). gettime ();}/** Time Format String */Private Static simpledateformat sdfinst = new simpledateformat ("HH: mm: SS ");}