java - Why Thread.sleep is bad to use -


apologies repeated question haven't found satisfactory answers yet. of question had own specific use case:
java - alternative thread.sleep
is there better or alternative way skip/avoid using thread.sleep(1000) in java?

my question generic use case. wait condition complete. operation. check condition. if condition not true, wait time , again same operation.

for e.g. consider method creates dynamodb table calling createapi table. dynamodb table takes time become active method call describetable api poll status @ regular intervals until time(let's 5 mins - deviation due thread scheduling acceptable). returns true if table becomes active in 5 mins else throws exception.

here pseudo code:

public void createdynamodbtable(string name) {   //call create table api initiate table creation    //wait table become active   long endtime = system.currenttimemillis() + max_wait_time_for_table_create;    while(system.currenttimemillis() < endtime) {     boolean status =  //call describetable api status;     if(status) {          //status true, return          return     } else {         try {             thread.sleep(10*1000);         } catch(interruptedexception e) {         }     }   }    throw new runtimeexception("table still not created"); } 

i understand using thread.sleep blocks current thread, thereby consuming resources. in mid size application, 1 thread big concern?
read somewhere use scheduledthreadpoolexecutor , status polling there. again, have initialize pool @ least 1 thread runnable method polling run.

any suggestions on why using thread.sleep said such bad idea , alternative options achieving same above.

http://msmvps.com/blogs/peterritchie/archive/2007/04/26/thread-sleep-is-a-sign-of-a-poorly-designed-program.aspx

it's fine use thread.sleep in situation. reason people discourage thread.sleep because it's used in ill attempt fix race condition, used notification based synchronization better choice etc.

in case, afaik don't have option poll because api doesn't provide notifications. can see it's infrequent operation because presumably not going create thousand tables.

therefore, find fine use thread.sleep here. said, spawning separate thread when going block current thread anyways seems complicate things without merit.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -