Green Threads

November 27th, 2008 drew Leave a comment Go to comments

This is a small framework I wrote that addresses some of the issues associated with ActionScript being executed linearly in one thread. If the language offered an API for threading, computationally expensive routines could be run in a separate thread, keeping the application’s UI appropriately responsive.

The idea is to offer a simple means to decompose:

  • deep recursion
  • large loops
  • computationally expensive tasks

I’ve created 2 demos (FP10) to illustrate the issue.

The first demo is an implementation of the A* algorithm. If you select to execute the path-finding normally, you’ll notice the Flex UI becomes unresponsive; if you’re lucky, you might even get the spinning beach ball of death. But when you run the algorithm in a green thread, the application UI remains responsive. Select start and end nodes at diagonally opposite corners to force the algorithm through a lot of work.

The second demo encodes 3 images. This is a little harder to visualize in a demo, so here’s what’s happening: I take an embedded image, encode it as a .png 3 times and then decode and render the ByteArray to show the encoding’s success. As with the path-finding demo, you’ll see the UI become unresponsive when you run the encoding normally. But decomposed to run over a few seconds, everything runs well.

A thread’s chief responsibility, aside from running its processes, is to maintain frame rate. The API offers the user the ability to set execution frequency and processing share (percentage). It polices itself according to these allocation constraints. If a process exceeds its allotment, it is penalized in the next cycle. This works surprisingly well to maintain frame rate and adhere to the user’s request.

The source is pretty well commented and should be easy to implement! And if nothing else, there’s an object-oriented implementation of the A* algorithm… (based on Ian Millington’s pseudo code in his Artificial Intelligence For Games).

path-finding demo

image encoding demo

source

I started this framework because of (and got the name from) my awesome friend, Roger Braunstein, who shared his elegant and simple solution with me a couple months ago, and referenced the obscure deprecated Java API.

Categories: ActionScript 3.0, Flash Player 10 Tags:
  1. November 27th, 2008 at 09:09 | #1

    Very interesting Drew. I might look at incorporating this in some of my fractal experiments.

  2. December 22nd, 2008 at 13:23 | #2

    super cool. i’m trying it out for parsing data for a degrafa component i’m working on,
    http://rojored.com/#flow-component-sketch

    thanks.

  3. January 6th, 2009 at 03:51 | #3

    Oh… Once I had an idea of the following tool: give a linear algorithm of some math problem and get its frame-splitted or timer-splitted version. When I met this post, I really got upset of the fact, that someone already has done that:)

    When I took a look at source… This is just an abstraction… Like a carcass of where you should put your splitted algorithm to perform it…

    But the main problem is not in orginizing all those abstracts around, but exactly in splitting an algorithm, which is still left for the developer to solve.

    Well… probably this one is still usefull… I dont’t know:) But if you make a sort of parser translating sequential algorithm into timer-splitted version (of frame-splitted) – this would be truely a great achievment.

    Though thanks anyway for the lib and sorry for my english:)

  4. January 6th, 2009 at 03:56 | #4

    In fact sometimes algorithm is so complicated, so full of finish-undefined cycles, with so many conditional goto-s, so you spend like 99.9% of time for splitting it and more for debugging and proving it’s the absolutely equal splitted version for the sequential one. Anf then you can breathe free and wrap the result into event-throwing “Process” abstraction…

  5. January 30th, 2009 at 14:23 | #5

    This looks like a great little library, I’d love to use it. Just curious what the license is on it, just to be safe – ideally it’d be great if it were MIT-style, but I just want to check since I can’t find it anywhere in the code. Thanks!

  6. drew
    February 1st, 2009 at 18:24 | #6

    Yeah, sorry about the licensing. Everything I release can be assumed under the MIT license… I’m just too lazy and/or excited sometimes!

  7. April 5th, 2009 at 23:55 | #7

    romantique: What you describe is basically the holy grail of computing – It probably can be done for most cases, but nobody’s done it yet, and it’s worth millions to whoever does it well first :)

  8. October 21st, 2009 at 19:25 | #8

    Very cool. Works great ! You should consider putting this up on GitHub/GoogleCode.

  9. Clayton Hughes
    March 21st, 2010 at 17:55 | #9

    Sorry for being obtuse, but how exactly is the library supposed to be used? I don’t see any documentation and I haven’t quite beaten ASDoc into working.

    From what I can tell of the example code included, you’ve just created the AbstractProcesses, which is super useful, but I’m not sure how you go from that point to using them.

  10. drew
    March 22nd, 2010 at 20:36 | #10

    @Clayton Hughes
    You can look at the 2 examples included in the source (in the examples package). There’s also a brief pseudo-code listing above the class declaration in GreenThread.as.

    Unfortunately, the tradeoff for free, open-source software is that there’s little support and crappy/absent documentation. But if you have specific questions I can answer or issues with the concept/architecture, I’m more than happy to help!

  11. futesat
    February 3rd, 2011 at 11:04 | #11

    cool! great code!

  12. bebop
    April 3rd, 2011 at 21:41 | #12

    Very interesting, thanks for sharing!

  13. Anant Kale
    May 2nd, 2011 at 00:18 | #13

    Hi, Realy great and useful post. Thanks.

  1. No trackbacks yet.