Multithreading combat

Thread basics:

  1. When <thread-safe> multiple threads access the same resource, the default queue is handled the way, but the order thread execution is not written in order to see, but look at how to allocate CPU threads. Therefore, the thread will continue to poll the lock, the lock will cause competition.

  2. <Multiple-threaded multi-lock> Multi-lock multiple threads, each thread will get their specified lock after lock respectively, to perform content synchronized method body. Two threads, the thread is to get two different locks, they affect each other. There is one case is the same lock, is added on the synchronized keyword on a static method to indicate the lock .class class, use the class-level locks.

  3. <Synchronous and asynchronous object lock>

    1. Synchronization is the existence of shared resources. No shared resource is no need to synchronize.
    2. Asynchronous is independent and not subject to any constraints between each other. For example, when using http, when the page launched ajax request, and the operation can continue to browse the contents of the page, there is no relationship between the two.
    3. Key: Atomicity (synchronous), the visibility.
  4. <Dirty read>

    1. For synchronous and asynchronous methods of an object, in the design, we need to consider the overall problem, otherwise it will not match the data error. In oracle also has such ACID, ensure the accuracy of the data, when acquiring data, if other clients modify this value, it will not return to the user to see, always see or old values.
  5. <Synchronized other concepts>

    1. The method of reentrant lock: You can lock method, again call the lock method, you can continue to be embedded.
    2. Parent-child class reentrant locks: the parent class subclass called, so are thread-safe.
  6. <Synchronized attention to detail>

    1. Note lock granularity, lock only code region shared resources, not too much lock code will result in reduced performance, a large number of threads waiting.
    2. Do not use String constants as a lock, there will be all kinds of problems.
    3. When using a String object as a lock, do not modify the lock in the thread, this will lead to lock failure, because the string object has been modified.
    4. When using an object as a lock, you can change the property value, the lock will not fail. You can not modify the object reference .
  7. volatile:

    1. The concept: The main role is to make visible the variables between multiple threads.
    2. FAQ: After the thread is started, to modify the thread attributes do not take effect. The main reason is because after JDK1.5 for optimizing thread, a thread after each start, will have its own independent running space, save the properties required, so external to modify the properties of the thread can not affect their property.
    3. Thread execution process: start a thread when the thread will open up a space for space use, storage properties of main memory, thread execution engine default will only get data from the thread space, you will not get the data to the main memory. But after using Volatile keyword, when the data is changed, the mandatory engine threads get data to main memory.
    4. Disadvantages: only to ensure the visibility between threads, the thread can not guarantee atomicity. But to make use of Atomic * class (class only guarantee atomicity Atomic method itself, does not guarantee atomicity operation a plurality of times).
  8. Communication between threads

    1. wait and notify: Both are object methods, both of which must be synchronized with the use, release the lock wait, notify does not release the lock, notify the notification will not immediately to wait.
    2. CountDownLatch: no need to use synchronized, real-time notifications.
    3. ThreadLocal: it is a multi-threaded solution between concurrent access to variables. Locks are not provided, but the thread has a copy of each variable (space for time approach). In the concurrent it is not very high, locked performance better; but in highly concurrent or under competitive circumstances, ThreadLocal can reduce lock contention.
    4. Single security modes: 1. Static inner classes 2.double check (twice whether obj is null)
  9. Concurrent class container:

    1. ConcurrentHashMap; ConcurrentSkipListMap (to make up for not ordering ConcurrentHashMap, very treeMap). The Map is divided into 16 segment (segment). Each segment is equivalent to a hashTable. Between segments are isolated, they are thread-safe. Reducing the lock granularity.
    2. copy-on-write containers: two realized CopyOnWriteArrayList and CopyOnWriteArraySet.
    3. Queue:
      1. ConcurrentLinkedQueue high-performance Queue representatives. By lock-free manner, high performance at high concurrency. It is an unbounded thread-safe queue based on linked nodes. Elements of the queue to follow the FIFO principle. Is added to the first head, the tail is added last. It does not permit null elements.
      2. BlockingQueue is blocking queue representatives.
      3. DelayQueue: to put an element, set the timeout. Into the element type must implement Delayed interface. GetDelay need to override two methods (to determine whether the deadline); comcompareTo (); call to take the time, not immediately remove the elements, but to getDelay negative when we come to remove the data.
      4. PriorityBlockingQueue: When the elements are not placed, it sorted, but take the time, it will be the highest priority data according to priority, to pick up.
      5. SynchronousQueue: no queue is a storage space, only the first take, in order to add, add nor is added to the container, but directly to take use.
  10. Thread Design Patterns:

    1. Future Mode: Focus reflect asynchronous mode.
      1. Requirements: receiving the request, direct return, and then process the data back myself, and finally return to real data when the user actually see the data, get real data. Will not let users have been waiting for data processing in a complete state.
      2. Realization: that returns fake data requests directly, using threads to implement real operating data in the background, really need real data when the data is ready to call again.
    2. Master-Worker mode: refinement tasks, using multiple threads, do the task.
      1. Demand: The system consists of two types collaboration process. master is responsible for receiving and distributing tasks. worker responsible for handling sub-tasks, worker finished processing tasks, and then return to the master, conducted and summarized. Thereby increasing the throughput of the system.
      2. achieve:
        1. Master's work: 1. Use ConcurrentLinkedQueue to carry all tasks. 2. HashMap <String, Thread> to carry all worker; 3. Use ConcurrentHashMap <String, Object> result set for each bearer concurrent processing tasks.
        2. . Worker work: a first implement a Runnable interface; 2. Each worker object needs to have a reference to the master ConcurrentLinkedQueue; 3. Each worker object requires a reference to the master ConcurrentHashMap;
        3. worker to pick up tasks from the queue master, and then put himself into a HashMap, then the final result is stored in the master ConcurrentHashMap.
  11. Thread Pool:

    1. Executors thread pool facility. Use it to create a variety of thread pool.
      1. newFixedThreadPool (): Returns a fixed number of thread pool, the number of threads in the thread pool is always the same. When there is a job submission, if the thread is idle, executed immediately, if not, wait idle thread performs a task queue in the staging.
      2. newSingleExecutor (): Creates a thread pool thread, when there is a job submission, if the thread is idle, executed immediately, if not, the temporary waiting idle threads execute in a task queue.
      3. newCachedThreadPool (): returns the number of threads can be adjusted according to the actual situation of the thread pool, the maximum number is not limited. If the thread is idle with mission, without task thread is not created. And each thread is automatically recovered after 60 seconds.
      4. newScheduleThreadPool (): Returns a ScheduleExecutorService object, but the thread you can specify the number of threads.
      5. ThreadPoolExecutor: custom thread pool. Based on using a bounded or unbounded queue queue to distinguish.
        1. Bounded queue: If new tasks to be performed, if the actual number of thread pool is less than corePoolSize, then create a thread priority. If more than corePoolSize, the task will be added to the queue. If the queue is full, the premise is not greater than the total number of threads maximumPoolSize to create a new thread. If the number of threads is greater than maximumPoolSize, refused to execute strategy, or other custom behavior.
        2. Unbounded queue: Compared with bounded queue, unless the thread resource depletion, otherwise, the task will not be unbounded queue team lost the phenomenon. When a new task, the system is less than the number of threads corePoolSize, the new thread to perform the task. When reached corePoolSize, it will not go any further. If there is no idle thread resources, it will be put to the task queue until system memory is exhausted.
        3. JDK denial strategy:
          1. AbortPolicy: direct throw, that prevent normal.
          2. CallerRunPolicy: As long as the thread pool is not closed, the policy directly in the caller's thread, run the current task is discarded.
          3. DiscardOldestPolicy: discard the oldest task, try to submit the current job again.
          4. DiscardPolicy: discard task, do not give any treatment.
    2. Executors of submit and execute difference:
      1. submit can be passed to achieve the instance of the Callable interface.
      2. submit method returns a value. The return value is a Future object, get these methods, it is possible to know whether the end of the task. At the end of, get method returns null.
  12. And common tools under contract (concurrent.util):

    1. CountDownLacth: commonly used to monitor some initialization tasks, such as initialization is complete, notify the main thread to continue working.
      1. Examples of countDownLatch when the parameters passed, how many times is the need countDown, to wake await;
    2. CyclicBarrier: All threads are ready, simultaneously. Examples of CyclicBarrier when parameters are passed, it is the need for the number of times await, in order to execute the contents of their own threads together. A thread is not ready, other threads can not execute. The difference is: CountDownLacth for a thread, CyclicBarrier for multiple threads.
    3. Callable:
    4. Future: suitable process very time-consuming business, can effectively reduce the response time of the system, improve the system throughput. TutureTask: get the object is executed asynchronously.
    5. Semaphore (Semaphore): before a new real systems, the need for a reasonable assessment of the access system. To ensure that resources are not wasted, it will not lack.
      1. PV (page view): The total site traffic, page views or clicks, users will be refreshed once recorded.
      2. UV (unique visitor): a computer client access page for a visitor, in general, the time within 0-24 points of the same IP can only be recorded once.
      3. QPS (query per second): the number of queries per second, largely on behalf of a busy business systems behind each request, may correspond to multiple disk I / O, multiple network requests, more CPU time tablets and so on. We can understand the system very intuitive qps current business situation. Once the current qps exceeds the set threshold, consider adding machines to the cluster expansion, in order to avoid excessive pressure leads to downtime, can be valued in accordance with the pre-stress test, in the case of combination of comprehensive operation and maintenance of late, estimated threshold .
      4. RT (response time): the response time of the request.
      5. Capacity Evaluation: In general, we after multiple rounds of stress testing, evaluation may be made to the system peak, a so-called principle of 80/20, i.e. 80% of the access requests will reach 20% of the time. Peak qps = (total pv * 80%) / (60 * 60 * 24 * 20%). Then the total number of single highest peak divided by qps qps value can withstand machine, the machine is required: the total number of machines = peak qps / pressure limit qps single measurement obtained.
    6. Semaphore: specified parameters, the number of threads that can simultaneously access, you can reach a certain limiting operation, access permissions were acquired and released by acquire and release. However, better use redis and nginx to do.
    7. Lock (Lock): In addition to using synchronized for lock operation may also be used Lock the object.
      1. Reentrant lock (ReentrantLock): synchronization code section plus the need to lock in, but in the end we must release the lock, or will never be able to cause the lock release, other threads can never enter the result. Format is try, finally the structure used to ensure that regardless of whether the code is executed correctly, are guaranteed the right to release the lock.
      2. Lock Lock communications: wait and notify the synchronized comparison. Lock condition used for communication between the lock. Gets condition is lock.newCondition; correspondence is wait correspond to await, notify corresponds to the signal; a Lock can produce multiple condition, so the interaction between multithreaded very flexible. Can make part of the thread wakes up, the other threads wait.
      3. Fair and unfair lock lock: Lock fair performance lower than fair locks, lock the need to maintain order as fair, unfair lock is in any order.
        1. Lock lock = new ReentrantLock(boolean isFair);
        2. usage:
          1. tryLock (): try to get a lock, use true and false returns the result.
          2. tryLock (): acquire the lock at any given time, the use of true and false return results.
          3. isFair (): whether it is fair locks.
          4. isLocked (): it is locked.
          5. getHoldCount (): the number of queries the current thread holds this lock, Lock number that is calling.
          6. lockInterruptibly (): priority response interrupt locks.
      4. Write lock (ReentrantReadWriteLock): for at least reading and writing of the scene, to read and write to achieve separation. Its essence is divided into two locks, a read lock and a write lock, in the case of a read lock, multiple threads can be accessed concurrently, but in the case of a write lock, only a sequential access. "Formulas: read sharing, write mutually exclusive, read-write mutually exclusive." By ReentrantReadWriteLock lock = new ReentrantReadWriteLock (); granted read and write locks: ReadLock readLock = lock.readLock (); WriteLock writeLock = lock.writeLock ();
      5. Distributed Lock: need a third party frameworks, such as zk, ensure that the code lock between a plurality of threads.
  13. High concurrent processing architecture:

    1. Networks.
    2. Service Business: divided into different modules, using nginx / lvs diversion.
    3. On the level of java: limit traffic.
  14. Disruptor:

    1. Overview: runs entirely in memory, using a network implemented Queue time driven in the case, the lock is no longer possible to be operating concurrently. Is a high-performance asynchronous processing framework, the JMS lightweight, but also to achieve a viewer mode, or listening mode to achieve.
    2. Common terms:
      1. RingBuffer: responsible for the flow of data is stored and updated in the Disruptor.
      2. Sequence: sequence for the serial number using a special assembly process. Each consumer maintains a Sequence. Most of the code depends on the operation of these concurrent Sequence values, and therefore supports a variety of sequence currently AtomicLong class characteristics.
      3. Sequencer: This is the core of Disputor. Implemented two producers of this interface (single / multi-producer) have achieved all of concurrent algorithms, in order to perform fast and accurate data transfer between producers and consumers.
      4. SequenceBarrier: generated by the Sequencer, and contains a reference has released Sequence of these sequence derived from the Sequencer and some independent consumer Sequence. It contains logical for consumers to decide whether to spend the Event of.
      5. WaitStrategy: a consumer decide how to wait for the producer to the Event into the Disruptor.
      6. Event: data units from the producer to the consumer during the process. Custom.
      7. EventProcess: The main event loop, the processing Disruptor the Event, and have consumers Sequence. It has implemented a class is BatchEventProcessor, comprising Event loop efficient implementation and to achieve the object EventHandler callback interface.
      8. EventHandler: implemented by the user and represents a Disruptor in consumer interfaces.
      9. Producer: implemented by the user, it calls RingBuffer to insert event (Event), there is no corresponding implementation code in Disruptor, the realization by the user.
      10. WorkProcess: Sequence ensure that every consumer is only a processor, process multiple workProcess in the same workPool is not consumed the same Sequence.
      11. WorkerPool: a WorkProcessor pool, which workProcessor will consume Sequence, so tasks can be handed over between worker workHandler implementation of the interface.
      12. LifecycleAware: BatchEventProcess when starting and stopping, to implement the interface for receiving notifications.
      13. Scenario: you can reference (concurrent website: http://ifeve.com/disruptor/ )
        1. May be performed sequentially, an Event handler continues processing in each.
        2. It may be first order, and then executed in parallel.
        3. Simple implementation producer - consumer: You can use only RingBuffer.

Guess you like

Origin blog.csdn.net/weixin_38608626/article/details/86008028