Java Synchronization with Reminder

advertisements

I am developing a module inside my system where based on some event the user has to receive sms.

I have the following situation

synchronized(notificationPhoneNumber)
{
    SmsProvider.sendSms(notificationPhoneNumber, smsText);
}

The code of the method sendSms is running asynchronious:

public static void send(final NotificationPhoneNumber npn, final String smsText)
{
    Thread smsSender = new Thread(new Runnable()
    {
        public void run()
        {
           //sms sending code runs here....
        }

    }); 

    smsSender.start();
}

So the question is how long is the notificationPhoneNumber object locked in this case? Is it going to be locked by the time the thread finishes its job or not?


As long as

 SmsProvider.sendSms(notificationPhoneNumber, smsText);

doesn't return. That means, your sendsms() method will create a thread and return. Just for that amount of time.

Also, if you start your thread in your method. The created thread will just hold the reference but not the lock after sendsms() returns.

The lock and synchronization are external to sendsms(). Whichever thread calls sendsms() will acquire the lock and the code within sendsms() has no knowledge of it.