Combing thread management concepts in Java and java thread concepts

Source: Internet
Author: User

Combing thread management concepts in Java and java thread concepts

In Java, "Thread" refers to java. lang. the Thread pool is ThreadPoolExecutor and ScheduledThreadPoolExecutor. A Thread pool with a fixed Thread ceiling should be used.

When static methods are modified with synchronized, the calls of any two different threads are mutually exclusive. When modifying a member function, the multi-thread method calls of the same object are mutually exclusive. Of course, the parameter after synchronized can be any object. Synchronized ensures the visibility of variables in synchronized blocks, while volatile ensures the visibility of modified variables. ReentrantLock is java. util. concurrent. A class in locks, similar to synchronized usage, but needs to be displayed unlocked. It provides ReentrantReadWriteLock, which is mainly used in scenarios where multiple reads and writes are less and no mutex is required for reads.

Classes starting with atomic in the Java. util. concurrent. Atomic package provide some atomic operations. The reason for the performance improvement is that the CAS commands supported by hardware are used internally through JNI.


Three essential methods for Object: wait, policy, and policyall. Nofity wakes up a waiting thread, and policyall wakes up all waiting threads. Wait is usually embedded in a loop to determine whether the relevant data status has reached expectations. If not, the Wait continues to Wait, mainly to prevent false wakeup.

 

Java. util. in concurrent, CountDownLatch provides a mechanism to trigger an event when multiple threads reach the expected state or finish the expected work. Other threads can wait for this event to trigger their subsequent work. CyclicBarrier can work with multiple threads to wait until all threads reach this barrier. wait.

 

Both CountDownLatch and CyclicBarrier are used for thread coordination. The main difference is that CountDownLatch executes latch in multiple threads. the countDown trigger event, wake up the wait thread on latch, and the thread executing countDown will continue the work of its own thread after the countDown is executed; the CyclicBarrier is a fence, it is used to synchronize all threads that call the wait method, and when all threads reach the wait method, these threads will return their respective work together. In addition, CountDownLatch cannot be used cyclically, and CyclicBarrier can be used cyclically.


Semaphore is used to manage semaphores. When a semaphores object is constructed, the number of incoming signals controls the number of concurrent requests. Obtain the signal through acquire before execution, and return it through release after execution. If there is no available signal, the aquire call will be blocked. Compared with the number of threads to control the number of concurrent threads, semaphore controls the number of concurrent threads in a finer granularity.

 

Exchanger is used for data exchange between two threads. The thread will block the exchange method in the Exchanger until another thread reaches the exchange method of the same Exchanger, the two exchange, and then the two threads will continue to execute their own related code.

 

Asynchronous call implementation methods are commonly used: future and callback functions. The called method will return a future object, and then perform its own processing. The actual return value will be obtained through future. get. FutureTask is a specific implementation class of future. The submit method of ThreadPoolExecutor returns the specific implementation of a FutureTask. FutureTask helps implement specific task execution and association with methods such as get in the future interface. FutureTask helps ThreadPoolExecutor to implement future support for tasks added to the thread pool, and also enables us to schedule tasks that support future.

 

Lock mutex can easily achieve thread security, but the cost is to reduce the performance. Concurrent containers not only pursue thread security, but also consider concurrency to Improve the Performance of containers in the concurrent environment. Representative containers starting with CopyOnWrite and Concurrent.

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.