Ctrip [get] Concurrency and multithreading

 

Complicated by

 

A period of time, the ability to multi-task processing alternately.

 

parallel

 

Ability to handle multiple tasks

Critical section

The critical area is used to indicate a public resource (or resource sharing), it can be used by multiple threads. But you can only use it once a thread, once the critical section resources are occupied, in order to use this resource other threads must wait for resources to be released. In the concurrent program, the critical area resources are protected object.

 

 

A thread-safe

Thread

  • A thread is the basic unit of CPU scheduling and dispatch, the role of the task is to improve the average processing speed.
  • Thread has its own stack operation, the program counter, the local variable table and other resources, sharing all the resources of the process with the other threads in the same process.
  • Multithreading problem may be caused by: occupy memory, resource competition deadlock.
  • The appropriate number of threads to make full use of CPU resources.

Thread of the life cycle (five states)

1.NEW: New state, the thread is created the state has not yet started

  • Three ways to create ways

2.RUNNABLE: state before running after the ready state, call the start ()

  • Thread start () can not be called multiple times

3.RUNNING: running, is run () when the thread is executing state

  • Because a thread may be factors quit RUNNING, such as time, abnormal, lock, scheduling, etc.

4.BLOCKING: blocking state, this state is entered, the following situations

  • Synchronous blocking: lock is occupied by another thread
  • Actively blocking: Thread calling certain methods of the initiative for the implementation of the right CPU, such as sleep (), join (), etc.
  • Waiting for blocking: the implementation of the wait ()

5.DEAD: termination of state, is run () execution ends, or because the state after abnormal exit, this status is irreversible

Create a thread (the thread starts) three (two kinds) ways / how to create a thread

1. Thread class inheritance override run () method

  • Disadvantages: often do not meet the Richter substitution principle. Not recommended
  • Disadvantages: the task is executed, the results can not be obtained directly, need the help of shared variables get

2. implement Runnable

  • Advantages: Can be programmed more flexible and less exposure to the details, allowing users to focus on the realization of the thread run () method.
  • Disadvantages: the task is executed, the results can not be obtained directly, need the help of shared variables get
  • Disadvantages: Only through setDefaultUncaughtException () subroutine way in order to capture an exception in the main thread

3. To achieve Callable Interface

  • The return value can be obtained by a call (). Callable and Future good solution to this problem, you can get the results
  • call () can throw an exception

Method class Thead

And the difference between Runnable interface Callable interface

Comparison of principles thread pool, thread pool and start directly thread

 

 

 

 

 

Second, what is the lock

You have to know which lock in Java

 

 

 

Third, thread synchronization

 

Fourth, the thread pool

 

五、ThreadLocal

 

Guess you like

Origin www.cnblogs.com/zwhu1216/p/11388197.html