---
title: "奇怪的notify()与notifyAll()的执行问题"
author: "yydcj"
date: "2010-11-26T17:12:14Z"
tags: ["IE", "UP", "thread"]
categories: ["编程语言"]
slug: "notify-notifyall"
draft: false
lastmod: "2010-11-26T17:23:47Z"
id: 825700
url: "https://yydcj.iteye.com/blog/825700"
---

摘自http://www.javaworld.com.tw/jute/post/view?bid=5&id=72452&sty=1

 

![](http://www.javaworld.com.tw/jute/images_zh_TW/post.gif) 於 2004-09-28 14:49 [![user profile](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_profile.gif)](http://www.javaworld.com.tw/jute/user/info?uid=23151) [![send a private message to user](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_pm.gif)](http://www.javaworld.com.tw/jute/user/message?action=WriteMessage&to=xianming&subject=Re%3A%E5%A5%87%E6%80%AA%E7%9A%84notify%28%29%E8%88%87notifyAll%28%29%E7%9A%84%E5%9F%B7%E8%A1%8C%E7%B7%92%E5%95%8F%E9%A1%8C%EF%BC%9F) [![reply to post](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_reply.gif)](http://www.javaworld.com.tw/jute/post/reply?bid=5&parent=71433&done=%2Fjute%2Fpost%2Fview%3Fbid%3D5%26id%3D72452%26sty%3D1&quote=0) [![reply to post](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_quote.gif)](http://www.javaworld.com.tw/jute/post/reply?bid=5&parent=71433&done=%2Fjute%2Fpost%2Fview%3Fbid%3D5%26id%3D72452%26sty%3D1&quote=1) [![search all posts by](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_find.gif)](http://www.javaworld.com.tw/jute/post/search?username=xianming&action=Search) [![select and copy to clipboard.  ie only, sorry for netscape users:-)](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_copy.gif)]() [![add this post to my favorite list](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_favorite.gif)](http://www.javaworld.com.tw/jute/user/favorite?action=Add&bid=5&id=71433)

* * *

大家好！我是Java新手！  
我發現一個問題捏！

notifyAll()可以通知所有等待它的執行緒，  
我測試用notify()的結果也會通知所有的執行緒捏！  
請問它們最大的差別在哪呢？

執行環境：j2sdk1.4.2\_05 + Eclipse3.0

```
package concurrent;

class Y extends Thread {
	  int total;
	  public void run() {
	    synchronized(this) {
	      while(total < 50) {
	        try {
	          Thread.sleep(500);
	        } catch(InterruptedException ie) {}
	        total++;
	      }
	      notify();
	    }
	  }
	}
	 
	public class ClassOne implements Runnable {
	  Y ty;
	  
	  ClassOne(Y ty) {
	    this.ty = ty;
	  }
	  
	  public void run() {
	    synchronized(ty) {
	      try {
	        ty.wait();
	      } catch(InterruptedException ie) {}
	      System.out.println(Thread.currentThread().getName() + " Be notify()");
	    }
	  }
	  public static void main(String[] args) {
	    Y ty = new Y();
	    ClassOne x = new ClassOne(ty);
	    
	    Thread t1 = new Thread(x, "t1");
	    Thread t2 = new Thread(x, "t2");
	    Thread t3 = new Thread(x, "t3");
	    t1.start();
	    t2.start();
	    t3.start();
	    ty.start();    
	  }
	}
```

 

![](http://www.javaworld.com.tw/jute/images_zh_TW/post.gif) 於 2004-10-03 04:52 [![user profile](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_profile.gif)](http://www.javaworld.com.tw/jute/user/info?uid=1711) [![send a private message to user](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_pm.gif)](http://www.javaworld.com.tw/jute/user/message?action=WriteMessage&to=Duncan&subject=Re%3ARe%3A%E5%A5%87%E6%80%AA%E7%9A%84notify%28%29%E8%88%87notifyAll%28%29%E7%9A%84%E5%9F%B7%E8%A1%8C%E7%B7%92%E5%95%8F%E9%A1%8C%EF%BC%9F) [![send email to Duncan](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_email.gif)](mailto:duncan0807@gmail.com) [![reply to post](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_reply.gif)](http://www.javaworld.com.tw/jute/post/reply?bid=5&parent=72106&done=%2Fjute%2Fpost%2Fview%3Fbid%3D5%26id%3D72452%26sty%3D1&quote=0) [![reply to post](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_quote.gif)](http://www.javaworld.com.tw/jute/post/reply?bid=5&parent=72106&done=%2Fjute%2Fpost%2Fview%3Fbid%3D5%26id%3D72452%26sty%3D1&quote=1) [![search all posts by](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_find.gif)](http://www.javaworld.com.tw/jute/post/search?username=Duncan&action=Search) [![select and copy to clipboard.  ie only, sorry for netscape users:-)](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_copy.gif)]() [![add this post to my favorite list](http://www.javaworld.com.tw/jute/images_zh_TW/icons/icon_favorite.gif)](http://www.javaworld.com.tw/jute/user/favorite?action=Add&bid=5&id=72106)

經過我與其他版主的討論，這可以說是一個 bug 或是說這是一個令人意想不到 side effect(我沒留意到有哪份官方文件有提到這一點)。

原 因在於 Thread object 結束時(成為 dead thread)會(在自己身上)執行 notifyAll method，這一點可以從 Thread - join method 看出來(當一個 thread T1 invoke join(0) or join() 在另一個 thread object T2 身上，T1 thread 會 block 住，直到 thread T2 成為 non-alive thread)。

```
public final synchronized void join(long millis) 
    throws InterruptedException {
	long base = System.currentTimeMillis();
	long now = 0;

	if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
	}

	if (millis == 0) {
	    while (isAlive()) {
		wait(0);
	    }
	} else {
	    while (isAlive()) {
		long delay = millis - now;
		if (delay <= 0) {
		    break;
		}
		wait(delay);
		now = System.currentTimeMillis() - base;
	    }
	}
    }
```

 

可以看到 Thread - join method 是以 synchronization 的機制來實做，當 join 的 actual argument value 為 0 是採用 wait(0) 來讓 caller thread block 住，wait(0) 執行後不會因為 timeout 過了而讓 caller thread wake up，可以想見 Thread class 的底層實做一定有在 run method 執行完畢而 Thread object 所啟動的 thread 成為 dead thread 時，有 invoke notifyAll 在 Thread object 身上，以便 wake up 其他因為 join(0) or join() invocation 而 block 住的 thread，否則其他的 thread 便一直停駐在紅色 的那一個 statement 處。

你試著把 Y - run method 中的 notify method call 拿掉，其他的 thread 還是會自動 wake up，在 ty thread 成為 dead thread 之後。

你把 Y instance 以 Thread 包裝起來執行，就可以避開這問題。

```
public class ClassOne implements Runnable {
    public static void main(String[] args)
    {
        Y ty = new Y();
        // [略] ...
        new Thread(ty).start();
    }
}
```