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();
}