Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

The greatest source of happiness is the ability to be grateful at all times.

~ Zig Ziglar (and many others)

The greatest source of happiness is ...
Table of Contents1 - Problem2 - Solution3 - Run one task, but block4 - Run one thing, but don’t block, use callback5 - The onSuccess and onFailure callback methods6 - Creating a method to return a Future[T]7 - How to use multiple Futures in a for loop8 - Discussion9 - A future and ExecutionContext10 - Callback methods11 - For-comprehensions (combinators: map, flatMap, filter, foreach, recoverWith, fallbackTo, andThen)12 - See Also13 - The Scala Cookbook

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 13.9, “Simple concurrency with Scala Futures.”

Back to top

Problem

You want a simple way to run one or more tasks concurrently in a Scala application, including a way to handle their results when the tasks finish. For instance, you may want to make several web service calls in parallel, and then work with their results after they all return.

Back to top

Solution

A Future gives you a simple way to run an algorithm concurrently. A future starts running concurrently when you create it and returns a result at some point, well, in the future. In Scala, it’s said that a future returns “eventually.”

A moose statue inside Krazy Moose Subs, Wasilla, Alaska. Seems fitting to go along with my moose in Rocky Mountain National Park image earlier today.

A moose statue inside Krazy Moose Subs, Wasilla, Alaska

Unfortunately this image (and accompanying video) are blurry — I didn’t want to get too close to them — but here are two moose in Rocky Mountain National Park, July 31, 2019.

Two moose in Rocky Mountain National Park

The wine guy at the liquor store was way too helpful yesterday, trying to sell me some very nice bottles of wine. I finally had to tell him, “Look, I just want some cheap wine to help me fall asleep faster at night.”

“Like Boone’s Farm,” he asked.

“Pretty much.”

“A good choice. Right over here, sir,” he said as we walked to the cheap wine section.

~ a note from august 1, 2014

I went up to Rocky Mountain National Park yesterday, and there was still some snow in the mountains. This snow is near the Alpine Visitor’s Center at a little under 12,000 altitude.

Snow at the top of Rocky Mountain National Park

Wow, many years ago I thought there should be an errata page for the Scala Cookbook, but to the best of my knowledge we never had one. But now, to my surprise, there is one.

“If we long for our planet to be important, there is something we can do about it. We make our world significant by the courage of our questions and the depth of our answers.”

~ Carl Sagan, Cosmos (via @WorldAndScience)

The Kwan Um Zen school has this fun Sherlock Holmes and Doctor Watson story.

And I had never heard of the saying, “It’s easy to see a flea on the nose of a person one mile away, but very difficult to see an elephant standing on your own nose,” but it’s way too true, and I like it.

Flowers and a grassy roof at the Visitor’s Center, Anchorage, Alaska.

Flowers and a grassy roof, the Visitor’s Center, Anchorage, Alaska

Creating a huge federal deficit during times of low unemployment seems like a ridiculous idea. It’s like racking up a huge credit card debt when you’re making good money. When times are hard, you’re not going to be able to pay off that debt and you’re going to have to declare bankruptcy.

A huge federal deficit is not a good idea

“Books are the training weights of the mind.”

~ Epictetus

“If you want to improve, be content to be thought foolish and stupid.” ~ Epictetus

(I learned of this quote while watching the movie Serendipity last night.)

“You become what you give your attention to. If you yourself don’t choose what thoughts and images you expose yourself to, someone else will ... and their motives may not be the highest.”

~ Epictetus

A snowy mountain peak, Palmer, Alaska. Took this picture on a bike ride in the spring of 2011.

A snowy mountain peak, Palmer, Alaska

I haven’t worked on many open source projects, so my ability to fork a Github project, pull it down, create a branch, push that branch back, and then submit a pull request are weak, at best.

That being said, I’ve done it a few times lately, so I’m getting better at it. Today was a very smooth process, so I thought I’d make these notes while they’re still fresh in my mind.

As a brief note today, I found that GraalVM was actually making one of my Scala/Java/JVM applications slower, so with the help of Thomas Wuerthinger at Oracle, I learned a little bit about how to use the GraalVM profile-guided optimizations.

I reloaded Gimp with all of its custom special effects and in my copious spare time at night I’ve been working on a simulated sketch of yours truly (Alvin Alexander).

A simulated pencil sketch of yours truly

[From time to time I write little stories that have nothing to do with programming or technology; this is one of those stories. So, if you’re only here for the technology stuff, you’ll want to skip this one.]

I’m standing in the kitchen of a friend’s house at a Christmas party, making myself a drink while talking to a friend named Angie. This was nothing unusual; she and I were always talking about something. We became friends during our last year in high school, and we’ve been talking every since.

In retrospect it’s obvious that I have feelings for her, but I guess you could say that I didn’t appreciate her back then. After high school my ambition took me away to college, and then to a series of jobs in different states. By the time I decided to move back home, she was married and had two young children.

While we talked all the time, this kitchen conversation was unusual. I don’t remember how it started, but Angie did ask me about something I rarely talk about: my parents getting divorced in high school.

To love the right,
Yet do so wrong.

To be the weak,
Yet burn to be so strong.

Go rider, although your ride has been through lies.
Go rider, see your soul through the devil’s eyes.

If I could live my life again,
Would I live that life in sin?

Go rider, go ride into the night.
Go rider, now see your soul through a woman’s eyes.

I am sinner,
Hold my prayers up to the sun.
(Hold my prayers up to the sun.)

I am sinner,
Heaven’s closed for what I’ve done.

.
.
.

What have I done ...
(What have I done?)
What have I done?

I am sinner.
(I am sinner.)
Hold my prayers up to the sun.
(Hold my prayers up to the sun.)

I am sinner.
Heaven’s closed for what I’ve done.
Heaven’s closed for what I’ve done ...

~ Sinner’s Prayer, Salvatore P. “Sully” Erna