为什么我在同步静态变量的时候,总会出:
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)..为什么还是不行???

解决方案 »

  1.   

    synchronized (list) 要清楚谁是owner
      

  2.   

    我想要的不是在对象级的lock,, synchronized (list) 、synchronized(this);肯定行,但是对于static的变量好象不起作用吧
      

  3.   

    The current thread must own this object's monitor. 
    觉得你还没搞清楚什么地方允许用wait/notifyAll, 我的blog上有篇文章,可以参考一下
    另外,不要说“好像”
      

  4.   

    不瞒楼上的 ,我刚开始就是用的 synchronized (list) ,但是,没有达到我的 意图要用wait/notifyAll,必要在该线程获得了锁以后,才能用如果用 synchronized (list)就 和同步一般的成员没什么区别了,,怎么体现它是 static了???
      

  5.   

    此时 Server.class 是同步对象list.notify 改成 Server.class.notify或Server.class.notifyAll另一种方法,同时也是更好的方法将同步对改为 list,即使用 synchronized(list) list.wait list.notify/notifyAll
    这种方法程序更易读,更符合思维逻辑
      

  6.   

    //我刚开始就是用的 synchronized (list) ,但是,没有达到我的 意图你的意图是什么?从程序看,你需要在Server.add();前加些延时,否则add完了可能你的线程才启动,根本执行不到wait
      

  7.   

    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 {
    Server.class.wait();
    } catch (Exception e) {
    System.out.println(e);
    }
    }
    }
    }

    public static void add(){
    synchronized(Server.class){
    list.add("1");
    Server.class.notifyAll();
    }
    }
    }
      

  8.   

    或者
    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();
    }
    }
    }
      

  9.   

    我想你如果不用 static 成员, 2 个 线程都是用同一个 server 对象,不用建2 个的话 , 不久少了麻烦。
      

  10.   

    猜测原因:
    如果当时 调用 wait 的不是 thread1,那么 thread1 不能 notify .
      

  11.   

    为什么要用server.class作为lockflag呢?用list不是更好理解吗
      

  12.   

    TO:ESoftWind(),我试一试你的方法...