Display a countdown for the python sleep function

you could always do #do some stuff print ‘tasks done, now sleeping for 10 seconds’ for i in xrange(10,0,-1): time.sleep(1) print i This snippet has the slightly annoying feature that each number gets printed out on a newline. To avoid this, you can import sys import time for i in xrange(10,0,-1): sys.stdout.write(str(i)+’ ‘) sys.stdout.flush() time.sleep(1)

Thread.sleep() implementation

One could easily say Occam’s Razor cuts the other way. The normal/expected implementation of the JVM underlying JDK is assumed to bind java ‘threads’ onto native threads most of the time, and putting a thread to sleep is a fundamental function of the underlying platform. Why reimplement it in java if thread code is going … Read more

C# put pc to sleep or hibernate

// Hibernate Application.SetSuspendState(PowerState.Hibernate, true, true); // Standby Application.SetSuspendState(PowerState.Suspend, true, true); Or, if you like system calls: [DllImport(“Powrprof.dll”, CharSet=CharSet.Auto, ExactSpelling=true)] public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent); // Hibernate SetSuspendState(true, true, true); // Standby SetSuspendState(false, true, true);

Behavior of sleep and select in go

That’s a very interesting question, so I did cd into my Go source to start looking. time.Sleep time.Sleep is defined like this: // src/time/sleep.go // Sleep pauses the current goroutine for at least the duration d. // A negative or zero duration causes Sleep to return immediately. func Sleep(d Duration) No body, no definition in … Read more

How to “sleep” until timeout or cancellation is requested

I just blogged about it here: CancellationToken and Thread.Sleep in Short: var cancelled = token.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)); In your context: void MyFunc (CancellationToken ct) { //… // simulate some long lasting operation that should be cancelable var cancelled = ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(10)); }

Wait x seconds or until a condition becomes true

Assuming you want what you asked for, as opposed to suggestions for redesigning your code, you should look at Awaitility. For example, if you want to see if a file will be created within the next 10 seconds, you do something like: await().atMost(10, SECONDS).until(() -> myFile.exists()); It’s mainly aimed at testing, but does the specific … Read more