Note that there are some explanatory texts on larger screens.

plurals
  1. POInvokeLater Freezes the GUI
    primarykey
    data
    text
    <p>I'm trying to simulate a simple thermostat, using multi-threading. The thermostat should increase the temperature in order to reach the value that the user requested for which is "Max" value in the code below. I have two thread, one in charge of increasing the temperature and the other for decreasing it. but the condition for decreasing is it should be only running when the gas is off. but I have a problem with implementing this concept. each time I press the Up button in order to increase the desired temperature the GUI freezes. How can I solve this problem?</p> <p>It's like a thread is holding the lock and won't release it but how can I notice which one?</p> <pre><code>`private volatile boolean isBoilerOn = false; protected int Max, Current; protected boolean isDone, isGasOn, isPumpOn; private temperatureUp tempUp; private temperatureDown tempDown; public void setBoilerSatus(boolean status) { this.isBoilerOn = status; } public boolean getBoilerStatus() { return this.isBoilerOn; } private synchronized void setGasBoilerStatus() { if(Max&gt;Current) setBoilerSatus(true); else setBoilerSatus(false); notifyAll(); } private void btnUpActionPerformed(java.awt.event.ActionEvent evt) { if(Max&lt;=8) { Max++; String strI = String.valueOf(Max); lblDesiredTemp.setText(strI); setGasBoilerStatus(); } } private void btnDownActionPerformed(java.awt.event.ActionEvent evt) { if(Max&gt;0) { Max--; String strI = String.valueOf(Max); lblDesiredTemp.setText(strI); setGasBoilerStatus(); } } private void formWindowActivated(java.awt.event.WindowEvent evt) { systemInitial(); tempUp = new temperatureUp(); tempDown = new temperatureDown(); tempUp.start(); tempDown.start(); } private synchronized void increaseTemeture() throws InterruptedException { while (!getBoilerStatus()) wait(); if(Max&gt;Current) { Thread.sleep(4000); Current ++; } setGasBoilerStatus(); } private synchronized void decreaseTemeture() throws InterruptedException { while(getBoilerStatus()) wait(); Thread.sleep(4000); if(Current == 0 ) return; Current --; setGasBoilerStatus(); } private void systemInitial() { isGasOn = false; Max = Current = 0; } class temperatureUp extends Thread { @Override public void run() { while(true) { try { increaseTemeture(); } catch(Exception ex) { StringWriter w = new StringWriter(); ex.printStackTrace(new PrintWriter(w)); txtLog.setText(w + "\n" + txtLog.getText()); } } } }; class temperatureDown extends Thread { @Override public void run() { while(true) { try { decreaseTemeture(); } catch(Exception ex) { StringWriter w = new StringWriter(); ex.printStackTrace(new PrintWriter(w)); txtLog.setText(w + "\n" + txtLog.getText()); } } } };` </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload