Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlinのcoroutine、async/awaitと同じでしょ?って思ってたけど意外と洗...

takuya kikuchi
October 25, 2019
89

 Kotlinのcoroutine、async/awaitと同じでしょ?って思ってたけど意外と洗練されててすごいなぁって思った話をさせてほしい

2019-10-25 VALU社内LT資料です

takuya kikuchi

October 25, 2019
Tweet

More Decks by takuya kikuchi

Transcript

  1. BTZODBXBJU $ private static async Task<int> HeavyWork() { // ͳΜ͔͍͢͝ॏ͍ͨॲཧ

    await Task.Delay(1000); Console.WriteLine("done!"); return 0; } static void Main(string[] args) { Task.Run(async () => { await HeavyWork(); await HeavyWork(); await HeavyWork(); }).Wait(); } > done! done! done!
  2. ,PUMJODPSPVUJOF suspend fun heavyWork(): Int { /// ॏ͍ॲཧ sleep(1000) println("done!")

    return 0 } fun main() { GlobalScope.launch { heavyWork() heavyWork() heavyWork() } } > done! done! done!
  3. ݟൺ΂ͯΈΔ private static async Task<int> HeavyWork() { // ͳΜ͔͍͢͝ॏ͍ͨॲཧ await

    Task.Delay(1000); Console.WriteLine("done!"); return 0; } static void Main(string[] args) { Task.Run(async () => { await HeavyWork(); await HeavyWork(); await HeavyWork(); }).Wait(); } suspend fun heavyWork(): Int { /// ॏ͍ॲཧ sleep(1000) println("done!") return 0 } fun main() { GlobalScope.launch { heavyWork() heavyWork() heavyWork() } }
  4. ݟൺ΂ͯΈΔ private static async Task<int> HeavyWork() { // ͳΜ͔͍͢͝ॏ͍ͨॲཧ await

    Task.Delay(1000); Console.WriteLine("done!"); return 0; } static void Main(string[] args) { Task.Run(async () => { await HeavyWork(); await HeavyWork(); await HeavyWork(); }).Wait(); } suspend fun heavyWork(): Int { /// ॏ͍ॲཧ sleep(1000) println("done!") return 0 } fun main() { GlobalScope.launch { heavyWork() heavyWork() heavyWork() } }
  5. ͬ͘͟Γͨ͠ϧʔϧ w BTZODBXBJUʢ$ʣ w ඇಉظ࣮ߦ͍ͨؔ͠਺͸5BTL5Λฦ͢Α͏ʹ࣮૷͢Δ w BTZODؔ਺͸ඞͣ5BTL5ܕΛฦ͢ w BTZODؔ਺಺ͰͷΈɺ5BTL5ΛBXBJUՄೳ w

    BXBJU͠ͳ͔ͬͨ৔߹͸ɺ'JSF'PSHFU w DPSPVUJOF LPUMJO  w ඇಉظ࣮ߦ͍ͨؔ͠਺͸TVTQFOEGVODUJPOʢதஅؔ਺ʣͱͯ͠ఆٛ͢Δ w TVTQFOEGVODUJPOͷ໭Γ஋ͷܕʹറΓ͸ͳ͠ w TVTQFOEGVODUJPO͸ɺTVTQFOEGVODUJPO಺෦ͰͷΈݺͼग़ͤΔ private static async Task<int> HeavyWork() { // ͳΜ͔͍͢͝ॏ͍ͨॲཧ await Task.Delay(1000); Console.WriteLine("done!"); return 0; } static void Main(string[] args) { Task.Run(async () => { await HeavyWork(); await HeavyWork(); await HeavyWork(); }).Wait(); } suspend fun heavyWork(): Int { /// ॏ͍ॲཧ sleep(1000) println("done!") return 0 } fun main() { GlobalScope.launch { heavyWork() heavyWork() heavyWork() } }
  6. BTZODBXBJUͰ͋Γ͕ͪͳࣄނ w BXBJU͠๨Ε w BTZODؔ਺֎Ͱ΋ɺBXBJUͳ͠ͳΒݺ΂ͪΌ͏ w ҙຯΛΑ͘ཧղͤͣʹίʔυॻ͍ͯͨΓ͢Δͱ΍Γ͕ͪ w େ఍ࣄނΔ static

    void Main(string[] args) { Task.Run(async () => { HeavyWork(); HeavyWork(); HeavyWork(); }).Wait(); } static void Main(string[] args) { HeavyWork(); HeavyWork(); HeavyWork(); }