'IllegalMonitorStateException: current thread is not owner

my application uses two SwingWorkers, one works in the Homepage to control online users and one works on opening a chat to keep the chat updated. My problem is that the moment I open the chat (the SwingWorker of the chat starts to work) the SwingWorker of the Homepage no longer works, it is as if it went to standby. For this reason I tried to "wake up" the thread, but unfortunately I came across the exception that is precisely the title of this article / question.

can any of you help me understand what the problem is? why is the chat thread blocking the homepage thread?

below you will find the source code (unmodified)

thank you so much in advance for your help!

HOMEPAGE:

private class SWOnlineUserCheck extends SwingWorker<Void, Void>{

    @Override
    protected Void doInBackground() throws Exception {
       while(running){
          sleep(3000);
          sql = "SELECT USRID,ONLINE FROM USR ORDER BY USRID ASC";
          res = db_conn.select(sql);
          chatItem = MLP_CHATLIST.getComponents();
          while(res.next()){
             for(int i=0; i<chatItem.length; i++){
                chatItemListStat = (ChatItemList) chatItem[i];
                if(chatItemListStat.getUserId() == res.getInt("USRID")){
                   sql = "SELECT DESCRIPTION FROM KWS WHERE KEYFIELD = 'ONLINESTATUS' AND KEYWORD = '"+res.getString("ONLINE")+"'";
                   res2 = db_conn.select(sql);
                   if(res2.next()){
                      chatItemListStat.ML_STATUS.setText(res2.getString("DESCRIPTION"));
                   }
                   if("J".equals(res.getString("ONLINE"))){
                      chatItemListStat.ML_IMGSTATUS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/OBMS_icons/icons8-green-circle-18.png")));
                   }
                   else if("P".equals(res.getString("ONLINE"))){
                      chatItemListStat.ML_IMGSTATUS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/OBMS_icons/icons8-orange-circle-18.png")));
                   }
                   else{
                      chatItemListStat.ML_IMGSTATUS.setIcon(new javax.swing.ImageIcon(getClass().getResource("/OBMS_icons/icons8-red-circle-18.png")));
                   }
                }
             }
          }
       }
       return null;
    }
    @Override
    protected void done(){
    }
}

hier i try to wake up the thread of the Homepage:

private void MC_STATUSItemStateChanged(java.awt.event.ItemEvent evt) {                                           
    synchronized(this) {
       if(running && swonlineusercheck.isDone()){
          swonlineusercheck.notify();
       }
    }
    for(var entry:map_status.entrySet()){
       if(entry.getValue().equals(MC_STATUS.getItemAt(MC_STATUS.getSelectedIndex()))){
          mc_status = entry.getKey();
          break;
       }
    }
    sql = "UPDATE USR SET ONLINE = '"+mc_status+"' WHERE USRID = '"+user.getUserId()+"'";
    try{
       db_conn.update(sql);
    }
    catch(SQLException ex){
       Logger.getLogger(OBMS_HOMEPAGE.class.getName()).log(Level.SEVERE, null, ex);
    }
    if("J".equals(mc_status)){
       ML_STATIMG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/OBMS_icons/icons8-green-circle-18.png")));
    }
    else if("P".equals(mc_status)){
       ML_STATIMG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/OBMS_icons/icons8-orange-circle-18.png")));
    }
    else{
       ML_STATIMG.setIcon(new javax.swing.ImageIcon(getClass().getResource("/OBMS_icons/icons8-red-circle-18.png")));
    }
}

CHAT_WINDOW:

private class SWCHAT_WINDOWCheck extends SwingWorker<Void, Void>{

    @Override
    protected Void doInBackground() throws Exception {
       while(running){
          if(firstChatQuery){
             sql="SELECT * FROM CHT WHERE SENDER IN ('"+chat_sender.getUserId()+"','"+chat_partner.getUserId()+"') "
               + "AND RECEIVER IN ('"+chat_sender.getUserId()+"','"+chat_partner.getUserId()+"') "
               + "AND CAST(DATE AS DATE) = CURRENT_DATE() "
               + "AND CAST(DATE AS TIME) > '"+chatTime+"' ORDER BY DATE ASC";
             res = db_conn.select(sql);
          }
          else{
             sql="SELECT * FROM CHT WHERE SENDER IN ('"+chat_sender.getUserId()+"','"+chat_partner.getUserId()+"') "
               + "AND RECEIVER IN ('"+chat_sender.getUserId()+"','"+chat_partner.getUserId()+"') "
               + "AND CHTID > "+lastChatId+" ORDER BY DATE DESC LIMIT 1";
             res = db_conn.select(sql);
          }
          fillChat(res);
       }
       return null;
    }
    @Override
    protected void done(){
    }
}

hier i try to wake up the thread of the Chat window:

private void ChatWrite() throws SQLException{
  synchronized(this) {
      if(swchatwindowcheck.isDone()){
         swchatwindowcheck.notify();
      }
  }
  usrid_sender = chat_sender.getUserId();
  usrid_receiver = chat_partner.getUserId();
  usrid_writer = usrid_sender;
  sql = "INSERT INTO CHT (CHTID,CHGID,SENDER,RECEIVER,WRITER,DATE,TEXT,IMG_VID) "
       +"VALUES (NULL,NULL,'"+usrid_sender+"','"+usrid_receiver+"','"+usrid_writer+"'"
       +",CURRENT_TIMESTAMP(),'"+MTA_CHATTEXT.getText()+"','')";
  db_conn.update(sql);
  MTA_CHATTEXT.setText("");
  MTA_CHATTEXT.requestFocus();
  
 }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source