Q: What classes are used in the concurrent package for Weibo short chain projects and what are their purposes? What are the scenarios? Effect?
- Java. util. concurrent. atomic. AtomicInteger (AtomicLong)
Purpose: It can be updated in atomic mode.intValue. For more information about attributes of atomic variables, seejava.util.concurrent.atomicPackage specification.AtomicIntegerAvailable in applications (such as counters added in atomic mode) and cannot be replacedInteger. However, this class does expandNumberAllows uniform access to tools and utilities that process digital data. (Jdk Documentation)
Scenario: ThreadPoolExecutor member variable, which records the number of waiting threads and ensures that the maximum number of threads is not exceeded.
Effect: normal, not exceeding
- Java. util. concurrent. ThreadPoolExecutor
Purpose: OneExecutorServiceIt uses one of several possible pool threads to execute each submitted task, usually usingExecutorsFactory method configuration.
The thread pool can solve two different problems: because it reduces the overhead of each task call, they can generally provide enhanced performance when executing a large number of asynchronous tasks, you can also bind and manage resources (including the threads used to execute the task set. EachThreadPoolExecutorSome basic statistics, such as the number of completed tasks, are also maintained. (Jdk Documentation)
Scenario: Start high-school and low-priority executors, set different maximum threads and maximum waiting numbers to execute different levels of tasks. For example, asynchronous repair of short-chain ext information, asynchronous reading of mcq data into the cache
Effect: Normally, tens of millions of write requests per day. However, the three executors are confusing. The size of maxPriorityExecutor is the same as that of minPriorityExecutor. Is normalPriorityExecutor the maximum number of threads and waits?
- Java. util. concurrent. Define blockingqueue
Purpose: a blocking queue with any range based on connected nodes. This queue sorts elements by FIFO (first-in-first-out. QueueHeaderIs the longest element in the queue. QueueTailIs the minimum time element in the queue. The new element is inserted at the end of the queue, and the elements in the queue header are obtained after the queue get operation. The throughput of the link queue is usually higher than that of the array-Based Queue, but its predictable performance is lower in most concurrent applications. (Jdk Documentation)
Scenario: Construct a member variable of ThreadPoolExecutor
Effect: unknown. Whether the performance is poor for jdk doc writing is to be studied. UseIs concurrent1_queue better? Empty test (http://blog.csdn.net/arkblue/article/details/6151488 two performance comparison; http://yanxuxin.iteye.com/blog/586943 multithreading basics summary 10, 11)
- Java. util. concurrent. locks. ReentrantLock
Purpose: A reentrant mutex lockLock, It has and usessynchronizedThe method and the implicit monitor lock accessed by the statement have the same basic behavior and semantics, but are more powerful.
ReentrantLockThe lock will be obtained by the thread that has recently succeeded and has not released the lock.Yes. When the lock is not owned by another threadlockThe thread will successfully obtain the lock and return it. If the current thread already has the lock, this method will return immediately. AvailableisHeldByCurrentThread()AndgetHoldCount()Method to check whether this situation occurs. Disadvantages of synchronized. there is only one "condition" associated with the phase lock, which is difficult to manage (wait and wake up) for a large number of concurrent threads; B. when multiple threads compete for a lock, the rest of the threads that do not get the lock can only keep trying to get the lock, but cannot be interrupted.
Scenario: Create a memcache client and lock the connection to avoid multiple socket creation.
- Java. util. concurrent. ConcurrentHashMap
- Java. lang. ThreadLocal
Purpose: This class provides the thread-local variable. These variables are different from their common counterparts, becauseGetOrSetEach thread has its own local variable, which is independent of the initialization copy of the variable.ThreadLocalAn instance is usually a private static Field in the class. They want to associate the state with a thread (for example, user ID or transaction ID.
Scenario: used to create dao objects for each thread and perform update operations.