java.lang.IllegalMonitorStateException

0

The LeaveServer method is in a class named ServerData , and serves to get a free server when the client wishes.

The notifyall method was implemented in another class where it contains all the servers, it is exemplified below.

I would like to know why the exception given below is raised whenever the LeaveServer method 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 that contains 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();
    }
}
    
asked by anonymous 18.12.2018 / 19:44

1 answer

0

You must give Synchronize , in this case:

Synchronized(servers_conditions.get(x))

and then notifyAll .

    
19.12.2018 / 21:25