-1

I started to learn programming for iOS and I want to find out how to implement next Java functions with threads in Swift, and also in Objective-C (in my app I have some code using this language too)

Java threads (use it for Android apps)

1) Wait for two or more threads to finished

final CountDownLatch latch = new CountDownLatch(2);

// start thread #1
new Thread(new Runnable() {
    @Override
    public void run() {
        // do some work
        latch.countDown();
    }
}).start();

// start thread #2
new Thread(new Runnable() {
    @Override
    public void run() {
        // do some work
        latch.countDown();
    }
}).start();

// wait for 2 threads to end (finish)
try {
    latch.await();
} catch (InterruptedException e) {
    //
}
// two threads are finished 
// do other work

2) Start new thread only if previous one was finished at this time or any thread wasn't started yet (mThread == null)

private Thread mThread = null; // class field

.

// check if thread has completed execution
if ((mThread != null && mThread.getState() == Thread.State.TERMINATED)
    || mThread == null) {

    mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            // do some work
        }
    });
    mThread.start();
}
user924
  • 8,146
  • 7
  • 57
  • 139
  • 1
    Have you checked the documentation? https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html – LorenzOliveto Mar 07 '18 at 09:52

2 Answers2

0

For 2, I would use serial queue:

dispatch_queue_t myQueue;

And implement my queue as of below:

myQueue = dispatch_queue_create("my_serial_queue", DISPATCH_QUEUE_SERIAL);

dispatch_async(myQueue, ^{
    for (int i = 0; i < 10000; i++) {
        usleep(100);
    }
    NSLog(@"Task 1 done");
});

dispatch_async(myQueue, ^{
    NSLog(@"Task 2 done");
});
RyanB
  • 1,287
  • 1
  • 9
  • 28
  • those task are called on the same thread and `Task 2 done` is printed after `Task 1 done`, my java example shows implementation of two different/separate threads – user924 Mar 12 '18 at 11:52
  • No, not really. In GCD even though you dispatch 2 task blocks onto a same queue, there still be no guarantee that your 2 blocks will run on the same thread, except main queue. – RyanB Apr 13 '18 at 07:55
-1

my answer for the first (1) solution (wait until all threads finish its execution):

dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
    // block1
    NSLog(@"Task Block1");
    [NSThread sleepForTimeInterval:6.0];
    NSLog(@"Task Block1 End");
});


dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
    // block2
    NSLog(@"Task Block2");
    [NSThread sleepForTimeInterval:5.0];
    NSLog(@"Task Block2 End");
});


dispatch_group_wait(group, DISPATCH_TIME_FOREVER); // blocks current thread

for second solution (2)

I didn't find for GCD method like isRunning, so I just use Boolean variable

before starting async task set variable isRunning to true ... ... when async task is finished we can use next callback:

dispatch_group_notify(_signInitThread, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
    isRunning = false;
});
user924
  • 8,146
  • 7
  • 57
  • 139