Pawan
Pawan

Reputation: 32331

In what way would Java's Thread.sleep would be useful here?

I am working on an existing Java EE Multi-threading application.

I am unable to understand this thing on to my Application. There is one thread named UserThread, and in its run method's while(true) condition it reads data from a location and pushes the data to a Websocket. After that the Thread sleeps for 1000 seconds.

Why is this Thread.sleep() useful?

Upvotes: 1

Views: 217

Answers (2)

Tudor
Tudor

Reputation: 62459

It is emulating a timer, executing a task every x seconds, which is very bad practice. Consider switching to java.util.Timer or ScheduledThreadPoolExecutor:

ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);

exec.scheduleAtFixedRate(
      new Runnable() {
          public void run() {
               // code
          }
      }, 0, 1000, TimeUnit.Milliseconds);

Upvotes: 4

Rob Hruska
Rob Hruska

Reputation: 120346

Sounds like it's just being used to throttle the reads and/or writes to avoid sending too many requests at once.

Upvotes: 8

Related Questions