1.1.3 How to reduce context switching

The method of reducing context switching absence lock concurrent programming, CAS algorithm, with minimal use of threads and coroutine.

  • Lock-free concurrent programming. Multi-threaded lock contention can cause a context switch, the multi-threaded processing of data, some of the approach can be used to avoid the use of a lock, such as the ID data in accordance with Hash algorithm modulo segment, different threads processing data in different segments.
  • CAS algorithm. Java packages using the Atomic CAS algorithm to update data without locking.
  • With a minimum thread. Avoid creating unnecessary threads, such as small task, but creating a lot of threads to handle, this will cause a large number of threads are in a wait state.
  • Coroutine. Scheduling multi-task to achieve in a single-threaded in and maintain switching between multiple tasks in a single thread.

Guess you like

Origin www.cnblogs.com/cglib/p/12055947.html