the practice of iterator methods we commonly use
Private classListitrextendsItrImplementsListiterator<e>{Listitr (intindex) {Cursor=index; } Public Booleanhasprevious () {returnCursor! = 0; } PublicE Previous () {checkforcomodification (); Try { inti = Cursor-1; E previous=get (i); Lastret= cursor =i; returnprevious; } Catch(indexoutofboundsexception e) {checkforcomodification (); Throw Newnosuchelementexception (); } } Public intNextindex () {returncursor; } Public intPreviousindex () {returnCursor-1; } Public voidset (e e) {if(Lastret = =-1) Throw Newillegalstateexception (); Checkforcomodification (); Try{abstractlist. This. Set (Lastret, E); Expectedmodcount=Modcount; } Catch(Indexoutofboundsexception ex) {Throw Newconcurrentmodificationexception (); } } Public voidAdd (e e) {checkforcomodification (); Try{abstractlist. This. Add (cursor++, E); Lastret=-1; Expectedmodcount=Modcount; } Catch(Indexoutofboundsexception ex) {Throw Newconcurrentmodificationexception (); } } }
the Resize method and Putall method in HashMap
void resize (int Newcapacity) {entry[] oldtable = table; int oldcapacity = Oldtable.length; if (oldcapacity == maximum_capacity) { Threshold = Integer.max_value; return ; } entry[] newtable = new entry[newcapacit Y]; Transfer (newtable); Table = newtable; Threshold = (int ) (newcapacity *
Public voidPutall (map<?extendsK?extendsV>m) {intnumkeystobeadded =m.size (); if(numkeystobeadded = = 0) return; if(Numkeystobeadded >threshold) { intTargetcapacity = (int) (Numkeystobeadded/loadfactor + 1); if(Targetcapacity >maximum_capacity) targetcapacity=maximum_capacity; intNewcapacity =table.length; while(Newcapacity <targetcapacity) newcapacity<<= 1; if(Newcapacity >table.length) Resize (newcapacity); } for(ITERATOR<?extendsmap.entry<?extendsK?extendsv>> i =M.entryset (). iterator (); I.hasnext ();) {Map.entry<?extendsK?extendsV> e =I.next (); Put (E.getkey (), E.getvalue ()); } }
I write my own application log in server monitoring, inheriting the Runnable interface
Public voidrun () {String curstopedreasonmsg; Try { if( This. Applicationmodel.getonpreruncallback ()! =NULL) { This. Applicationmodel.getonpreruncallback (). CallBack ( This); } intFirstflag = This. Applicationmodel.getfirstflag (); String Command= Cmdutil.getcommand ( This. Applicationmodel); Log.info ("EXE Command====>:" +command); File FilePath= Stringutils.isnotblank ( This. Applicationmodel.getprogrampath ())?NewFile ( This. Applicationmodel.getprogrampath () + file.separator):NULL; This. Process = Runtime.getruntime (). EXEC (Command,NULL, FilePath); if( This. Applicationmodel.getonruncallback ()! =NULL) { This. Applicationmodel.getonruncallback (). CallBack ( This); } inputstream stdin= This. Process.getinputstream (); InputStream stderr= This. Process.geterrorstream (); Thread TIn=NewThread (NewAppconsole (stdin, This. Applicationmodel)); Thread Terr=NewThread (NewAppconsole (stderr, This. Applicationmodel)); Tin.start (); Terr.start (); if( This. Applicationmodel.getonrunmonicallback ()! =NULL) { This. Applicationmodel.getonrunmonicallback (). CallBack ( This); } intresult = This. Process.waitfor (); Tin.join (); Terr.join (); if(Result = = 0) {Log.info ("Server [" + This. Applicationmodel.getappname () + "] Stop successful!); } Else{log.info ("Server [" + This. Applicationmodel.getappname () + "] Stop failed!"); } String stoptime=Dateutil.getnow (); if(AppStatusEnum.RUNNING.equals ( This. Applicationmodel.getappstatusenum ())) { This. stopedmsg = (appconstants.stoped_exception_msg + "[" + StopTime + "]"); } Else { This. stopedmsg = ( This. applicationmodel.getstopedreasonmsg () + "[" + StopTime + "]"); } } Catch(Exception e) {log.error ("", E); This. stopedmsg = (appconstants.running_exception_msg + "[" + e.getmessage () + "]"); Try { if( This. Applicationmodel.getonstopcallback ()! =NULL) { This. Applicationmodel.getonstopcallback (). CallBack ( This); Curstopedreasonmsg= This. applicationmodel.getstopedreasonmsg (); if( (Stringutils.isnotblank (curstopedreasonmsg))&& (Curstopedreasonmsg.indexof (appconstants.stoped_restart_msg)! =-1) {thread.sleep (3000L); Appmanager.getinstance (). STARTAPP ( This. Applicationmodel); } } } Catch(Exception ex) {Log.error ("", ex); Throw NewRuntimeException (ex); } } finally { Try { if( This. Applicationmodel.getonstopcallback ()! =NULL) { This. Applicationmodel.getonstopcallback (). CallBack ( This); Curstopedreasonmsg= This. applicationmodel.getstopedreasonmsg (); if( (Stringutils.isnotblank (curstopedreasonmsg))&& (Curstopedreasonmsg.indexof (appconstants.stoped_restart_msg)! =-1) {thread.sleep (3000L); Appmanager.getinstance (). STARTAPP ( This. Applicationmodel); } } } Catch(Exception e) {log.error ("", E); Throw NewRuntimeException (e); } } }
I have the level of code