Reputation: 6900
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
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
Reputation: 62469
So from my understanding you need a singleton which is:
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
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
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
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