# synchronized 方法

synchronized 关键字只能用于修饰方法和代码块，而且不能用于修饰构造方法。

A synchronized method automatically performs a lock action when it is invoked; its body is not executed until the lock action has successfully completed. If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method). If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined. If execution of the method’s body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.

• 当前线程的同步方法执行结束、无论是正常结束还是异常结束。
• 在当前线程执行同步方法或者同步代码块的时候调用了同步监视器对象的 wait() 方法。

• 线程执行同步方法或者同步代码块的时候，程序调用 Thread.sleep() 方法或者 Thread.yield() 方法暂停当前线程的执行，当前线程不会释放同步监视器。
• 线程执行同步方法或者同步代码块的时候，其他线程调用了该线程的 suspend() 方法将该线程挂起，该线程不会释放同步监视器对象。

wait()

# 线程池

• 系统创建一个新线程的成本比较高，因为涉及到和操作系统的交互，可以通过线程池提高性能
• 使用线程池可以控制系统中并发线程的数量，防止 JVM 中线程数过多导致 JVM 性能下降

JDK 中使用 java.util.concurrent.ExecutorService 接口表示线程池。