[concurrency-interest] SynchronizedLong vs. other locks

Aleksey Gureev spyromus at noizeramp.com
Fri Jul 8 05:37:16 EDT 2005


I have a following piece of code:

    private static synchronized long getTomorrowTime()
        final long current = System.currentTimeMillis();
        if (current > tomorrow)
            GregorianCalendar cal = new GregorianCalendar();
            cal.add(Calendar.DATE, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            tomorrow = cal.getTimeInMillis();

        return tomorrow;

"tomorrow" is static. It's obvious that the instance synchronization is
not really what's necessary because the other objects of this class can
still introduce race conditions. My question is can SynchronizedLong
help me somehow or it's better to replace instance locking with
class-wide locking?

Thank you,

Aleksey Gureev
Noizeramp Creative Group

More information about the Concurrency-interest mailing list