简体   繁体   中英

dispatch_queue name and thread

there's a button method :

-(void) buttonTapped:(id) sender{
    dispatch_async(dispatch_queue_create("countQueue", NULL), ^{
        sleep(10);
        NSLog(@"%d",self.count++);
    });
}

self.count is property:

@property (nonatomic, assign) int count;

I want to know if I create the "countQueue" many times by click the button ,

  1. how many threads is running for processing task of countQueue?
  2. because the block need to run in 10 seconds, if I click the button every 2 seconds, then it will create a new queue, how the system process the previous task? just cancel it?
  • Each call to dispatch_queue_create() creates a new queue , even if a queue with the same label already exists. The label is only meant as a tool to identify the queue during debugging. Arbitrary many queues with the same label can exist at the same time.
  • Since you don't keep a strong reference to the queue, it is destroyed automatically after all blocks dispatched to the queue have finished.
  • How the system allocates threads to queues is an implementation detail of GCD.
  • In your case, it might be easier to use dispatch_after() .

Edit: After reading your comment, I assume that there is a better solution to your problem, such as updating the property if the image download has actually finished. How to do this depends on the method used for the download. One example would be the completion handler of sendAsynchronousRequest:queue:completionHandler: of NSURLConnection .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM