Thread Sleep Makes Other Threads Wait

280 Views Asked by At

I have a task where while generating a random password for user the SMS should go after 4 MIN, but the welcome SMS should go immediately. Since password I am setting first and need to send after 4 MIN I am making that thread sleep (Cant use ExecutorServices), and welcome SMS thread start.

Here is the code:

String PasswordSMS="Dear User, Your password is "+'"'+"goody"+'"'+" Your FREE 
recharge service is LIVE now!";
String welcomeSMS="Dear goody, Welcome to XYZ";
         try {          
            Thread q=new Thread(new GupShupSMSUtill(PasswordSMS,MOB_NUM));
            Thread.sleep(4 * 60 * 1000);
            q.start();
             GupShupSMSUtill sendWelcomesms2=new GupShupSMSUtill(welcomeSMS, MOB_NUM);
                Thread Bal3=new Thread(sendWelcomesms2);
                Bal3.start();

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

</code> 

So if I change the order the thread sendWelcomesms2 Immediately starts.I have to send welcome SMS then password sms (After 4 Min) how its achievable ??

NOTE: Both SMS come after 4 MIN

4

There are 4 best solutions below

0
On BEST ANSWER
Thread.sleep(4 * 60 * 1000);

delays execution of your currently running thread, your q.start() is not executed until the wait time is over. This order doesn't make sense.

1
On

You are sleeping the current thread, before you issue the startcommand for q.

You probably want to issue the sleep inside GupShupSMSUtill() (maybe change its signature to something like GupShupSMSUtill(PasswordSMS,MOB_NUM, sleeptime) to be able to control how long it sleeps).

0
On

Your thread is only created when

Thread q=new Thread(new GupShupSMSUtill(PasswordSMS,MOB_NUM));

is executed. Your thread is started when

q.start();

is executed. So if you want to achieve running the q thread while the main thread sleep, you should write your lines in this order:

        Thread q=new Thread(new GupShupSMSUtill(PasswordSMS,MOB_NUM)); // Create thread
        q.start(); // start thread
        Thread.sleep(4 * 60 * 1000); // suspend main thread for 4 sec
0
On

You can use join():

String PasswordSMS = "Dear User, Your password is " + "\"" + "goody" + "\"" + " Your FREE recharge service is LIVE now!";
String welcomeSMS = "Dear goody, Welcome to XYZ";
try
{
    GupShupSMSUtill sendWelcomesms2 = new GupShupSMSUtill(welcomeSMS, MOB_NUM);
    Thread Bal3 = new Thread(sendWelcomesms2);
    Bal3.start();
    Thread q = new Thread(new GupShupSMSUtill(PasswordSMS, MOB_NUM));
    q.start();
    q.join();
}
catch (InterruptedException e)
{
    e.printStackTrace();
}

Or latch:

private static java.util.concurrent.CountDownLatch latch = new java.util.concurrent.CountDownLatch(1);

And the code:

String PasswordSMS = "Dear User, Your password is " + "\"" + "goody" + "\"" + " Your FREE recharge service is LIVE now!";
String welcomeSMS = "Dear goody, Welcome to XYZ";
try
{
    GupShupSMSUtill sendWelcomesms2 = new GupShupSMSUtill(welcomeSMS, MOB_NUM);
    Thread Bal3 = new Thread(sendWelcomesms2);
    Bal3.start();
    Thread q = new Thread(new GupShupSMSUtill(PasswordSMS, MOB_NUM));
    q.start();
    latch.await(); // Wait
}
catch (InterruptedException e)
{
    e.printStackTrace();
}

At the end of the Thread "q":

latch.countDown(); // stop to wait

Hint - Don't use Thread.sleep(x) in this case.