Flying Over Interviewer (2)-JUC

    Hello everyone! I am the only official designated urinal without dandruff in this number- afraid of fart forest .

    What is JUC? I believe that many little friends who are still inexperienced will be ignorant. Me too. I can read all three letters. I do n’t know what I ’m talking about. In fact, if I write down the full name of it, "java.util.concurrent "This is very clear. The contents of this jar package are atomic class, volatile, cas, concurrentHashMap, CountDownLatch, CyclicBarrier, Semaphore, implementation of Callable interface to create threads, ReentrantLock synchronization lock, ReadWriteLock read-write lock, thread pool.

        The categories under the JUC listed above are almost all the hardest hit areas for interviews. It is strange not to ask.

        Does ReentrantLock understand? Ok. The function is as the name suggests, it is a lock, and it is reentrant. This is similar to synchriozed. It acts on code snippets, and can be used to compete for locks through the lock method. Of course, it also provides a form of sequential locking. This synchriozed cannot be done. Its underlying principle is AQS. The unlock method must use the unlock method, otherwise it will always block. Change the code judgment, and also provide some methods for sensing the status of the lock and responding to interrupts, and can also wake up the specified thread through condition.

        You say AQS ? Yes, it is located under the java.util.concurrent.locks package. AQS is a framework for building locks and synchronizers, such as ReentrantLock and Semaphore. The principle is that the requested shared resource is idle, so the currently idle thread is It will be marked as running, and the resource will also be marked as locked. A mechanism for thread blocking and waiting and lock allocation when awakened is required. AQS uses CLH queue lock (spin lock, first-come-first-served to ensure no hunger). , The thread that will not obtain the lock temporarily is placed in the queue.

        Simply talk about CAS ! The full name is compare and swap. By saving an old value, the old value is used to compare the current value when updating. If there is no change, the updated value is updated. Like atomicInteger is to use volatile to modify the value, and then use the cas method mentioned above to do it.

        How does concurrentHashMap ensure thread safety? Unlike Hashtable, concurrentHashMap does not lock the entire table. JDK7 and before did a segment lock. The table was divided into 16 segments, and each segment was locked separately. Inside each segment was an array that would not interfere with each other. In JDK8 and later, the method of segmented lock becomes CAS plus synchroized, and the array in the segment will become a red-black tree after more than 8 arrays.

        What is the role of Volatil e ? It can guarantee that each thread can get the latest value, or the value read from the main memory. It can also be used to prevent instruction reordering. Like a variable initialization, the first step is to open up memory, the second step is to assign memory, and the third step is to point to a memory reference. The second and third steps may or may not necessarily depend on the JVM, and this step will not be replaced after volatile is used.

        Use too many threads? According to my understanding, the use of thread pool and CountDownLatch and the like, that is, use too many threads. Here to talk about ContDownLatch, it can be used to control the start of a specified number of threads, mark the completion of the call by calling the countDown method, and finally call the await method, blocking waiting for the last thread to complete execution. This can be used in applications that require multiple requests to be executed simultaneously.

        In addition to CountDownLatch, there is also a CycliBarrier fence. Fence is a thread control scheme that is further than CountDownLatch. The specified number of threads can be executed to a certain number before the next step can be performed. Just like, the teacher arranged homework, and then everyone started to do it. After finishing the work, they handed in the homework. The teacher waited for the last student to hand in the homework, and then the teacher took all the homework written and divided it up for each student. After all the changes were made, everyone handed them in again. After the teacher had collected all the homework, he could turn over the test paper to find the highest score, and then let the students send it down. After the last test paper was sent out, the matter was over. Too.

        Other advanced tools like semaphore Semaphore and Phaser phase shifter, feel Semaphore and thread pool are similar, Phaser and fence are similar.

        Thread Pool? Can you tell me why you should use a thread pool? Using pooling technology, it is good! The first one reduces the overhead of creating and destroying threads, which is conducive to reuse. The second task can run without waiting for the creation thread. The third task is to manage, tune, and monitor the running of threads. Here it is necessary to mention the use of thread pools. Java provides four built-in methods for creating thread pools, although they are not considered as good by the Ali specification and avoid OOM. There is newFixedThreadExecutor, but there is a possibility that the waiting queue is too large to cause OOM, ScheduleThreadExecutor, newSingleThreadExecutor, newCacheThreadExecutor, and there is a risk of OOM due to the creation of too large threads. At this time, only the custom thread pool can be used. The custom thread pool is created by ThreadPoolExecutor. The parameters include the number of core threads, the maximum number of threads, the waiting time of threads other than the number of core threads destroyed, waiting queue, thread factory, rejection strategy . Among them, the rejection strategy includes directly throwing an exception, discarding the oldest, directly discarding, and executing by the caller.

        The thread pool can execute threads that implement the Runable and Callable interfaces. The execute method is used when the result is not needed. The submit method is executed when the result is needed. The returned result should be used in conjunction with Future. When the execution result is obtained with get, it will block. Thread until execution is complete.

        supplement:

        Talk about Java's exception ? They are all subclasses of Throwable, and Error basically means that the program hangs up, and manual intervention is required. Exception is also divided into runtime and non-runtime exceptions. Non-runtime exceptions require catch processing, such as IO operations. Runtime exceptions, such as subscript out of bounds, null pointer, class does not exist, method does not exist, number format is abnormal.

 

The first version of the Java interview knowledge point summary download (with many typos and no updates): https://pan.baidu.com/s/1MxKXIZtoBd57pTwTIDyrgA extraction code: 3arb.

 

Related Reading:

Flying over the interviewer (1)-Java foundation

Heavy! Summary of 20,000-word Java interview knowledge points

 

Guess you like

Origin www.cnblogs.com/ljy-1471914707/p/12751055.html