Tag:lin main thread service cond time () str get exce inject
package Com.wjz.quartz; import java.util.concurrent.Executors; public class Quartzdemo { public static void main (string[] args) Throws Exception { new myrunnable (Executors.newsinglethreadscheduledexecutor ()). Schedule (); // Here the main thread sleeps because if the main threaded execution is done, the Myrunnable object is destroyed. The experimental effect cannot be Show Thread.Sleep (1000*60*5
I can see the initialization of a task thread delay scheduler Scheduledexecutorservice executor = Executors.newsinglethreadscheduledexecutor (); Injected into the runnable.
The core class Myrunnable implements the Runnable and Scheduledfuture interfaces
Task scheduling performed in the Run method
PackageCom.wjz.quartz;Importjava.util.Date;Importjava.util.concurrent.Delayed;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ScheduledExecutorService;Importjava.util.concurrent.ScheduledFuture;ImportJava.util.concurrent.TimeUnit;Importjava.util.concurrent.TimeoutException;ImportOrg.springframework.scheduling.support.SimpleTriggerContext;classMyrunnableImplementsRunnable, scheduledfuture<object> { Private FinalScheduledexecutorservice executor; Private FinalMytrigger trigger =NewMytrigger (); Private FinalSimpletriggercontext Triggercontext =NewSimpletriggercontext (); Private FinalObject Triggercontextmonitor =NewObject (); PrivateScheduledfuture<?>currentfuture; PrivateDate Scheduledexecutiontime; Publicmyrunnable (Scheduledexecutorservice executor) { This. Executor =executor; } PublicScheduledfuture<?>Schedule () {synchronized(triggercontextmonitor) {
//Get the next execution time in the plan This. Scheduledexecutiontime =Trigger.nextexecutiontime (Triggercontext); LongDelay = Scheduledexecutiontime.gettime ()-System.currenttimemillis ();
//Delay task scheduling This. currentfuture = Executor.schedule ( This, delay, timeunit.milliseconds); return This; } } Public voidrun () {Date actualexecutiontime=NewDate (); System.out.println ("------Call timing Method------"); Date Completiontime=NewDate (); synchronized(triggercontextmonitor) {triggercontext.update (Scheduledexecutiontime, Actualexecutiontime, CompletionTim e); if(! This. currentfuture.iscancelled ()) {Schedule (); } } } Public BooleanCancelBooleanmayinterruptifrunning) { synchronized( This. Triggercontextmonitor) { return This. Currentfuture.cancel (mayinterruptifrunning); } } Public Booleaniscancelled () {synchronized( This. Triggercontextmonitor) { return This. currentfuture.iscancelled (); } } Public BooleanIsDone () {synchronized( This. Triggercontextmonitor) { return This. Currentfuture.isdone (); } } PublicObject get ()throwsinterruptedexception, executionexception {scheduledfuture<?>Curr; synchronized( This. Triggercontextmonitor) {Curr= This. currentfuture; } returnCurr.get (); } PublicObject Get (LongTimeout, timeunit unit)throwsinterruptedexception, Executionexception, timeoutexception {scheduledfuture<?>Curr; synchronized( This. Triggercontextmonitor) {Curr= This. currentfuture; } returnCurr.get (timeout, unit); } Public Longgetdelay (timeunit unit) {scheduledfuture<?>Curr; synchronized( This. Triggercontextmonitor) {Curr= This. currentfuture; } returnCurr.getdelay (unit); } Public intCompareTo (Delayed other) {if( This==Other ) { return0; } Longdiff = Getdelay (timeunit.milliseconds)-Other.getdelay (timeunit.milliseconds); return(diff = = 0? 0: (diff < 0)? -1:1)); }}
Trigger is mainly to return the trigger time
PackageCom.wjz.quartz;Importjava.util.Date;ImportOrg.springframework.scheduling.Trigger;ImportOrg.springframework.scheduling.TriggerContext; Public classMytriggerImplementsTrigger {PrivateMySequence sequence =NewMySequence (); PublicDate Nextexecutiontime (Triggercontext triggercontext) {Date Date=Triggercontext.lastcompletiontime (); if(Date = =NULL) {Date=NewDate (); } returnSequence.next (date); }}
The schedule builder mainly generates trigger times and returns the next trigger time
PackageCom.wjz.quartz;ImportJava.util.BitSet;ImportJava.util.Calendar;Importjava.util.Date;ImportJava.util.GregorianCalendar;ImportJava.util.TimeZone; Public classMySequence {Private FinalBitSet seconds =NewBitSet (60); MySequence () {setnumber (seconds,"0/5", 0, 60); } PublicDate Next (date date) {Calendar Calendar=NewGregorianCalendar (); Calendar.settime (date); Calendar.settimezone (Timezone.getdefault ()); //Resetting the number of millisecondsCalendar.set (Calendar.millisecond, 0); LongOriginaltimestamp =Calendar.gettimeinmillis (); Donext (Calendar); if(Calendar.gettimeinmillis () = =Originaltimestamp) { //Calendar Time just to the original timestamp time, plus one secondCalendar.add (Calendar.second, 1); Donext (Calendar); } returnCalendar.gettime (); } Private voiddonext (Calendar calendar) {intSecond =Calendar.get (Calendar.second); intNextsecond =Seconds.nextsetbit (second); if(Nextsecond = =-1) { //add a minute .Calendar.add (Calendar.minute, 1); //Reset number of secondsCalendar.set (Calendar.second, 0); Nextsecond= Seconds.nextsetbit (0); } if(Second! =Nextsecond) {Calendar.set (Calendar.second, Nextsecond); } } Private Static voidSetnumber (BitSet BitSet, String field,intMinintmax) {string[] fields= Field.split ("/");//0, 5 int[] split =New int[2]; intStart = split[0] = integer.valueof (fields[0]); intGrow = split[1] = integer.valueof (fields[1]); int[] Range =New int[2]; range[0] = split[0]; intend = Range[1] = max-1; for(inti = start; I < end; i + =grow) {Bitset.set (i); } }}
Custom Spring Timers