So stornieren Sie eine von vielen threads in swift

Gut .. ich bin in der situation wie dieser: ich habe eine Funktion, die beginnt 3 asynchrone threads. Jeder thread etwas tun, welche einige Zeit in Anspruch nehmen. Wenn einige thread zuerst fertig zu werden, ich brauche es halt die anderen 2, aber ich weiß nicht wie es geht (noch).

Mein code:

    class SomeController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //Do any additional setup after loading the view.
    threads();
}

func threads(){

    let operationQueue = NSOperationQueue();

    operationQueue.addOperationWithBlock(
        {
            let thread = NSThread.currentThread();
            let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue;
            NSThread.sleepForTimeInterval(30);

            println("Task #1 completed on thread #\(threadNumber)");

    })

    operationQueue.addOperationWithBlock(
        {
            let thread = NSThread.currentThread();
            let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue;

            NSThread.sleepForTimeInterval(20);
            println("Task #2 completed on thread #\(threadNumber)");

    })

    operationQueue.addOperationWithBlock(
        {
            let thread = NSThread.currentThread();
            let threadNumber = thread.valueForKeyPath("private.seqNum").integerValue;

            NSThread.sleepForTimeInterval(5);
            println("Task #3 completed on thread #\(threadNumber)");
    })
}}

Jede Rasche Hilfe oder Beratung wird apperciated 🙂

Schreibe einen Kommentar