In Java 5.0, the java.util.Concurrent (JUC) package adds utility classes commonly used in concurrent programming to define custom thread-like subsystems, including thread pools, asynchronous IO, and lightweight task frameworks. The following changsha Java training institutions to share with you high concurrent programming JUC package interview questions and answers:

(1) multi-thread and single thread difference and connection answer: in single-core CPU, the CPU is divided into a very small time slice, in each moment can only have a thread in execution, is a microscopic take turns CPU mechanism. Multithreading involves thread context switching, which can slow down the execution of a program, meaning that a two-threaded process takes more time to execute than a one-threaded process takes twice. Conclusion: The use of multithreading will not improve the execution speed of the program, but will reduce the speed, but for users, can reduce the response time of users.

(2) How to specify the execution order of multiple threads? How do I get 10 threads to print 0123456789 in sequence? A: Create an orderNum. After each thread completes execution, update orderNum to indicate the next thread to execute and wake up all waiting threads. At the beginning of each thread, while determines if orderNum is equal to its required value! If no, wait. If yes, the thread is executed.

A: process is a “process in execution”, is the system for resource allocation and scheduling of an independent unit. A thread is an entity of a process. There are multiple threads in a process that share address space and other resources between threads (so it is easier to communicate and synchronize threads than processes). Thread context switching is much faster than process context switching: The process switchover involves saving the CPU environment of the current process and setting the CPU environment of the new process to be scheduled. Thread switching requires only a small amount of register content to be saved and set, and does not involve storage management.

(4) What are the four necessary conditions for multiple threads to produce deadlocks? A: incompatible conditions: a can be used by one thread at a time resource request with maintaining conditions: a thread blocked by request resources, stay put for acquired resources not deprived conditions: process has acquired resources, before has not used up, can’t forcibly deprived of circular wait conditions: formed between a number of threads a relationship of end of cycle waiting for resources by q: How do I avoid deadlocks? A: Specify the order in which locks are acquired. For example, A thread can operate on A resource only when it obtains locks A and B. How to avoid deadlocks in multi-threaded conditions? The order in which locks are acquired is certain. For example, only the thread that acquires lock A is eligible to acquire lock B. Acquiring locks sequentially can avoid deadlocks!

(5) What is the difference between sleep() and wait(n) and wait()? A: The sleep() method is a static method of the Thread class. The current Thread will sleep for n milliseconds, and the Thread will be blocked. When it is time to sleep, the block is unblocked and the CPU is in a runnable state waiting for it to arrive (sleep does not release the lock, if any). Wait () : an Object method that must be used with synchronized. The thread blocks and is released when notify or NotifyAll is invoked. However, the mutex will not be runnable until it is reoccupied. While sleeping, the mutex is released.

(6) What is synchronized? A: Underlying implementation: On entry, monitorenter is executed, counter +1 is released, counter −1 is released; When a thread determines that the counter is 0, the current lock is free and can be occupied. Otherwise, the current thread enters the wait state. Meaning :(monitor mechanism) Synchronized is an object lock. Object lock is a kind of weight lock (Monitor). Synchronized lock mechanism has partial lock (single thread), light lock (multiple threads access Synchronized area), object lock (weight lock, multiple threads compete), spin lock, etc. This keyword is an encapsulation of several locks.

(7) Volatile keyword A: This keyword guarantees visibility but not atomicity. Main memory and working memory, directly interact with main memory, read and write operations, ensure visibility prohibit instruction reorder by the JVM: for instructions reorder, see DCL double lock failure.

A: Basic data types can be added and subtracted atomically. See my blog for details on the use of AtomicInteger under the concurrent. Atomic package.