Thread management concept grooming in Java

Source: Internet
Author: User

in the Java in which " Threads " means Java.lang.Thread an instance of the class and the execution of the thread, The main thread pool used is threadpoolexecutor and scheduledthreadpoolexecutor, to use a fixed thread cap.

When modifying a static method with synchronized, the invocation of any two different threads is mutually exclusive, and when the member function is decorated, the multi-threaded method invocation of the same object is mutually exclusive; Of course, the argument after synchronized can be any object. Synchronized guarantees the visibility of variables in the synchronized block, while volatile guarantees the visibility of the modified variable. Reentrantlock is a class in java.util.concurrent.locks that is similar to synchronized usage but needs to be displayed for unlocking, which provides reentrantreadwritelock, primarily for scenarios where there is less read and no mutex to read.

Classes that start with atomic in the Java.util.concurrent.atomic package provide some related atomic operations, and the performance increase is due to the internal use of the hardware-supported CAS directives in the form of JNI.


Three required methods on object objects: Wait, notify, Notifyall. Nofity wakes up a waiting thread, Notifyall wakes up all the waiting threads. The use of wait is generally embedded in a loop, to determine whether the relevant data state is expected, and if not, to continue to wait, mainly to prevent false wake.

The mechanism provided by Countdownlatch in Java.util.concurrent is to trigger events when multiple threads have reached the expected state or to complete the expected work, and other threads can wait for the event to trigger their own subsequent work. Cyclicbarrier can collaborate on multiple threads, allowing multiple threads to wait before mybarrier.wait () until all the threads have reached the barrier, and then perform the subsequent actions together.

Countdownlatch and cyclicbarrier are used for thread coordination, The main differences are: Countdownlatch triggers events after multiple threads have executed latch.countdown, wakes wait threads on latch, and the thread that executes countdown continues to work on its own thread after the countdown is done; cyclicbarrier is a fence that synchronizes all the threads that call the wait method, and when all the threads have reached the wait method, the lines friend together to return to their respective work. In addition, the countdownlatch cannot be recycled, and cyclicbarrier can be used for recycling.


Semaphore is used to manage semaphores, and the number of incoming signals when the semaphore object is constructed is the amount of control concurrency. The signal is obtained by acquire before execution and returned by release after execution, and if no signal is available, the Aquire call is blocked. The control granularity of the semaphore control concurrency is finer than controlling the number of threads to control the number of concurrent numbers.

The exchanger is used to exchange data between two threads. The thread blocks the Exchange method in exchanger until another thread is switched to the same exchanger exchange method, and then two threads continue to execute their own related code.

The way asynchronous calls are implemented is commonly used: future and callback functions. The called method returns a future object and then proceeds to its own processing, followed by Future.get () to get the true return value. Futuretask is a concrete implementation class for the future. Threadpoolexecutor's Submit method returns a concrete implementation of a futuretask. Futuretask helps implement specific task execution and association with methods such as Get in the future interface. Futuretask helps Threadpoolexecutor achieve future support for joining thread pool tasks and enables us to implement task scheduling that supports the future.

Locking mutex facilitates thread safety, but at the cost of reducing performance, while concurrent containers not only pursue thread safety, but also consider concurrency to improve the performance of containers in concurrent environments. The comparison is representative of several containers beginning with copyonwrite and concurrent.


Thread management concept grooming in Java

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.