# Counter

* [Thread safe counter](#thread-safe-counter)
  * [With synchronized blocks / methods](#with-synchronized-blocks--methods)
  * [With ReentrantLock](#with-reentrantlock)
  * [Atomic class](#atomic-class)
  * [Unsafe class implementation](#unsafe-class-implementation)

## Thread safe counter

### With synchronized blocks / methods

```java
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

```java
public class ReentrantLockCounter
{
    private final Lock lock = new ReentrantLock();
    private int value;

    public int get()
    {
        try
        {
            lock.lock();
            return value;
        }
        finally
        {
            lock.unlock();
        }
    }

    public synchronized void increment()
    {
        try
        {
            lock.lock();
            value++;
        }
        finally
        {
            lock.unlock();
        }
    }
}

public class ReadWriteLockCounter
{
    private int count;
    private final ReadWriteLock lock = new ReentrantReadWriteLock();

    public void increment()
    {
        try
        {
            lock.writeLock().lock();
            count++;
        }
        finally
        {
            lock.writeLock().unlock();
        }
    }

    public int getCount()
    {
        try
        {
            lock.readLock().lock();
            return count;
        }
        finally
        {
            lock.readLock().unlock();
        }
    }
}
```

### Atomic class

```java
public class AtomicVariableCounter
{
    private AtomicInteger c = new AtomicInteger(0);

    public void increment()
    {
        c.incrementAndGet();
    }

    public int getCount()
    {
        return c.get();
    }
}
```

### Unsafe class implementation

```java
import sun.misc.Unsafe;

import java.lang.reflect.Field;

public class CounterUnsafe 
{
    volatile int i = 0; //cas hardware   memory address --Long 232323523454235

    private static  Unsafe unsafe =null;

    private static  long valueOffSet; // The offset address inside memory

    static 
    {
        try 
        {
            // Use reflection to get the unsafe instance
            // Use reflection to get field 
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            // Set the field as visible
            field.setAccessible(true);
            // Get the Unsafe instance
            unsafe = (Unsafe) field.get(null);

            // Get the offset within memory
            // Get the field
            Field field1 = CounterUnsafe.class.getDeclaredField("i");
            // Get the field offset address
            valueOffSet = unsafe.objectFieldOffset(field1);
        } 
        catch (NoSuchFieldException | IllegalAccessException e) 
        {
            e.printStackTrace();
        }
    }

    public void add() 
    {
        for(;;) 
        {
            // Get the field
            int current = unsafe.getIntVolatile(this, valueOffSet); 

            // Performance: increment
            if (unsafe.compareAndSwapInt(this, valueOffSet, current, current + 1))
            { 
                break;
            };
        }

    }

    public static void main(String[] args) {

    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eric-zhang-seattle.gitbook.io/mess-around/java-concurrency/multithreads/counter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
