3 Interesting Facts About Dart Timers

I wrote a program to test dart’s timers:

#!/home/jim/dart/dart-sdk/bin/dart –enable-checked-mode
#import(“dart:isolate”);

void
zero(Timer timer) {
print(“zero\n”);
}

class Timee {
Timer timer;
int id;

Timee(id) {
this.id = id;
this.timer = new Timer(id, timeout);
}

void
timeout(Timer timer) {
print(“timeout $id\n”);
}
}

void
main() {
var zero = new Timer(0, zero);
print(“created zero\n”);
var one = new Timee(1);
var two = new Timee(2);
print(“end of main\n”);
}

This produces the following output:

created zero
end of main
zero
timeout 1
timeout 2

The first interesting thing is that the timers go off after the main program has exited. This is because, as long as there is unfinished work, dart will continue to process events. The second interesting thing is that timer callbacks are functors. They include the function or method and, in the case of a method, the object (instance) to call it on.

The last and most interesting thing is that the timer with duration 0 is still deferred until after the main program exits. This means that using a zero length timer lets you avoid calling back during a forward call, which can lead to really tricky reentrancy problems for the callers of asynchronous libraries.

Advertisements

About jimbelton

I'm a software developer, and a writer of both fiction and non-fiction, and I blog about movies, books, and philosophy. My interest in religious philosophy and the search for the truth inspires much of my writing.
This entry was posted in dart, programming and tagged , , . Bookmark the permalink.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s