LoginSignup
1
2

More than 5 years have passed since last update.

Android と iOS の、複数の非同期処理の完了を待つ処理

Last updated at Posted at 2017-07-05

はい

クラス

Android iOS
java.util.concurrent.CountDownLatch DispatchSemaphore

CountDownLatch
public void Test() {

    int max = 10;

    CountDownLatch count = new CountDownLatch(max);

    for (int i=0; i < max; ++i) {
        new Thread() {
            public void run() {
                Log.d("Hoge", "count = " + i);
                count.countDown();
            }
        }.start();
    }

    count.await();

    Log.d("Hoge", "finish");
}
DispatchSemaphore
func Test() {

    let max:  Int = 10;

    let sema: DispatchSemaphore = DispatchSemaphore(value: 0) // <- 注意

    for i: Int 0..<10 {
        DispatchQueue.main.async {
            print("count =", i)
            sema.signal()
        }
    }

    for _ in 0..<max { sem.wait() } // <- はい

    print("finish")
}

動作確認

してない

キーワード

排他制御

^ν^「こうやったほうがいいよ!」

きいてないです

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2