4. Sync
my $ua = LWP::UserAgent−>new;
my $response = $ua->get('http://search.cpan.org/');
# blocks until request end
if ($response->is_success) {
# do something ...
}
12年12月3日月曜日
5. Async
var req = new XMLHttpRequest();
req.onload = function() {
// do something...
};
req.open("GET", "http://example.com");
req.send();
// runs without waiting HTTP request
12年12月3日月曜日
20. Execution model of
Android / iOS
red => Code on Main Thread
yellow => Code on Other Threads
Main Thread Other Threads
12年12月3日月曜日
21. Android (AsyncTask)
class ATask extends AsyncTask<String,Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
// run on background thread, do heavy tasks
}
@Override
protected void onPostExecute(Boolean result) {
// run on main thread, update UI
}
}
12年12月3日月曜日
22. iOS (GCD)
// main thread
dispatch_queue_t = dispatch_get_global_queue(...);
dispatch_async(queue, ^{
// run on background thread, do heavy tasks
dispatch_async(dispatch_get_main_queue(), ^{
// run on main thread, update UI
});
});
12年12月3日月曜日
24. Execution model of
Android / iOS (again)
red => Code on Main Thread
yellow => Code on Other Threads
blue => UI Events
Main Thread Other Threads
async call
← Sync HTTP req
← req finished
callback
12年12月3日月曜日
25. And more...
red => Code on Main Thread
yellow => Code on Other Threads
blue => UI Events
Main Thread Other Threads
async call
← Sync HTTP req
← Sync file read
← Sync DB read
← Sync HTTP req
callback
12年12月3日月曜日
26. 分かること
• Main Threadのblockを防ぐだけなら、全て
のI/Fが非同期である必要は全くない
• 適切にThreadの境界を跨ぐための仕組み
が必要
12年12月3日月曜日
27. JS future?
red => Code on Main Thread
yellow => Code on Other Threads
blue => UI Events
Main Thread Web Workers
async call
← Sync HTTP req
← Sync file read
← Sync DB read
←Heavy calculation
callback
12年12月3日月曜日
28. “Heavy” APIs
• synchronous XHR (supported)
• localStorage has only synchronous API
• synchronous WebSQL (Web Worker only)
• synchronous IndexedDB (Web Worker
only?)
12年12月3日月曜日