Presentation is loading. Please wait.

Presentation is loading. Please wait.

Asynchronous Programming CS Programming Languages for Web Applications

Similar presentations


Presentation on theme: "Asynchronous Programming CS Programming Languages for Web Applications"— Presentation transcript:

1 Asynchronous Programming CS 4640 Programming Languages for Web Applications
[Joshua Bloch, “Effective Java” Robert W. Sebesta, “Programming the World Wide Web Based in part on GMU SWE 432 by Jeff Offutt, Thomas LaToza, and Jon Bell]

2 Asynchronous Programming
Allow multiple things to happen at the same time An app is doing more than one thing at a time Maintain an interactive application while still doing something Processing data Communicating with remote servers/hosts Use threads A thread = a running program whose execution may be interleaved with other programs by the operating system Program execution = a series of sequential method calls

3 Multi-Threading Allow more than one things to be run at once (i.e., asynchronous) Typically handled by multiple OS scheduler Everything (you write) will run in a single thread (i.e., event loop) Event loop processes events and calls the callback functions Program execution = a series of sequential method calls event queue thread1 thread2 thread3 threadn event loop

4 Event Loop Event queue Response from google.com
thread1 thread2 thread3 threadn queue Event queue Program execution = a series of sequential method calls Response from google.com Response from twitter.com Response from uva.com

5 Event Loop Event queue Event being processed Response from twitter.com
thread1 thread2 thread3 threadn queue Event queue Program execution = a series of sequential method calls Response from twitter.com Response from uva.com Event being processed Response from google.com Are there any listeners registered for this event? If so, call listener with event. After the listener is finished, repeat

6 Event Loop Event queue Event being processed Response from uva.com
thread1 thread2 thread3 threadn queue Event queue Program execution = a series of sequential method calls Response from uva.com Event being processed Response from twitter.com Are there any listeners registered for this event? If so, call listener with event. After the listener is finished, repeat

7 Writing Good Event Handler
Events are processed in the order they are received Events may arrive in unexpected order The next event will not be handled until your event handler finishes (“run-to-completion”) Event must not block (stall or wait for inputs such as alert() or non-asynchronous request) If something that takes a long time must be done, split it up into multiple events


Download ppt "Asynchronous Programming CS Programming Languages for Web Applications"

Similar presentations


Ads by Google