java.lang.Illegalmonitorstateexception

Asked

Viewed 32 times

0

The method LeaveServer is in a class called ServerData, and serves to put an open server when the client so wishes.

The method notifyall has been implemented in another class where contains all Servers, is exemplified below.

I would like to know why the exception presented below is raised whenever the method LeaveServer is called.

java.lang.IllegalMonitorStateException
   at java.lang.Object.notifyAll(Native Method)
   at Servidor.ServerData.notifyall(ServerData.java:58)
   at Servidor.ClientData.LeaveServer(ClientData.java:192)
   at Servidor.Interface.write(Interface.java:120)
   at Servidor.Interface$1.run(Interface.java:36)
public static Boolean LeaveServer(String Client_email,String Server_name,Integer Server_id){
    try {
        lock.lock();
        for (Server sv : ServerData.getServers().get(Server_name)) {
            if (sv.getServerId() == Server_id) {
                sv.TurnOff();//poe o server inativo
                try {
                    ServerData.notifyall(Server_name);//aviso os clientes que este server está disponível
                }catch (Exception e){
                    e.printStackTrace();
                }
                acquired_servers.get(Client_email).remove(sv);//reitra o server dos adquiridos do servidor
                return true;
            }
        }
    }finally {
        lock.unlock();
    }
    return false;
}


Class ServerData containing the method notifyall

public class ServerData {
//mapa de servidores do sistema
private static Map<String,List<Server>> servers = new HashMap<>();
private static ReentrantLock lock = new ReentrantLock();
private static Map<String,Condition> servers_conditions = new HashMap<>();

static{ //meter vários servidores de um TIPO
    servers_conditions.put("t3Server",lock.newCondition());
    List<Server> t3Server = new ArrayList<>();
    servers_conditions.put("m5Server",lock.newCondition());
    List<Server> m5Server = new ArrayList<>();
    List<Server> f4Server = new ArrayList<>();
    servers_conditions.put("f4Server",lock.newCondition());

    t3Server.add(new ServerGenerator("t3Server",20F,1));
    m5Server.add(new ServerGenerator("m5Server",10F,2));
    m5Server.add(new ServerGenerator("m5Server",10F,3));
    f4Server.add(new ServerGenerator("f4Server",2F,4));
    //f4Server.add(new ServerGenerator("f4Server",2F,5));
    //f4Server.add(new ServerGenerator("f4Server",3F,6));

    servers.put("t3Server",t3Server);
    servers.put("m5Server",m5Server);
    servers.put("f4Server",f4Server);

}

  public static void notifyall(String x) throws InterruptedException{
    try{
        lock.lock();
        servers_conditions.get(x).notifyAll();
    }finally {
        lock.unlock();
    }

}

public static void await(String x) throws InterruptedException{
    try {
        lock.lock();
        servers_conditions.get(x).await();
    }finally {
        lock.unlock();
    }
}

1 answer

0

It is necessary to give Synchronize, in this case:

Synchronized(servers_conditions.get(x))

and then notifyAll.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.