NetBeans / Java / New hint: Thread.sleep called in a loop

NetBeans has a new hint that says: Thread.sleep called in a loop.

Question 1: How / when can this be a problem for sleep in a cycle?

Question 2: If this is a problem, what should I do instead?

UPDATE: Question 3: Here is the code. Tell me in this case if I should use something else instead of Thread.Sleep in a loop. In short, this is used by a server that listens for TCP client connections. Sleep is used here if the maximum number of sessions with clients is reached. In this situation, I want the application to wait for a free session to become available.

public class SessionManager { private static final int DEFAULT_PORT = 7500; private static final int SLEEP_TIME = 200; private final DatabaseManager database = new DatabaseManager(); private final ServerSocket serverSocket = new ServerSocket(DEFAULT_PORT); public SessionManager() throws IOException, SQLException { } public void listen() { while (true) if (Session.getSessionCount() < Session.getMaxSessionCount()) try { new Thread(new Session(database, serverSocket.accept())).start(); } catch (IOException ex) { ex.printStackTrace(); } else try { Thread.sleep(SLEEP_TIME); } catch (InterruptedException ex) { ex.printStackTrace(); } } public static void main(String[] args) throws IOException, SQLException { new SessionManager().listen(); } } 
+45
java multithreading sleep netbeans
Aug 21 '10 at 0:35
source share
5 answers

Calling sleep in a cycle usually results in poor performance. For example:

 while (true) { if (stream.available() > 0) { // read input } sleep(MILLISECONDS); } 

If MILLISECONDS is too large, then this code will take a long time to understand that the input is available.

If MILLISECONDS is too small, then this code will spend a lot of system resources on input that has not arrived yet.

Other uses of sleep in a loop are usually also dubious. Generally, the best way.

If this is a problem, what should I do instead?

Send the code and maybe we can give you a reasonable answer.

EDIT

IMO, the best way to solve the problem is to use ThreadPoolExecutor .

Something like that:

 public void listen() { BlockingQueue queue = new SynchronousQueue(); ThreadPoolExecutor executor = new ThreadPoolExecutor( 1, Session.getMaxSessionCount(), 100, TimeUnit.SECONDS, queue); while (true) { try { queue.submit(new Session(database, serverSocket.accept())); } catch (IOException ex) { ex.printStackTrace(); } } } 

This sets up the executor as your code works. There are many other ways to do this; see javadoc link above.

+22
Aug 21 2018-10-18T00:
source share

As others have said, it depends on usage. Acceptable Use is a program designed to run something every 10 seconds (but not so important that it takes an exact time). We have many of these "utility applications" that import data and other similar tasks every few minutes. This is an easy way to accomplish these tasks, and we usually set the wait interval to very low and use a counter so that the program remains responsive and can exit easily.

 int count = 0; while (true) { try { // Wait for 1 second. Thread.sleep(1000); } catch (InterruptedException ex) {} // Check to see if the program should exit due to other conditions. if (shouldExit()) break; // Is 10 seconds up yet? If not, just loop back around. count++; if (count < 10) continue; // 10 seconds is up. Reset the counter and do something important. count = 0; this.doSomething(); } 
+3
Jul 14 2018-11-11T00:
source share

How / when can it be a problem to sleep in a cycle?
Sometimes people use it instead of the right synchronization methods (e.g. wait / notify).

If this is a problem, what should I do instead?
Depends on what you do. Although it’s enough for me to imagine a situation where this is the best approach, I think it is also possible.

You can check out the Sun concurrency tutorial on this.

+2
Aug 21 '10 at 0:43
source share

I think I come across one completely legal use of the sleep() method in a loop.

We have one-way communication between the server and the client. Therefore, when the client wants to achieve asynchronous communication with the server, it sends a message to the server, and not periodically polls for any response from the server. There should be some timeout period.

 Response resp = null; for (int i = 0; i < POLL_REPEAT && resp == null; i++) { try { Thread.sleep(POLL_INTERVAL); } catch (InterruptedException ie) { } resp = server.getResponse(workflowId); } 

POLL_REPEAT * POLL_INTERVAL ~ TIMEOUT Interval

+2
Jul 17 2018-12-17T00:
source share

This is not the best solution, but you can reduce the number of milliseconds, for example Thread.sleep(1);

0
Jan 12 '19 at 18:45
source share



All Articles