关于java.long.Object.notify()
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 3 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:我心为谁动 (等级:3 - 略知一二,发帖:515) 发表:2003-11-20 17:15:01  楼主  关注此帖
关于java.long.Object.notify()
Java API doc 对这个method有这么一段叙述:

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

1) By executing a synchronized instance method of that object.
2) By executing the body of a synchronized statement that synchronizes on the object.
3) For objects of type Class, by executing a synchronized static method of that class.

---------------------------------------------------
关于第二点, 请问它的意思是指可以在synchronized的method里面call notify()吗? 比如:

class Buffer {

private String[] buff = new String[8];
private boolean writable = true;
private boolean readable = false;
private int readloc = 0, writeloc = 0;

synchronized void produce (String data) {
while (!writable) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} //end while
buff[writeloc] = data;
readable = true;
writeloc = (writeloc+1)%8;
if (writeloc == readloc) {
writable = false;
}
notify();
}

synchronized String consume () {
while (!readable) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} //end while
writable = true;
String s = buff[readloc];
readloc = (readloc+1)%8;
if (readloc == writeloc) {
readable = false;
}
notify();
return s;
}
};

Put your OWN COOL signature here!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:我心为谁动 (等级:3 - 略知一二,发帖:515) 发表:2003-11-20 17:20:37  2楼
勘误:java.lang.Object.notify()
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2003-11-20 22:20:49  3楼
Yes, you are right.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 3 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码