Sam
Sam

Reputation: 6900

Alternative for synchronized block in java

I use following code for guarantee startTime variable set once only:

public class Processor
{
    private Date startTime;

    public void doProcess()
    {
        if(startTime == null)
            synchronized(this)
            {
                  if(startTime == null)
                  {
                     startTime = new Date();
                  }
            }

        // do somethings
    }
}

I will guarantee by this code for variable instantiated once only for any number of invoking process method call.

My question is:

Is there alternative approach for my code be more concise? (for sample remove if & synchronized statements)

Upvotes: 12

Views: 12863

Answers (6)

Peter Lawrey
Peter Lawrey

Reputation: 533870

Based on you comments, you could use AtomicReference

firstStartTime.compareAndSet(null, new Date());

or AtomicLong

firstStartTime.compareAndSet(0L, System.currentTimeMillis());

I would use

private final Date startTime = new Date();

or

private final long startTime = System.currentTimeMillis();

Upvotes: 12

Tudor
Tudor

Reputation: 62469

So from my understanding you need a singleton which is:

  1. Short, easy to implement/understand.
  2. Only initialized when doProcess is called.

I suggest the following implementation using a nested class:

public class Processor {
    private Date startTime;

    private static class Nested {
        public static final Date date = new Date();
    }

    public void doProcess() {
        startTime = Nested.date; // initialized on first reference
        // do somethings
    }
}

Upvotes: 3

yegor256
yegor256

Reputation: 105203

Use AtomicReference:

public class Processor {
  private final AtomicReference<Date> startTime = new AtomicReference<Date>();
  public void doProcess() {
    if (this.startTime.compareAndSet(null, new Date())) {
      // do something first time only
    }
    // do somethings
  }
}

Upvotes: 12

Alex D
Alex D

Reputation: 30485

To sum up what other posters have already explained:

private volatile Date startTime;

public void doProcess()
{
   if(startTime == null) startTime = new Date();
   // ...
}

Concise enough for you?

Upvotes: 2

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

1 What you have used is known as double checked locking.

2. There are another 2 ways to do it

  - Use synchronized on the Method
  - Initialize the static variable during declaration.

3. As you want an example with No if and synchronized keyword, i am showing you the Initialize the static variable during declaration. way.

public class MyClass{

  private static MyClass unique = new MyClass();

  private MyClass{}

  public static MyClass getInstance(){

      return unique;

  }

 }

Upvotes: 0

AlexR
AlexR

Reputation: 115388

Your code is an example of so called "double check locking." Please read this article. It explains why this trick does not work in java although it is very smart.

Upvotes: 4

Related Questions