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.