Multi-threaded development attention to the problem

  • From thread to a meaningful name, so easy to find Bug
  • Synchronization narrow range, thereby reducing lock contention, for example, the synchronized, should not try to use the sync block synchronization method
  • Multi-use synchronization tool less wait () and notify (). First, CountDownLatch, CyclicBarrier, Semaphore Exchanger these synchronization classes and the simplified encoding operation, and a wait () and notify () is difficult to achieve complex control flow; secondly, the synchronization classes are best prepared and maintained by the enterprise, in the subsequent of the JDK will continue to optimize and improve.
  • Use producers and consumers to achieve BlockingQueue
  • Multi-purpose concurrent collections less synchronous collection, for example, should be used instead of Hashtable ConcurrentHashMap
  • Use local variables and immutable classes to ensure thread safety
  • Use a thread pool instead of directly creating a thread, it is because of the high cost of creating a thread, the thread pool can effectively use limited thread to start the task

Guess you like

Origin www.cnblogs.com/yjxyy/p/11125635.html