user1110790
user1110790

Reputation: 787

Adding a delay based on size of an array

I need to introduce wait in application based on the size of an array. So say if the size of the array is 100 then the wait time will be 5000
then for each next batch of 1000 the delay time will be incremented by 1 second each . What will be the best possible way to do this ? Sample code

int waitTime = 5000;
          if(array.length < 100)
          {
             waitTime= 5000;
          }
          if(array.length > 100 && array.length <=1000)
          {
             waitTime = waitTime+ 1000;
          }

Upvotes: 0

Views: 87

Answers (2)

stinepike
stinepike

Reputation: 54692

I find no problem with your code. But if you can create a relation between array size and sleep time then you can simply write in one line .. something like following

Thread.sleep(array.length < 100 ? 5000 : 6000 + 1000 * (array.length / 1000));

Upvotes: 1

pamphlet
pamphlet

Reputation: 2104

How about:

waitTime = 5000 + 1000 * (array.length / 1000);

Upvotes: 2

Related Questions