teaching machines

CS 330 Lecture 37 – Promises and Futures

Agenda

  • what ?s
  • what does this do?
  • easier concurrency¬†without side effects
  • call by value, name, need
  • the dangers of the C preprocessor
  • delayed/lazy evaluation
  • promises in Ruby
  • futures in Java

What Does This Do?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define PRINT_SMALL(r) \
  if (r < 1000000) \
    printf("%d\n", r)

int main(int argc, char **argv) {
  srand(time(NULL));
  
  for (int i = 0; i < 100000; ++i) {
    PRINT_SMALL(rand());
  }
  
  return 0;
}

Code

compromise.rb

#!/usr/bin/env ruby

require 'open-uri'

class Promise
  def initialize &block
    @block = block
    @cache = nil
  end

  def get
    if not @cache
      @cache = @block.call
    end
    @cache
  end
end

p = Promise.new do
  puts "The block is executing!"
  open('http://www.twodee.org/tests/slowimage/slowimage.php').read;
end

# puts p.get.length 
# puts p.get.length 
# puts p.get.length 

Thunk.java

package foo;

public interface Thunk<T> {
  public T call();
}

Future.java

package foo;

public class Future<T> {
  private T cache;
  private Thread thread;

  public Future(Thunk<T> thunk) {
    thread = new Thread(new Runnable() {
      @Override
      public void run() {
        cache = thunk.call();
      }
    });
    thread.start();
  }
  
  public T get() {
    while (cache == null) {
      try {
        thread.join();
      } catch (InterruptedException e) {
      }
    }
    return cache;
  }
}

Main.java

package foo;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Main {
  public static void main(String[] args) throws IOException {
    Thunk<BufferedImage> thunk = new Thunk<BufferedImage>() {
      @Override
      public BufferedImage call() {
        try {
          BufferedImage in = ImageIO.read(new File("/Users/johnch/Desktop/plasma.bmp"));
          Thread.sleep(5000);
          BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(), in.getType());

          for (int r = 0; r < out.getHeight(); ++r) {
            for (int c = 0; c < out.getWidth(); ++c) {
              Color color = new Color(in.getRGB(c, r));
              Color swappedColor = new Color(color.getBlue(), color.getGreen(), color.getRed());
              out.setRGB(c, r, swappedColor.getRGB());
            }
          }

          return out;
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    };

    System.out.println("starting future");
    Future<BufferedImage> future = new Future<>(thunk);
    System.out.println("constructed future");

    
    BufferedImage image = future.get();
    ImageIO.write(image, "bmp", new File("/Users/johnch/Desktop/future_plasma.bmp"));
  }
}

Haiku

No, that arrow zinged!
He’ll love the first thing he sees
Hit him with a thunk

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *