Advertisement
Guest User

Untitled

a guest
Jan 30th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // multiprocess
  4. //
  5. // Created by Alexander Kolov on 30.1.15.
  6. // Copyright (c) 2015 Alexander Kolov. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.  
  13. @IBOutlet weak var button: UIButton!
  14.  
  15. var lockQueue = dispatch_queue_create("lockQueue", nil)
  16.  
  17. lazy var queue: NSOperationQueue = {
  18. let queue = NSOperationQueue()
  19. queue.maxConcurrentOperationCount = 10
  20. return queue
  21. }()
  22.  
  23. lazy var memory: [[String]] = {
  24. var memory = [[String]]()
  25. return memory
  26. }()
  27.  
  28. override func viewDidLoad() {
  29. super.viewDidLoad()
  30. // Do any additional setup after loading the view, typically from a nib.
  31. }
  32.  
  33. override func didReceiveMemoryWarning() {
  34. super.didReceiveMemoryWarning()
  35. // Dispose of any resources that can be recreated.
  36. }
  37.  
  38. @IBAction func didTapButton(sender: UIButton) {
  39.  
  40. var operations = [NSBlockOperation]()
  41.  
  42. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
  43. for i in 0...10 {
  44. let operation = NSBlockOperation() {
  45. var arr = [String]()
  46.  
  47. for a in UnicodeScalar("a").value...UnicodeScalar("z").value {
  48. arr.append(String(format: "%c", a))
  49. }
  50.  
  51. dispatch_sync(self.lockQueue) {
  52. self.memory.append(arr)
  53. }
  54. }
  55.  
  56. operations.append(operation)
  57. }
  58.  
  59. self.queue.addOperations(operations, waitUntilFinished: true)
  60. println("\(self.memory)")
  61. }
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement