Your SlideShare is downloading. ×
0
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Concurrent Programming with ActionScript workers
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Concurrent Programming with ActionScript workers

12,551

Published on

For additional notes, code example, and links see: …

For additional notes, code example, and links see:
http://probertson.com/articles/2012/10/31/slides-and-code-concurrent-programming-with-actionscript-workers/

One of the most frequent and oldest feature requests for the Flash Platform is the ability to execute code "in the background." In Flash Player 11.4/AIR 3.4 (released in August 2012) this request was finally answered, in the form of ActionScript workers. This presentation covers why concurrent programming is useful, how to use ActionScript workers in their current implementation, and looks at the short- and long-term future for concurrent programming in ActionScript.

Published in: Technology
5 Comments
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
12,551
On Slideshare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
1
Comments
5
Likes
8
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Need a way to make it easy and obvious what code is allowed to call other codecf. other background operations that run developer code: SQLite database, Pixel Bender shaders
  • At this point (when the worker’s start() method is called) the runtime creates the new AVM instance, loads the worker SWF into it, and calls the worker SWF’s main class’s constructor.
  • Transcript

    • 1. Concurrent programming with ActionScript workers Paul Robertson | Adobe Developer Relations | @probertson | http://probertson.com/© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
    • 2. Gratuitous promotion© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 2
    • 3. Gratuitous promotion How can I know what features were added to the Flash runtime recently?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 3
    • 4. Gratuitous promotion How can I know what features were added to the Flash runtime recently? http://adobe.ly/O2SRKF© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 4
    • 5. Why concurrent programming?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 5
    • 6. “My UI keeps freezing!” Why concurrent programming?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 6
    • 7. “I want to run code in the “My UI keeps background” freezing!” Why concurrent programming?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 7
    • 8. “I want to run code in the “My UI keeps background” freezing!” Why concurrent programming? “Give me multithreaded ActionScript… ” “…or give me death!”© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 8
    • 9. Why concurrent programming? Flash Runtime’s execution model  Single threaded  All* code runs in a single line of execution  This includes your code (event handlers) and runtime’s rendering code© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 9
    • 10. Why concurrent programming? Flash Runtime’s execution model  Frame model  Code execution is scheduled into time units a.k.a. “frames”  Length based on frame rate  Tries to execute all code in one thread in a frame’s worth of time© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 10
    • 11. Why concurrent programming? For example…© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 11
    • 12. Why concurrent programming? For example… Enter frame private function onEnterFrame(event:Event):void© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 12
    • 13. Why concurrent programming? For example… Enter frame private function onEnterFrame(event:Event):void Mouse event private function onMouseOver(event:MouseEvent):void© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 13
    • 14. Why concurrent programming? For example… Enter frame private function onEnterFrame(event:Event):void Mouse event private function onMouseOver(event:MouseEvent):void Load complete event private function onDataLoad(event:Event):void© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 14
    • 15. Why concurrent programming? For example… Enter frame private function onEnterFrame(event:Event):void Mouse event private function onMouseOver(event:MouseEvent):void Load complete event private function onDataLoad(event:Event):void Mouse event private function onClick(event:MouseEvent):void© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 15
    • 16. Why concurrent programming? For example… Enter frame private function onEnterFrame(event:Event):void Mouse event private function onMouseOver(event:MouseEvent):void Load complete event private function onDataLoad(event:Event):void Mouse event private function onClick(event:MouseEvent):void ...© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 16
    • 17. Why concurrent programming? For example… Enter frame private function onEnterFrame(event:Event):void Mouse event private function onMouseOver(event:MouseEvent):void Load complete event private function onDataLoad(event:Event):void Mouse event private function onClick(event:MouseEvent):void ... Rendering <native player code>© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 17
    • 18. Why concurrent programming? 33 ms later…© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 18
    • 19. Why concurrent programming? 33 ms later… Enter frame private function onEnterFrame(event:Event):void© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 19
    • 20. Why concurrent programming? Now suppose… Enter frame private function onEnterFrame(event:Event):void Mouse event private function onMouseOver(event:MouseEvent):void Load complete event private function onDataLoad(event:Event):void Mouse event private function onClick(event:MouseEvent):void ... Rendering <native player code>© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 20
    • 21. Why concurrent programming? Now suppose… Enter frame private function onEnterFrame(event:Event):void Mouse event private function onMouseOver(event:MouseEvent):void Load complete event private function onDataLoad(event:Event):void { var myXML:XML = _loader.data; for each (var dataPoint:XML in myXML.dataPoint) { // parse thousands of XML nodes and create objects } } Mouse event private function onClick(event:MouseEvent):void ... Rendering <native player code>© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 21
    • 22. Why concurrent programming? > 33 ms later…© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 22
    • 23. What are Workers?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 23
    • 24. What are workers? A way to do concurrent programming safely* in ActionScript© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 24
    • 25. How do they work?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 25
    • 26. How do they work? Two important architectural needs© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 26
    • 27. How do they work? Two important architectural needs  Isolate code without language changes© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 27
    • 28. How do they work? Two important architectural needs  Avoid having to rewrite the entire Flash runtime© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 28
    • 29. How do they work? Solution  1 SWF = 1 thread  Separate instances of AVM  Code isolated in SWF© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 29
    • 30. How do they work? Conceptually, it’s like this…© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 30
    • 31. How do they work? Except like this© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 31
    • 32. What are Workers (redux)?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 32
    • 33. What are Workers (redux)? A separate AVM instance ActionScript object  Represents a container in which a SWF file executes  Yes, that includes non-concurrent apps too© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 33
    • 34. What are Workers (redux)?© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 34
    • 35. What are Workers (redux)? WorkerDomain© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 35
    • 36. What are Workers (redux)? WorkerDomain “Primordial” Worker© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 36
    • 37. What are Workers (redux)? WorkerDomain “Primordial” Worker public class App extends Sprite { // ... }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 37
    • 38. What are Workers (redux)? WorkerDomain “Primordial” Worker public class App extends Sprite { WorkerDomain.current.createWorker(); }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 38
    • 39. What are Workers (redux)? WorkerDomain “Primordial” Worker public class App extends Sprite { WorkerDomain.current.createWorker(); } “background” Worker public class BGWorker extends Sprite { // ... }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 39
    • 40. What are Workers (redux)? WorkerDomain “Primordial” Worker public class App extends Sprite { WorkerDomain.current.createWorker(); } “background” Worker public class BGWorker extends Sprite { // ... }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 40
    • 41. Caveats (part 1)© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 41
    • 42. Caveats (part 1) Desktop only – not available in mobile AIR (for now)© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 42
    • 43. How do I use a Worker? (in code please)© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 43
    • 44. How do I use a Worker? Step 1. Create the background worker SWF public class BGWorker extends Sprite { // ... }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 44
    • 45. How do I use a Worker? Step 2. Get the bytes of the SWF [Embed] [Embed(source="../swfs/BgWorker.swf", mimeType="application/octet-stream")] private static var BgWorker_ByteClass:Class; private function createWorker():void { var workerBytes:ByteArray = new BgWorker_ByteClass(); } Load var workerLoader:URLLoader = new URLLoader(); workerLoader.dataFormat = URLLoaderDataFormat.BINARY; workerLoader.addEventListener(Event.COMPLETE, loadComplete); workerLoader.load(new URLRequest("BgWorker.swf")); private function loadComplete(event:Event):void { var workerBytes:ByteArray = event.target.data as ByteArray; }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 45
    • 46. How do I use a Worker? Step 3. Create the Worker from the WorkerDomain [Embed(source="../swfs/BgWorker.swf", mimeType="application/octet-stream")] private static var BgWorker_ByteClass:Class; private var bgWorker:Worker; private function createWorker():void { var workerBytes:ByteArray = new BgWorker_ByteClass(); bgWorker = WorkerDomain.current.createWorker(workerBytes); }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 46
    • 47. How do I use a Worker? Step 4. Register for the workerState event and start the worker [Embed(source="../swfs/BgWorker.swf", mimeType="application/octet-stream")] private static var BgWorker_ByteClass:Class; private var bgWorker:Worker; private function createWorker():void { var workerBytes:ByteArray = new BgWorker_ByteClass(); bgWorker = WorkerDomain.current.createWorker(workerBytes); bgWorker.addEventListener(Event.WORKER_STATE, workerStateHandler); bgWorker.start() } private function workerStateHandler(event:Event):void { if (bgWorker.state == WorkerState.RUNNING) { // The worker is running -- send it a message or wait } }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 47
    • 48. Worker communication© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 48
    • 49. Worker communication Two new communication mechanisms  Worker shared properties  MessageChannel© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 49
    • 50. Worker communication Two new communication mechanisms  Worker shared properties  MessageChannel “Classic” techniques  Local shared objects  Write to/read from files  Server communication  SQLite database© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 50
    • 51. Worker communication Worker shared properties  “dictionary” of properties stored in a worker  String keys and * values  Available for reading and writing from within and outside the worker© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 51
    • 52. Worker communication Worker shared properties  “dictionary” of properties stored in a worker  String keys and * values  Available for reading and writing from within and outside the worker Setting shared property (e.g. in parent worker): bgWorker.setSharedProperty("propertyName", someObject); Reading shared property (e.g. in background worker): var receivedProperty:Object; receivedProperty = Worker.current.getSharedProperty("propertyName");© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 52
    • 53. Worker communication Worker shared properties  Available even before worker.start() is called Parent worker: private function createWorker():void { var workerBytes:ByteArray = new BgWorker_ByteClass(); bgWorker = WorkerDomain.current.createWorker(workerBytes); bgWorker.setSharedProperty("propertyName", someObject); bgWorker.addEventListener(Event.WORKER_STATE, workerStateHandler); bgWorker.start() } Background worker: public class BGWorker extends Sprite { public function BGWorker() { var receivedProperty:Object; receivedProperty = Worker.current.getSharedProperty("propertyName"); } }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 53
    • 54. Worker communication MessageChannel  One-way data-passing channel  One worker is the “sender,” another is the “receiver”  When a message is sent, MessageChannel dispatches an event© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 54
    • 55. Worker communication Step 1. Sending worker creates the MessageChannel Code in the sending worker: var sendChannel:MessageChannel; sendChannel = Worker.current.createMessageChannel(receivingWorker);© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 55
    • 56. Worker communication Step 2. Sending worker passes the MessageChannel to the receiver Code in the sending worker: var sendChannel:MessageChannel; sendChannel = Worker.current.createMessageChannel(receivingWorker); receivingWorker.setSharedProperty("incomingChannel", sendChannel);© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 56
    • 57. Worker communication Step 3. Receiving worker gets the MessageChannel object Code in the receiving worker: var incomingChannel:MessageChannel; incomingChannel = Worker.current.getSharedProperty("incomingChannel");© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 57
    • 58. Worker communication Step 4. Receiving worker listens for the channelMessage event Code in the receiving worker: var incomingChannel:MessageChannel; incomingChannel = Worker.current.getSharedProperty("incomingChannel"); incomingChannel.addEventListener(Event.CHANNEL_MESSAGE, handleIncomingMessage);© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 58
    • 59. Worker communication Step 5. Sending worker calls the MessageChannel’s send() method Code in the sending worker: var sendChannel:MessageChannel; sendChannel = Worker.current.createMessageChannel(receivingWorker); receivingWorker.setSharedProperty("incomingChannel", sendChannel); // ... sendChannel.send("This is a message");© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 59
    • 60. Worker communication Step 6. Receiving worker handles the channelMessage event Code in the receiving worker: var incomingChannel:MessageChannel; incomingChannel = Worker.current.getSharedProperty("incomingChannel"); incomingChannel.addEventListener(Event.CHANNEL_MESSAGE, handleIncomingMessage); private function handleIncomingMessage(event:Event):void { var message:String = incomingChannel.receive() as String; }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 60
    • 61. Worker communication Object passing rules© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 61
    • 62. Worker communication Object passing rules Objects passed to setSharedProperty() or MessageChannel.send() are…© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 62
    • 63. Worker communication Object passing rules Objects passed to setSharedProperty() or MessageChannel.send() are… …Serialized in AMF format by sending worker© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 63
    • 64. Worker communication Object passing rules Objects passed to setSharedProperty() or MessageChannel.send() are… …Serialized in AMF format by sending worker …Deserialized by receiving worker© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 64
    • 65. Worker communication Object passing rules Side effects of using AMF© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 65
    • 66. Worker communication Object passing rules Side effects of using AMF:  Object in receiver is a copy, not a reference© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 66
    • 67. Worker communication Object passing rules Side effects of using AMF:  Objects that can’t be serialized in AMF can’t be passed© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 67
    • 68. Worker communication Object passing rules Two exceptions:  Worker  MessageChannel© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 68
    • 69. Caveats (part 2)© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 69
    • 70. Caveats (part 2) Code execution order is no longer guaranteed (across workers) e.g. setting a breakpoint makes code nondeterministic© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 70
    • 71. Caveats (part 2) Performance impact “Adobe recommends that you do not use more than one or two background workers in a typical scenario.”© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 71
    • 72. “Crystal Ball”© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 72
    • 73. “Crystal Ball” Shareable ByteArray  Avoid AMF overhead  Especially useful when data is binary by nature (e.g. BitmapData)© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 73
    • 74. “Crystal Ball” Mutex  “Mutually exclusive” access to shared resource lock() tryLock() unlock()© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 74
    • 75. “Crystal Ball” Mobile AIR© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 75
    • 76. “Crystal Ball” Language features Warning: totally made up pseudocode: public class MyClass { public function doSomething():void { // do some stuff in the main thread backgroundThread { // do something in the background // using variables/methods that are in scope } // do more in the main thread } }© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 76
    • 77. Thank you and Q&A© 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. 77
    • 78. © 2012 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

    ×