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