Resettable Java Timer

According to the Timer documentation, in Java 1.5 onwards, you should prefer the ScheduledThreadPoolExecutor instead. (You may like to create this executor using Executors.newSingleThreadScheduledExecutor() for ease of use; it creates something much like a Timer.) The cool thing is, when you schedule a task (by calling schedule()), it returns a ScheduledFuture object. You can use … Read more

How do you time a function in Go and return its runtime in milliseconds?

Go’s defer makes this trivial. In Go 1.x, define the following functions: func trace(s string) (string, time.Time) { log.Println(“START:”, s) return s, time.Now() } func un(s string, startTime time.Time) { endTime := time.Now() log.Println(” END:”, s, “ElapsedTime in seconds:”, endTime.Sub(startTime)) } After that, you get Squeaky Clean one line elapsed time log messages: func someFunction() … Read more

VBA Macro On Timer style to run code every set number of seconds, i.e. 120 seconds

When the workbook first opens, execute this code: alertTime = Now + TimeValue(“00:02:00”) Application.OnTime alertTime, “EventMacro” Then just have a macro in the workbook called “EventMacro” that will repeat it. Public Sub EventMacro() ‘… Execute your actions here’ alertTime = Now + TimeValue(“00:02:00”) Application.OnTime alertTime, “EventMacro” End Sub

How to get time.Tick to tick immediately

Unfortunately, it seems that Go developers will not add such functionality in any foreseeable future, so we have to cope… There are two common ways to use tickers: for loop Given something like this: ticker := time.NewTicker(period) defer ticker.Stop() for <- ticker.C { … } Use: ticker := time.NewTicker(period) defer ticker.Stop() for ; true; <- … Read more

How I can run my TimerTask everyday 2 PM?

Calendar today = Calendar.getInstance(); today.set(Calendar.HOUR_OF_DAY, 2); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); // every night at 2am you run your task Timer timer = new Timer(); timer.schedule(new YourTask(), today.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS)); // period: 1 day

Bash sleep in milliseconds

Make sure you’re running your script in Bash, not /bin/sh. For example: #!/usr/bin/env bash sleep 0.1 In other words, try to specify the shell explicitly. Then run either by: ./foo.sh or bash foo.sh. In case, sleep is an alias or a function, try replacing sleep with \sleep.

Run a java function after a specific number of seconds

new java.util.Timer().schedule( new java.util.TimerTask() { @Override public void run() { // your code here } }, 5000 ); EDIT: javadoc says: After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer’s task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can … Read more