Suspend and resume of Java learning notes

Source: Internet
Author: User

Suspend and Resume:

Suspend and resume use methods:

The following example proves that the thread is actually paused and can be restored to a running state.

 Public classSuspendresumethreadextendsthread{Private Longi = 0;  Public LongGeti () {returni; }     Public voidSetI (Longi) { This. i =i; } @Override Public voidrun () { while(true) {i++; }    }} Public classThreadrunmain { Public Static voidMain (string[] args) {testsuspendresumethread (); }   Public Static voidTestsuspendresumethread () {Try{Suspendresumethread srt=NewSuspendresumethread ();            Srt.start (); Thread.Sleep (5000);            Srt.suspend (); System.out.println ("A=" + system.currenttimemillis () + "i=" +Srt.geti ()); Thread.Sleep (5000); System.out.println ("A=" + system.currenttimemillis () + "i=" +Srt.geti ());            Srt.resume (); Thread.Sleep (5000);            Srt.suspend (); System.out.println ("A=" + system.currenttimemillis () + "i=" +Srt.geti ()); Thread.Sleep (5000); System.out.println ("A=" + system.currenttimemillis () + "i=" +Srt.geti ()); } Catch(interruptedexception e) {e.printstacktrace (); }    }}

Operation Result:

Suspend and resume disadvantage 1-monopoly:

 Public classSuspendresumeobject {synchronized  Public voidprintstring () {System.out.println ("Begin"); if(Thread.CurrentThread (). GetName (). Equals ("a") {System.out.println ("A thread would suspend.");        Thread.CurrentThread (). suspend (); } System.out.println ("Suspendresumeobject End"); }} Public classThreadrunmain { Public Static voidMain (string[] args) {testsuspendresumeobject (); }    Public Static voidTestsuspendresumeobject () {Try {            FinalSuspendresumeobject Object =NewSuspendresumeobject (); Thread T=NewThread () {@Override Public voidrun () {object.printstring ();            }            }; T.setname (A);            T.start (); Thread.Sleep (1000); Thread T2=NewThread () {@Override Public voidrun () {System.out.println ("Thread 2 start, but can ' t enter printstring ()"); System.out.println ("Because of Printstring () is blocked by thread a");                    Object.printstring (); System.out.println ("Thread 2 Complete");            }            };        T2.start (); } Catch(interruptedexception e) {e.printstacktrace (); }    }}

Operation Result:

 Public classSuspendresumeprintstreamissueextendsthread{Private Longi = 0; @Override Public voidrun () { while(true){//try {i++;//Thread.Sleep (1);System.out.println (i);//} catch (Interruptedexception e) {//e.printstacktrace ();//            }        }    }} Public classThreadrunmain { Public Static voidMain (string[] args) {testsuspendresumeprintstreamissue (); }     Public Static voidtestsuspendresumeprintstreamissue () {Try{suspendresumeprintstreamissue srpsi=Newsuspendresumeprintstreamissue ();            Srpsi.start (); Thread.Sleep (1000);            Srpsi.suspend (); System.out.println ("Main end."); } Catch(interruptedexception e) {e.printstacktrace (); }    }}

Operation Result:


The same is exclusive when the program runs to the PrintStream object println () method inside the stop, the sync lock is not released. This results in the SYSTEM.OUT.PRINTLN in the main () method ("Main end."); cannot be executed.

Suspend and resume disadvantage 2-inconsistent data:

 Public classSuspendresumedataissue {PrivateString userName = "1"; PrivateString Password = "11"; synchronized  Public voidSetValue (String u, string p) { This. UserName =u; if(Thread.CurrentThread (). GetName (). Equals ("a") {System.out.println ("Thread a suspend.");        Thread.CurrentThread (). suspend (); }         This. Password =p; }     Public voidPrintusernamepassword () {System.out.println (UserName+ " " +password); }} Public classThreadrunmain { Public Static voidMain (string[] args) {testsuspendresumedataissue (); }     Public Static voidtestsuspendresumedataissue () {Try{suspendresumedataissue Srdi=Newsuspendresumedataissue (); Thread T1=NewThread () {@Override Public voidrun () {Srdi.setvalue ("A", "AA");            }            }; T1.setname (A);            T1.start (); Thread.Sleep (500); Thread T2=NewThread () {@Override Public voidrun () {Srdi.printusernamepassword ();            }            };        T2.start (); } Catch(interruptedexception e) {e.printstacktrace (); }    }}

Operation Result:

Suspend and resume of Java learning notes

Related Article

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.