Counter
Thread safe counter
With synchronized blocks / methods
public class SynchronizedBlocksCounter
{
private int value;
public synchronized int get()
{
synchronized ( this )
{
return value;
}
}
public synchronized void increment()
{
synchronized ( this )
{
value++;
}
}
}
public class SynchronizedMethodsCounter
{
private int value;
public synchronized int get()
{
return value;
}
public synchronized void increment()
{
value++;
}
}With ReentrantLock
Atomic class
Unsafe class implementation
Last updated
Was this helpful?