---
title: "为什么wait()必须要在放synchronized里面？"
author: "standalone"
date: "2010-10-19T11:01:40Z"
tags: []
categories: ["编程语言"]
slug: "wait-synchronized"
draft: false
lastmod: "2010-10-19T11:01:40Z"
id: 788259
url: "https://standalone.iteye.com/blog/788259"
---

假如你要调用某个对象的wait()方法，一般都要放在synchronized声明里面，why? 下面是答案。

 

Suppose `d` is the object we're using to invoke `wait`. When a thread invokes `d.wait`, it must own the intrinsic lock for `d` — otherwise an error is thrown. Invoking `wait` inside a synchronized method is a simple way to acquire the intrinsic lock.

When `wait` is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke [`Object.notifyAll`](http://download.oracle.com/javase/7/docs/api/java/lang/Object.html#notifyAll%28%29), informing all threads waiting on that lock that something important has happened。