SlideShare a Scribd company logo
Greenthreading in Flex
Presented by Huyen Tue Dao
About me
Fell in love with
programming in a C++ class
in high school.

Flex developer since 2006.

Second time presenting,
first time at 360|Flex.

Weapons of choice in L4D2:
grenade launcher/katana
“…PATENTED APPROACH OF SHOOT FIRST, SHOOT LATER,   I AM THE STIG…OKAY, NOT REALLY BUT I DID PASS MY
SHOOT SOME MORE AND THEN WHEN EVERYBODY'S DEAD                        DRIVER’S TEST ON THE FIRST GO
TRY TO ASK A QUESTION OR TWO.”
Outline
The Problem

Some Solutions

Greenthreading

Comparison

Credit Where Credit Is Due

Conclusion

Questions

                    GREENTHREADING IN FLEX
The Problem
Flash Player well suited to certain tasks: animation.

Seems to be able to do several things at once:
concurrency.

However, it can get hung up if one of these things
requires lots of processing.

Does not take complex code to hang it up either.

Let me show you…

                   GREENTHREADING IN FLEX
The Problem
The problem revolves around Flash platform’s
event-driven, frame-base architecture.

Flash movies/SWFs: content divided into frames
played according to a timeline.




                 GREENTHREADING IN FLEX
FLASH SWF
                        1        2    3    4      5         6

         TIMELINE


                                     FLEX SWF
                                      1    2
                                                 FRAME 2 REPEATEDLY CALLED

                      TIMELINE

                            FRAME 1         FRAME 2
                  BOOTSTRAPPING/            APPLICATION CODE
                      PRELOADER

WHAT THE SWF KIND OF LOOKS LIKE
ARTIST (NOT-SO-MUCH) RENDERING
The Problem
Flash platform is event-driven: most everything is
triggered by an event.

Events collected into the Event Queue.

FP handles one event at time.

To get to the next frame? An event is triggered.



                  GREENTHREADING IN FLEX
1   2      3       4   5     6

         TIMELINE

    EVENTS DISPATCHED IN FRAME 1




   EVENT QUEUE




               KEYBOARD EVENT       MOUSE EVENTS
                                                           *FRAME EVENT


THE EVENT QUEUE
ARTIST (NOT-SO-MUCH) RENDERING
The Problem
SWF plays at a certain number of frames per second
(FPS). The time per frame is 1/FPS.

Default FPS is 24. Therefore, a frame needs to
execute every ~42 milliseconds.

Application FPS is not a minimum, it’s a maximum.

If code running in a frame takes longer than allotted
time, FP cannot process next frame event “on time.”

Hello, non-responsive window/beach ball of death.

                   GREENTHREADING IN FLEX
The Problem
Another way to look at it, using Ted Patrick’s Elastic Mental Racetrack:




             IMAGE FROM SEAN CHRISTMANN’S UPDATED ‘ELASTIC RACETRACK’ FOR FLASH 9 AND AVM2



The racetrack extends to accommodate whatever code it is asked to execute.

No built-in way to defer execution till later.

If FP starts executing long-running code, code execution half will just keep
elongating.

FP gets stuck running on the first half of the track.

                                     GREENTHREADING IN FLEX
Some Solutions
General idea: break up long-running code into
smaller batches/jobs.

Trigger batches at different times, different frames.

Gives FP a chance to catch up.

Allow other events to be processed (including the
important screen-updating events).



                   GREENTHREADING IN FLEX
Some Solutions
Several ways to break up the process:

  callLater( ): function called using this will be queued and
  run either at handler for RENDER event or at the next
  ENTER_FRAME event

     UIComponent method

     “Some features, like effects, can cause queued
     functions to be delayed until the feature completes…”

  ENTER_FRAME: run a batch explicitly at each frame

  Timers: run a batch every χ milliseconds

                     GREENTHREADING IN FLEX
Some Solutions
AND NOW FOR A LITTLE CODE…
// To break up an algorithm via callLater()
// 1. Write a method that performs the main work of algorithm and takes as
// parameters the current iteration and the total iterations to execute.
// 2. In method heck for stopping condition and if it has not been reached,
// use callLater to schedule a new call to method.
...
<!-- Some object that is being displayed -->
<mx:UIComponent id=”obj”/>
...
var numIterations : int = 0;
var totalIterations : int = 100000;

private function runLoop(... args) : void
{
  // Do work of iteration here.
  numIterations++;
  if(numIterations < totalIterations)
  {
     obj.callLater( runLoop, /* args to pass to next method call */ );
  }
}

                                 GREENTHREADING IN FLEX
Some Solutions
AND NOW FOR A LITTLE CODE…

//   To break up an algorithm via a ENTER_FRAME event.
//   1. Initialize progress variables.
//   2. Add a listener to the application for the ENTER_FRAME event the does
//     main work of the algorithm.
//   3. In handler check for stopping condition and remove the listener if it
//     has been reached.

var numIterations : int = 0;
var totalIterations : int = 100000;
Application.application.addEventListener(Event.ENTER_FRAME, runLoop);

private function runLoop(event : Event) : void
{
   // Do work of iteration here.
     numIterations++;
     if(numIterations >= totalIterations)
     {
        Application.application.removeEventListener(Event.ENTER_FRAME,runLoop);
     }
}


                                   GREENTHREADING IN FLEX
Some Solutions
AND NOW FOR A LITTLE CODE…
//   To break up an algorithm via a Timer
//   1. Initialize progress variables.
//   2. Initialize a Timer with a delay. Here delay ≈≈ time/frame.
//   3. Add listener for TimerEvent.TIMER that does main work of algorithm.
//   4. In handler check for stopping condition and call stop() on Timer if it
//     has been reached.

var numIterations : int = 0;
var totalIterations : int = 100000;
var timer : Timer = new Timer(1000 / Application.application.stage.frameRate);
timer.addEventListener(TimerEvent.TIMER, runLoop);
timer.start();

private function runLoop(event : TimerEvent) : void
{
   // Do work of iteration here.
     numIterations++;
     if(numIterations >= totalIterations)
     {
        timer.stop();
     }
     // Else next iteration will execute next TimerEvent.TIMER event.
}
                                    GREENTHREADING IN FLEX
Some Solutions
The good:

 Event Queue can process other events between
 batches.

 Application remains responsive.




                GREENTHREADING IN FLEX
Some Solutions
The not-so-good:

  Code becomes little more complex.

  Can take much longer to finish the job:

     Spread out over time.

     More overhead.

  Fine tune amount of work done and how often you do it.

     Can be time-consuming for developer.

     Might need to re-tune if code or data changes.


                       GREENTHREADING IN FLEX
Greenthreading
Multi-threading on FP:

   Developers can’t access threading FP uses:

      Networking

      Watchers on your code for timeouts

   True threading available via PixelBender:

      Designed for media processing and to take advantage of ability of
      shaders, filters, and blenders to run multi-threaded.

      Works great if you can find a way to convert your problem space to
      PIxelBender’s.


                            GREENTHREADING IN FLEX
Greenthreading
So what is a “green thread?”

   Want to give developer more control over when code gets executed.

   Can emulate threads in developer code, i.e., green threads.

   Similar to how other platforms provide threading even for single
   processor systems.

General idea:

   Do as much work as possible in frame.

   Leave enough time for other events to process, for screen to
   update.

                          GREENTHREADING IN FLEX
Greenthreading
How does it work?

  Specify some amount of time to leave for other events:
  EPSILON

  Break long-running code into basic blocks that will run
  repeatedly.

  Each time greenthread finishes with a block, check how
  much total time used so far in frame.

  If the total time used >= TIME_PER_FRAME - EPSILON,
  stop running for now.

                      GREENTHREADING IN FLEX
EXECUTED BEFORE         EPSILON
              GREENTHREAD STARTS
                                   2

          AFTER 1 ITERATIONS


                                   2
          AFTER 2 ITERATIONS


                                   2
          AFTER X ITERATIONS

                                                   EXECUTED AFTER
                                                 GREENTHREAD PAUSES
                                   2
     WAITING FOR NEXT FRAME



GREENTHREADING
ARTIST (NOT-SO-MUCH) RENDERING
Greenthreading
Encapsulate all this as a class: GreenThread by
Charlie Hubbard.

http://code.google.com/p/greenthreads/

Hubbard’s class also provides statistics, progress
monitoring via events.

To use: convert your code block/algorithm/job etc.
to a GreenThread.


                  GREENTHREADING IN FLEX
Greenthreading
AND NOW FOR A LITTLE CODE…
public class MyGreenthreadAlgorithm extends GreenThread
{
  // Called when start() called on a GreenThread
  override public function initialize() : void
  {
    // Initialize variables needed.
    progress = 0;
    maximum = 100;
  }

    override public function run() : Boolean
    {
      // Do work of one iteration here.
      progress++;
      // Return true if done, false otherwise.
    }
}
                           GREENTHREADING IN FLEX
ALGORITHM
                                                                                                  1 ITERATION


 DIVIDE + CONQUER                    ENTER_FRAME                TIMER

                                     1        2             1           2
                                                                                                  LEFTOVER
                                                                                                    TIME



       INCREASE ITERATIONS
          RUN PER FRAME



        ENTER_FRAME              TIMER                                           GREENTHREADING    EPSILON


       1         2           1           2                                       1          2       KEY
                                                   SPECIFY EPSILON: SET AMOUNT
                                                    OF TIME LEFT IN FRAME FOR
                                                   PROCESSING OTHER EVENTS.
                                                    FILL UP REMAINING FRAME
                                                      TIME WITH ITERATIONS




GREENTHREADING VS OTHER SOLUTIONS
ARTIST (NOT-SO-MUCH) RENDERING, NOT DRAWN TO SCALE
Timer             Timer
                    Original                                       GreenThread
                               (1 loop/frame)   (20k loop/frame)


   Runtime         58 sec*      ~11 hrs -          62 sec           85 sec

 Slow Down          None*        V. Large           Small          Moderate

 Responsive                                        NOT
                      NO           YES                                YES
     ?                                            REALLY


                               *If the application doesn’t crash that is.

DEMO COMPARISON
RUNTIME AND RESPONSIVENESS
Greenthreading
The not-so-good:

 Still have overhead.

   More code complexity

   Runtime increase

 Still may need to make adjustments to time left
 over to rest of events.


                   GREENTHREADING IN FLEX
Greenthreading
The good:

 Application responsiveness

 Greenthread determines how much work to do
 itself given bounds.

 Greenthread encapsulates management of time/
 work. Makes some things neater.



                GREENTHREADING IN FLEX
Greenthreading
But what about multi-threading?

  Important to maintain EPSILON time to handle rest of the
  execution and rendering of the frame.

  Queue up Greenthreads as they are instantiated.

  Time allocated to each Greenthread: (time for frame -
  EPSILON) / # active Greenthreads.

  When Greenthreads finish executing, they are removed from
  queue.

  Implemented in Hubbard’s library: just create as many
  GreenThreads as needed.

                       GREENTHREADING IN FLEX
Credit Where Credit Is
Due
Doug Knudsen: “AIR, CSVs, and Mean Greenies
oh my!” @ cubicleman.com

Charlie Hubbard, “Actionscript and Concurrency”
@ wrongnotes.blogspot.com

Drew Cummins, “Green Threads” @
blog.generalrelativity.org

Jesse Warden, “Parsing & Rendering Lots of Data
in Flash Player” @ jessewarden.com

                 GREENTHREADING IN FLEX
Conclusion
Applications that have long-running code can hang because Event
Queue stalls.

To keep application responsive:

  Break work down into small jobs

  Trigger jobs at different time, give Event Queue a chance to
  process other events.

Greenthreading: fit as much work into frame as possible, leave
time for other events.

Costs of greenthreading: more complex code, little longer runtime.


                        GREENTHREADING IN FLEX
Questions?


    THANKS FOR COMING!


                                 HUYEN TUE DAO
                              DAOTUEH@GMAIL.COM
                       QUEENCODEMONKEY @ TWITTER
                        WWW.QUEENCODEMONKEY.COM
        GREENTHREADING IN FLEX
Links
Greenthreading

   http://www.cubicleman.com/2009/03/08/air-csvs-and-mean-greenies-oh-my/

   http://wrongnotes.blogspot.com/2009/02/concurrency-and-actionscript-part-
   i-of.html

   http://blog.generalrelativity.org/actionscript-30/green-threads/

   http://jessewarden.com/2009/02/parsing-rendering-lots-of-data-in-flash-
   player.html

Frame execution model

   http://www.craftymind.com/2008/04/18/updated-elastic-racetrack-for-flash-9-
   and-avm2/

   http://www.onflex.org/ted/2005/07/flash-player-mental-model-elastic.php


                              GREENTHREADING IN FLEX

More Related Content

PPTX
FreeRTOS basics (Real time Operating System)
PPTX
Process synchronization in Operating Systems
PPTX
Concurrency: Mutual Exclusion and Synchronization
PDF
Operating System-Ch6 process synchronization
PPTX
Process synchronization in operating system
PPT
Operating Systems - "Chapter 5 Process Synchronization"
PPTX
PART-1 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PPTX
Transactional Memory
FreeRTOS basics (Real time Operating System)
Process synchronization in Operating Systems
Concurrency: Mutual Exclusion and Synchronization
Operating System-Ch6 process synchronization
Process synchronization in operating system
Operating Systems - "Chapter 5 Process Synchronization"
PART-1 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
Transactional Memory

What's hot (19)

PPT
Ch7 Process Synchronization galvin
PPTX
Operating systems question bank
PDF
Transactional Memory
PPT
Operating System 5
PPT
CPU Scheduling
PPT
PPT
Reviewer cpu scheduling
DOCX
Module-related pages
PPT
Cpu Scheduling Galvin
PDF
Final Exam OS fall 2012-2013 with answers
PDF
OperatingSystemChp3
PPTX
Chapter4
PDF
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
PPTX
Chapter05 new
PPTX
Scheduling Algorithms
PPT
Synchronization linux
PDF
ITFT_Semaphores and bounded buffer
PDF
PPTX
Hard versus Soft real time system
Ch7 Process Synchronization galvin
Operating systems question bank
Transactional Memory
Operating System 5
CPU Scheduling
Reviewer cpu scheduling
Module-related pages
Cpu Scheduling Galvin
Final Exam OS fall 2012-2013 with answers
OperatingSystemChp3
Chapter4
PART-3 : Mastering RTOS FreeRTOS and STM32Fx with Debugging
Chapter05 new
Scheduling Algorithms
Synchronization linux
ITFT_Semaphores and bounded buffer
Hard versus Soft real time system
Ad

Similar to 360|Flex Greenthreading In Flex (20)

PPTX
Python multithreading
PDF
Python multithreading
PDF
Deep into your applications, performance & profiling
PDF
BEAM (Erlang VM) as a Soft Real-time Platform
PPTX
Microsoft kafka load imbalance
PPT
Os2 2
PDF
Linux Security and How Web Browser Sandboxes Really Work (NDC Oslo 2017)
PDF
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
PDF
Terraforming
PDF
Let's Talk Locks!
PPTX
04 threads-pbl-2-slots
PPTX
04 threads-pbl-2-slots
PDF
HARD COPY REPORT CDAC
PPT
05 Transactions
PDF
Import golang; struct microservice
PDF
The Beam Vision for Portability: "Write once run anywhere"
PPT
DDD Framework for Java: JdonFramework
PDF
Developing a Reverberation Plugin with the aim of a Genetic Algorithm
PDF
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Python multithreading
Python multithreading
Deep into your applications, performance & profiling
BEAM (Erlang VM) as a Soft Real-time Platform
Microsoft kafka load imbalance
Os2 2
Linux Security and How Web Browser Sandboxes Really Work (NDC Oslo 2017)
Devel::NYTProf v3 - 200908 (OUTDATED, see 201008)
Terraforming
Let's Talk Locks!
04 threads-pbl-2-slots
04 threads-pbl-2-slots
HARD COPY REPORT CDAC
05 Transactions
Import golang; struct microservice
The Beam Vision for Portability: "Write once run anywhere"
DDD Framework for Java: JdonFramework
Developing a Reverberation Plugin with the aim of a Genetic Algorithm
Devel::NYTProf 2009-07 (OUTDATED, see 201008)
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPT
Teaching material agriculture food technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
Advanced methodologies resolving dimensionality complications for autism neur...
Teaching material agriculture food technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Approach and Philosophy of On baking technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Programs and apps: productivity, graphics, security and other tools
“AI and Expert System Decision Support & Business Intelligence Systems”
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Big Data Technologies - Introduction.pptx
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf

360|Flex Greenthreading In Flex

  • 2. About me Fell in love with programming in a C++ class in high school. Flex developer since 2006. Second time presenting, first time at 360|Flex. Weapons of choice in L4D2: grenade launcher/katana “…PATENTED APPROACH OF SHOOT FIRST, SHOOT LATER, I AM THE STIG…OKAY, NOT REALLY BUT I DID PASS MY SHOOT SOME MORE AND THEN WHEN EVERYBODY'S DEAD DRIVER’S TEST ON THE FIRST GO TRY TO ASK A QUESTION OR TWO.”
  • 3. Outline The Problem Some Solutions Greenthreading Comparison Credit Where Credit Is Due Conclusion Questions GREENTHREADING IN FLEX
  • 4. The Problem Flash Player well suited to certain tasks: animation. Seems to be able to do several things at once: concurrency. However, it can get hung up if one of these things requires lots of processing. Does not take complex code to hang it up either. Let me show you… GREENTHREADING IN FLEX
  • 5. The Problem The problem revolves around Flash platform’s event-driven, frame-base architecture. Flash movies/SWFs: content divided into frames played according to a timeline. GREENTHREADING IN FLEX
  • 6. FLASH SWF 1 2 3 4 5 6 TIMELINE FLEX SWF 1 2 FRAME 2 REPEATEDLY CALLED TIMELINE FRAME 1 FRAME 2 BOOTSTRAPPING/ APPLICATION CODE PRELOADER WHAT THE SWF KIND OF LOOKS LIKE ARTIST (NOT-SO-MUCH) RENDERING
  • 7. The Problem Flash platform is event-driven: most everything is triggered by an event. Events collected into the Event Queue. FP handles one event at time. To get to the next frame? An event is triggered. GREENTHREADING IN FLEX
  • 8. 1 2 3 4 5 6 TIMELINE EVENTS DISPATCHED IN FRAME 1 EVENT QUEUE KEYBOARD EVENT MOUSE EVENTS *FRAME EVENT THE EVENT QUEUE ARTIST (NOT-SO-MUCH) RENDERING
  • 9. The Problem SWF plays at a certain number of frames per second (FPS). The time per frame is 1/FPS. Default FPS is 24. Therefore, a frame needs to execute every ~42 milliseconds. Application FPS is not a minimum, it’s a maximum. If code running in a frame takes longer than allotted time, FP cannot process next frame event “on time.” Hello, non-responsive window/beach ball of death. GREENTHREADING IN FLEX
  • 10. The Problem Another way to look at it, using Ted Patrick’s Elastic Mental Racetrack: IMAGE FROM SEAN CHRISTMANN’S UPDATED ‘ELASTIC RACETRACK’ FOR FLASH 9 AND AVM2 The racetrack extends to accommodate whatever code it is asked to execute. No built-in way to defer execution till later. If FP starts executing long-running code, code execution half will just keep elongating. FP gets stuck running on the first half of the track. GREENTHREADING IN FLEX
  • 11. Some Solutions General idea: break up long-running code into smaller batches/jobs. Trigger batches at different times, different frames. Gives FP a chance to catch up. Allow other events to be processed (including the important screen-updating events). GREENTHREADING IN FLEX
  • 12. Some Solutions Several ways to break up the process: callLater( ): function called using this will be queued and run either at handler for RENDER event or at the next ENTER_FRAME event UIComponent method “Some features, like effects, can cause queued functions to be delayed until the feature completes…” ENTER_FRAME: run a batch explicitly at each frame Timers: run a batch every χ milliseconds GREENTHREADING IN FLEX
  • 13. Some Solutions AND NOW FOR A LITTLE CODE… // To break up an algorithm via callLater() // 1. Write a method that performs the main work of algorithm and takes as // parameters the current iteration and the total iterations to execute. // 2. In method heck for stopping condition and if it has not been reached, // use callLater to schedule a new call to method. ... <!-- Some object that is being displayed --> <mx:UIComponent id=”obj”/> ... var numIterations : int = 0; var totalIterations : int = 100000; private function runLoop(... args) : void { // Do work of iteration here. numIterations++; if(numIterations < totalIterations) { obj.callLater( runLoop, /* args to pass to next method call */ ); } } GREENTHREADING IN FLEX
  • 14. Some Solutions AND NOW FOR A LITTLE CODE… // To break up an algorithm via a ENTER_FRAME event. // 1. Initialize progress variables. // 2. Add a listener to the application for the ENTER_FRAME event the does // main work of the algorithm. // 3. In handler check for stopping condition and remove the listener if it // has been reached. var numIterations : int = 0; var totalIterations : int = 100000; Application.application.addEventListener(Event.ENTER_FRAME, runLoop); private function runLoop(event : Event) : void { // Do work of iteration here. numIterations++; if(numIterations >= totalIterations) { Application.application.removeEventListener(Event.ENTER_FRAME,runLoop); } } GREENTHREADING IN FLEX
  • 15. Some Solutions AND NOW FOR A LITTLE CODE… // To break up an algorithm via a Timer // 1. Initialize progress variables. // 2. Initialize a Timer with a delay. Here delay ≈≈ time/frame. // 3. Add listener for TimerEvent.TIMER that does main work of algorithm. // 4. In handler check for stopping condition and call stop() on Timer if it // has been reached. var numIterations : int = 0; var totalIterations : int = 100000; var timer : Timer = new Timer(1000 / Application.application.stage.frameRate); timer.addEventListener(TimerEvent.TIMER, runLoop); timer.start(); private function runLoop(event : TimerEvent) : void { // Do work of iteration here. numIterations++; if(numIterations >= totalIterations) { timer.stop(); } // Else next iteration will execute next TimerEvent.TIMER event. } GREENTHREADING IN FLEX
  • 16. Some Solutions The good: Event Queue can process other events between batches. Application remains responsive. GREENTHREADING IN FLEX
  • 17. Some Solutions The not-so-good: Code becomes little more complex. Can take much longer to finish the job: Spread out over time. More overhead. Fine tune amount of work done and how often you do it. Can be time-consuming for developer. Might need to re-tune if code or data changes. GREENTHREADING IN FLEX
  • 18. Greenthreading Multi-threading on FP: Developers can’t access threading FP uses: Networking Watchers on your code for timeouts True threading available via PixelBender: Designed for media processing and to take advantage of ability of shaders, filters, and blenders to run multi-threaded. Works great if you can find a way to convert your problem space to PIxelBender’s. GREENTHREADING IN FLEX
  • 19. Greenthreading So what is a “green thread?” Want to give developer more control over when code gets executed. Can emulate threads in developer code, i.e., green threads. Similar to how other platforms provide threading even for single processor systems. General idea: Do as much work as possible in frame. Leave enough time for other events to process, for screen to update. GREENTHREADING IN FLEX
  • 20. Greenthreading How does it work? Specify some amount of time to leave for other events: EPSILON Break long-running code into basic blocks that will run repeatedly. Each time greenthread finishes with a block, check how much total time used so far in frame. If the total time used >= TIME_PER_FRAME - EPSILON, stop running for now. GREENTHREADING IN FLEX
  • 21. EXECUTED BEFORE EPSILON GREENTHREAD STARTS 2 AFTER 1 ITERATIONS 2 AFTER 2 ITERATIONS 2 AFTER X ITERATIONS EXECUTED AFTER GREENTHREAD PAUSES 2 WAITING FOR NEXT FRAME GREENTHREADING ARTIST (NOT-SO-MUCH) RENDERING
  • 22. Greenthreading Encapsulate all this as a class: GreenThread by Charlie Hubbard. http://code.google.com/p/greenthreads/ Hubbard’s class also provides statistics, progress monitoring via events. To use: convert your code block/algorithm/job etc. to a GreenThread. GREENTHREADING IN FLEX
  • 23. Greenthreading AND NOW FOR A LITTLE CODE… public class MyGreenthreadAlgorithm extends GreenThread { // Called when start() called on a GreenThread override public function initialize() : void { // Initialize variables needed. progress = 0; maximum = 100; } override public function run() : Boolean { // Do work of one iteration here. progress++; // Return true if done, false otherwise. } } GREENTHREADING IN FLEX
  • 24. ALGORITHM 1 ITERATION DIVIDE + CONQUER ENTER_FRAME TIMER 1 2 1 2 LEFTOVER TIME INCREASE ITERATIONS RUN PER FRAME ENTER_FRAME TIMER GREENTHREADING EPSILON 1 2 1 2 1 2 KEY SPECIFY EPSILON: SET AMOUNT OF TIME LEFT IN FRAME FOR PROCESSING OTHER EVENTS. FILL UP REMAINING FRAME TIME WITH ITERATIONS GREENTHREADING VS OTHER SOLUTIONS ARTIST (NOT-SO-MUCH) RENDERING, NOT DRAWN TO SCALE
  • 25. Timer Timer Original GreenThread (1 loop/frame) (20k loop/frame) Runtime 58 sec* ~11 hrs - 62 sec 85 sec Slow Down None* V. Large Small Moderate Responsive NOT NO YES YES ? REALLY *If the application doesn’t crash that is. DEMO COMPARISON RUNTIME AND RESPONSIVENESS
  • 26. Greenthreading The not-so-good: Still have overhead. More code complexity Runtime increase Still may need to make adjustments to time left over to rest of events. GREENTHREADING IN FLEX
  • 27. Greenthreading The good: Application responsiveness Greenthread determines how much work to do itself given bounds. Greenthread encapsulates management of time/ work. Makes some things neater. GREENTHREADING IN FLEX
  • 28. Greenthreading But what about multi-threading? Important to maintain EPSILON time to handle rest of the execution and rendering of the frame. Queue up Greenthreads as they are instantiated. Time allocated to each Greenthread: (time for frame - EPSILON) / # active Greenthreads. When Greenthreads finish executing, they are removed from queue. Implemented in Hubbard’s library: just create as many GreenThreads as needed. GREENTHREADING IN FLEX
  • 29. Credit Where Credit Is Due Doug Knudsen: “AIR, CSVs, and Mean Greenies oh my!” @ cubicleman.com Charlie Hubbard, “Actionscript and Concurrency” @ wrongnotes.blogspot.com Drew Cummins, “Green Threads” @ blog.generalrelativity.org Jesse Warden, “Parsing & Rendering Lots of Data in Flash Player” @ jessewarden.com GREENTHREADING IN FLEX
  • 30. Conclusion Applications that have long-running code can hang because Event Queue stalls. To keep application responsive: Break work down into small jobs Trigger jobs at different time, give Event Queue a chance to process other events. Greenthreading: fit as much work into frame as possible, leave time for other events. Costs of greenthreading: more complex code, little longer runtime. GREENTHREADING IN FLEX
  • 31. Questions? THANKS FOR COMING! HUYEN TUE DAO DAOTUEH@GMAIL.COM QUEENCODEMONKEY @ TWITTER WWW.QUEENCODEMONKEY.COM GREENTHREADING IN FLEX
  • 32. Links Greenthreading http://www.cubicleman.com/2009/03/08/air-csvs-and-mean-greenies-oh-my/ http://wrongnotes.blogspot.com/2009/02/concurrency-and-actionscript-part- i-of.html http://blog.generalrelativity.org/actionscript-30/green-threads/ http://jessewarden.com/2009/02/parsing-rendering-lots-of-data-in-flash- player.html Frame execution model http://www.craftymind.com/2008/04/18/updated-elastic-racetrack-for-flash-9- and-avm2/ http://www.onflex.org/ted/2005/07/flash-player-mental-model-elastic.php GREENTHREADING IN FLEX

Editor's Notes

  • #9: Frame event: especially important because where drawing of the stage occurs.
  • #10: Bring up simple parser application code and the parsing loop and reiterate that because application keeps processing loop, application becomes unresponsive, hangs, beach ball of death.
  • #11: Bring up simple parser application code and the parsing loop and reiterate that because application keeps processing loop, application becomes unresponsive, hangs, beach ball of death.
  • #14: Now go to application code and show parser example using callLater
  • #15: Now go to application code and show parser example using a Timer event.
  • #17: So what we have here is a trade-off between a quicker-running application that&amp;#x2019;s unresponsive and a responsive application that takes way too long to run.
  • #18: So what we have here is a trade-off between a quicker-running application that&amp;#x2019;s unresponsive and a responsive application that takes way too long to run.
  • #22: Frame event: especially important because where drawing of the stage occurs.
  • #24: Now go to main demo application and show green
  • #30: Links to these blog posts are included at the end of my slides.