Swift多线程

来源: 浏览: 评论:0


1:第一种多线程  func fun1(){  for i in 200 300{  NSLog("%d",i);  }   } 

1:第一种多线程

  func fun1(){

  for i in 200...300{

  NSLog("%d",i);

  }  

   }

  func fun2(){

  for i in 300...400{

  NSLog("%d",i);

  }

  }

  var th1 = NSThread(target:self,selector:"fun1",object:nil);

  th1.start();

  //开启线程

  NSThread.detachNewThreadSelector("fun2",toTarget:self,withObject:nil);

 

第二种创建线程池:

  var queue = NSOperationQueue();

  queue.maxConcurrentOperationCount = 1;

  queue.addOperationWithBlock({

  for i in 400...500{

  NSLog("%d",i);

  }

  var op = NSInvocationOperation (target:self,selector:"fun1",object:nil);

  var op1 = NSInvocationOperation(target:self,selector:"fun2",object:nil);

  queue.addOperation(op);

  queue.addOperation(op1);

  })

 

第三种GCD创建多线程

  

        var queue = dispatch_queue_create("test", nil);

        dispatch_async(queue, {

            for i in 0...100{

                NSLog("异常%d", i);

            }

            dispatch_sync(dispatch_get_main_queue(), {

                NSLog("是否是主线程(NSThread.isMainThread())");

                })

            })

Swift多线程


上一篇:listview 设置数组为空
下一篇:java中String new和直接赋值的区别

 站点咨询
 站点推荐