-3

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

tirz
  • 2,041
  • 1
  • 22
  • 37
goodyzain
  • 683
  • 2
  • 8
  • 21

4 Answers4

4
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.

Thorsten Schöning
  • 3,501
  • 2
  • 25
  • 46
1

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
mg30rg
  • 1,311
  • 13
  • 24
0

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.

tirz
  • 2,041
  • 1
  • 22
  • 37
-1

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).

ths
  • 2,858
  • 1
  • 16
  • 21
  • Call `sleep()` from inside a constructor? Sounds like bad design. In a good design, every method has exactly one responsibility. A constructor's responsibility is to initialize an instance of its class. `sleep()` does not initialize anything. – Solomon Slow Jun 16 '15 at 16:46