Java thread _ 03

Source: Internet
Author: User

Lock releasing scenarios for lock-holding threads

    1. LockedCodeBlock execution completed
    2. An exception occurs during the execution of the lock code block. The thread is terminated.
    3. Object # WaitWill release the lock

lock not released

    1. sleep ()/join ()
    2. sleep () is to pause a thread for a specified time. After the time, the thread continues to run.
    3. join () the purpose is to allow the thread to run only after the specified thread has completed its execution

Thread # interrupt

    1. Application and interruption servicesBlocking
      Method
      Status thread
  1. Blocking MethodIncludingWait ()/join ()/sleep ()
  2. AvailableIsinterrpted ()Access thread interruption ID
  3. CallInterruptMay throwInterruptedexceptionException
  4. 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 ");}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.