Analysis of ObjectC's GCD (Grand Central Dispatch) mechanism

Source: Internet
Author: User
Tags gcd

GCD mechanism vs JAVA Thread pool
1. GCD is easier to execute than JAVA
ObjectC GCD: executable code snippet (code block)

The code is as follows: Copy code

Dispatch_async (queue, ^ {
NSLOG (@ "hello, word ");
});
 

JAVA: must be an object (you can use an anonymous class)

The code is as follows: Copy code

Pool. submit (new Runnable (){
Public void run (){
System. out. println ("hello, world ");
 }
});
 

2. GCD does not need to care about thread creation and release.
The GCD mechanism is more silly, so you don't have to worry about when the thread is recommended and when it is released. You just need to use it. The problem of creating and releasing is solved in the running environment.
JAVA needs to establish different thread pools by itself, but also care about the maximum number of threads. It must be manually disabled (otherwise, the maximum number of threads will exceed ).

3. GCD thread mechanism is more efficient
The GCD mechanism thread can be said to be inherent. Dealing with programmers is a queue, where the thread hides behind the queue and executes the code snippets in the queue. When the thread is not enough, the system schedules and creates new threads on its own. Programmers do not know about all this. Thread creation and destruction can be globally scheduled to reduce unnecessary overhead.
JAVA Multithreading mechanism. The thread pool is roughly equivalent to the queue of GCD. The difference is that the threads in each thread pool are independent and cannot be used by each other. This requires programmers to plan the thread size of each pool and establish and destroy policies. When the system size is slightly larger, a large number of thread pools may exist in the same JVM, when thread A pool is busy, Thread B pool may be very idle. However, thread A pool cannot use threads in thread B pool, leading to A high frequency of thread creation and destruction.

4. Conclusion
GCD is better than JAVA's multi-threaded mechanism in terms of performance, simplicity, and usability. It is hoped that the JAVA virtual machine can introduce the GCD feature in a specific version.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.