Thread.Sleep option in Java

I was told that using Thread.Sleep() is a bad solution, from time to time you need to make a certain time interval in the action loop in a synchronized method.

On the other hand, I have two different threads that are active for the duration of my program, as well as one common object, and when I use Object.wait (long) in this common object, it makes my GUI freeze for when something.

What would be the best solution for this problem?


Update This part of the code includes one of the threads that runs in the GUI:

 class temperatureUp extends Thread { @Override public void run() { while(true) { try { GBC.increaseTemp(); updateSystemStatus(); } catch(Exception ex) { StringWriter w = new StringWriter(); ex.printStackTrace(new PrintWriter(w)); txtLog.setText(w + "\n" + txtLog.getText()); } } } }; 

and this is a synchronized method in a shared object, GBC:

 public synchronized void increaseTemp() throws InterruptedException{ // don't increase the temperature if the boiler // is not turned on... while (!isBoilerOn) wait(); // increase the current temperature if ((currentTemp + 1) < MAX_TEMP && currentTemp < desiredTemp) { Thread.sleep(2000); ///what should put here if not thread sleep? currentTemp ++; updateGasBoilerStatus(); } } 
+6
source share
5 answers

Do not sleep inside the synchronized method! Do not wait in GUI event handlers / methods!

Separate synchronized actions so that the Sleep () call is not called in the context of the GUI thread.

Maybe for the second bit use InvokeLater ().

+7
source

You can reduce the scope of the synchronize statement. For example, if you synchronize the whole method

 public synchronized void foo() 

You can remove the modifier and use a synchronized block instead.

 synchronized (this) { // ... } 

and, if possible, move Thread.sleep() outside of this block. Only those statements that change the state of shared data are synchronized.

Many Swing-related threading problems are related to Event Dispatcher Thread and can be easily resolved with it. I recommend you read it.

A bit of background why you shouldn't call Thread.sleep() inside the synchronization block:

Sleeping or waiting while holding a lock. Calling Thread.sleep using a lock can prevent other threads from flowing for a long time and is therefore a potentially serious life threat. calling Object.wait or Condition.await with two held locks poses a similar danger. [Jcip]

+4
source

I would use monitors: http://www.artima.com/insidejvm/ed2/threadsynch4.html Maybe with a notification or a notification. All of this can be solved. Good luck

0
source

Always keep the Dispatcher Event Stream (EDT), which is responsible for working with your GUI, away from any work not related to the user interface. Also, do not synchronize the whole method, but synchronize atomic statements

 synchronized(this){ //... } 
0
source

You can try the following code:

 public static void delay(int waitTime) { long endTime = System.currentTimeMillis() + (waitTime * 1000); while (System.currentTimeMillis() < endTime) {} } 

Call as a delay (5). Control will wait 5 seconds.

-1
source

Source: https://habr.com/ru/post/921559/


All Articles