Weekly Material: High concurrent programming <a>

Chapter One

Several concepts

  • Sync: one method call must wait until there is follow-up after their return
  • Asynchronous: a method call after the execution of another thread, the caller may not necessarily return the like can be carried out subsequent
  • Concurrency: tasks to a very short time alternately
  • Parallel: tasks simultaneously
  • The critical area: public resources
  • Obstruction: a critical section resources are occupied, must wait for the release of the previous thread.
  • Deadlock: occupy each other needed resources, we can not proceed
  • Hunger: thread priority is too low, can not always access to resources
  • Livelock: active thread release resources with each other to each other, making it impossible to get the normal execution of all resources

Concurrency level:

  • Obstructive: critical region lock, the lock will not be suspended to wait until all the resources (pessimistic locking)
  • No Hunger: fair locks, all have the opportunity to execute the main program
  • Accessibility: we can get the resources out of the conflict rollback (optimistic locking), could lead to unlimited rollback, not a thread after executing any
  • No Lock: no lock are accessible in parallel, the difference is bound to ensure that there is a thread to complete execution in a limited step
  • No wait: requiring all threads to finish in a finite number of steps (an implementation is: uncontrolled read, modify and await the return on a copy-on-write)

JMM (Java Memory Model) established principles: atomicity, visibility, orderly

Atomicity: indivisible operation, upon execution not disturbed

Visibility: a thread changes the value of a shared variable, other variables immediately know whether this modification

Ordering: the program sequence problem (caused by a rearrangement instruction)

Chapter two

1. processes and threads

2. Thread the basic operation

  • New threads: Thread inherit or implement Runable / Callable, rewrite run, call start.
  • Terminate the thread: try not to use the stop () method
  • Thread interrupts: interrupt ()

  • Waiting and notification

    wait (), notify () must be included in synchronzied ---- notify statement () random wake a thread, notifyAll () all wake up.

  • Suspends and resumes execution (abandoned Method)
  • Wait for the end of the thread (join ()) and humility (yield)

Objects can be seen currently waiting threadA will block until the object is to wait after the end of threadB isAlive () returns false when the while loop will end when threadB exit calls notifyAll () method notifies all waiting threads.

 public static native void yield (); this is a static method, if implemented, it would make the current thread CPU, however, be noted that, get out of the CPU is not representative of the current thread is no longer running, and if the next competition, was given a slice of CPU time the current thread will still continue to operate. in addition, let out only time slice allocated to the current thread of the same priority thread .

 3. For frequent changes of type field, you can use the field volatile to ensure that other threads after thread changes to the data can see this change.

 4. Thread Group

5. daemon worker thread execution systems (Daemon)

6. Thread Priority

7.synchronized keyword

 

 

Under concurrency errors:

ArrayList: thread safe (destruction of the internal consistency when accessing, saving variable-sized container access exception access) ------ using Vector 

HashMap: Unsafe thread (may cause structural damage occurs ring can not exit) ------------------ use ConcurrentHashMap

i ++ nature: i = Integer.valueOf (i.intValue () + 1); wherein, Integer.valueOf is a factory method, each time creating a new Integer object and a reference to the copied i

i++

Guess you like

Origin www.cnblogs.com/lvoooop/p/12021668.html