为什么我在同步静态变量的时候,总会出:
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at com.jufeng.ftp.Server.add(text.java:48)
at com.jufeng.ftp.text.main(text.java:26)
Exception in thread "main" 的问题》》》public class text {
public static void main(String[] args) {
Thread thread1=new Thread(new Server());
Thread thread2=new Thread(new Server());
thread1.start();
thread2.start();
Server.add();
}
}class Server implements Runnable {
private static List list = new ArrayList(); public void run() {
synchronized (Server.class) {
if (list.isEmpty()) {
try {
list.wait();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
public static void add(){
synchronized(Server.class){
list.add("1");
list.notify();
}
}
}在notify()之前,线程要获得list的 lock,我在前面加了 synchronized(Server.class)..为什么还是不行???
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.notify(Native Method)
at com.jufeng.ftp.Server.add(text.java:48)
at com.jufeng.ftp.text.main(text.java:26)
Exception in thread "main" 的问题》》》public class text {
public static void main(String[] args) {
Thread thread1=new Thread(new Server());
Thread thread2=new Thread(new Server());
thread1.start();
thread2.start();
Server.add();
}
}class Server implements Runnable {
private static List list = new ArrayList(); public void run() {
synchronized (Server.class) {
if (list.isEmpty()) {
try {
list.wait();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
public static void add(){
synchronized(Server.class){
list.add("1");
list.notify();
}
}
}在notify()之前,线程要获得list的 lock,我在前面加了 synchronized(Server.class)..为什么还是不行???
解决方案 »
- solr分页
- JAVA基础?
- 新手一个关于JDialog的问题
- bluetooth
- 我用JBuilder2006加载Servlet时,出错了java.lang.ClassNotFoundException: myservlet.UserallServlet,请问,各位大虾,这个时什么原因
- 奇怪,我的jbuilder8无法编辑保存jsp文件
- import外部类和look&feel问题
- 如果来修改表格(jtable)中的数据,然后再如果得到修改那一行的数据?
- 如何使用java做套打?最好有例子,急!在线等待
- 匿名类
- JFreechart 高手请进
- DataInputStream.readByte()的问题
觉得你还没搞清楚什么地方允许用wait/notifyAll, 我的blog上有篇文章,可以参考一下
另外,不要说“好像”
这种方法程序更易读,更符合思维逻辑
public static void main(String[] args) {
Thread thread1=new Thread(new Server());
Thread thread2=new Thread(new Server());
thread1.start();
thread2.start();
Server.add();
}
}class Server implements Runnable {
private static List list = new ArrayList(); public void run() {
synchronized (Server.class) {
if (list.isEmpty()) {
try {
Server.class.wait();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
public static void add(){
synchronized(Server.class){
list.add("1");
Server.class.notifyAll();
}
}
}
public class text {
public static void main(String[] args) {
Thread thread1=new Thread(new Server());
Thread thread2=new Thread(new Server());
thread1.start();
thread2.start();
Server.add();
}
}class Server implements Runnable {
private static List list = new ArrayList(); public void run() {
synchronized (list) {
if (list.isEmpty()) {
try {
list.wait();
} catch (Exception e) {
System.out.println(e);
}
}
}
}
public static void add(){
synchronized(list){
list.add("1");
list.notifyAll();
}
}
}
如果当时 调用 wait 的不是 thread1,那么 thread1 不能 notify .