# java.util.concurrent.atomic

***

**1. AtomicInteger for Simple Counter**

```java
import java.util.concurrent.atomic.AtomicInteger;

public class Counter {
    private AtomicInteger count = new AtomicInteger();

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

    public int get() {
        return count.get();
    }
}
```

**2. AtomicBoolean for Flag**

```java
import java.util.concurrent.atomic.AtomicBoolean;

public class Flag {
    private AtomicBoolean isOn = new AtomicBoolean(false);

    public void turnOn() {
        isOn.set(true);
    }

    public void turnOff() {
        isOn.set(false);
    }

    public boolean isOn() {
        return isOn.get();
    }
}
```

**3. AtomicReference for Singleton**

```java
import java.util.concurrent.atomic.AtomicReference;

public class Singleton {
    private static final AtomicReference<Singleton> INSTANCE = new AtomicReference<>();

    private Singleton() {}

    public static Singleton getInstance() {
        Singleton instance = INSTANCE.get();
        if (instance == null) {
            instance = new Singleton();
            INSTANCE.compareAndSet(null, instance);
        }
        return instance;
    }
}
```

**4. AtomicMarkableReference for Double-Checked Locking**

```java
import java.util.concurrent.atomic.AtomicMarkableReference;

public class LockFreeList {
    private static final AtomicMarkableReference<Node> HEAD = new AtomicMarkableReference<>(null, false);

    public void add(Object item) {
        Node newNode = new Node(item);
        while (true) {
            Node prev = HEAD.getReference();
            Node next = prev.next;

            if (HEAD.compareAndSet(prev, next, false, true)) {
                newNode.next = next;
                HEAD.compareAndSet(next, newNode, true, false);
                return;
            }
        }
    }
}
```

**5. AtomicLong for Concurrent Sum**

```java
import java.util.concurrent.atomic.AtomicLong;

public class Summator {
    private AtomicLong sum = new AtomicLong();

    public void add(long value) {
        sum.addAndGet(value);
    }

    public long get() {
        return sum.get();
    }
}
```

**6. AtomicIntegerArray for Shared Array**

```java
import java.util.concurrent.atomic.AtomicIntegerArray;

public class SharedArray {
    private AtomicIntegerArray array = new AtomicIntegerArray(10);

    public void set(int index, int value) {
        array.set(index, value);
    }

    public int get(int index) {
        return array.get(index);
    }
}
```

**7. AtomicReferenceArray for Concurrent Object Array**

```java
import java.util.concurrent.atomic.AtomicReferenceArray;

public class ObjectArray {
    private AtomicReferenceArray<Object> array = new AtomicReferenceArray<>(10);

    public void set(int index, Object value) {
        array.set(index, value);
    }

    public Object get(int index) {
        return array.get(index);
    }
}
```

**8. AtomicIntegerFieldUpdater for Thread-Safe Field Access**

```java
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class ThreadSafeCounter {
    private static final AtomicIntegerFieldUpdater<ThreadSafeCounter> COUNT = AtomicIntegerFieldUpdater.newUpdater(ThreadSafeCounter.class, "count");
    private volatile int count;

    public void increment() {
        COUNT.incrementAndGet(this);
    }

    public int get() {
        return count;
    }
}
```

**9. AtomicLongFieldUpdater for Thread-Safe Long Field Access**

```java
import java.util.concurrent.atomic.AtomicLongFieldUpdater;

public class ThreadSafeSummator {
    private static final AtomicLongFieldUpdater<ThreadSafeSummator> SUM = AtomicLongFieldUpdater.newUpdater(ThreadSafeSummator.class, "sum");
    private volatile long sum;

    public void add(long value) {
        SUM.addAndGet(this, value);
    }

    public long get() {
        return sum;
    }
}
```

**10. AtomicReferenceFieldUpdater for Thread-Safe Reference Field Access**

```java
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class ThreadSafeStack {
    private static final AtomicReferenceFieldUpdater<ThreadSafeStack, Node> TOP = AtomicReferenceFieldUpdater.newUpdater(ThreadSafeStack.class, Node.class, "top");
    private volatile Node top = null;

    public void push(Object item) {
        Node newNode = new Node(item);
        while (true) {
            Node prev = TOP.get(this);
            newNode.next = prev;
            if (TOP.compareAndSet(this, prev, newNode)) {
                return;
            }
        }
    }
}
```

**11. ConcurrentHashMap for Concurrent Map**

```java
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentMap {
    private ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();

    public void put(String key, Object value) {
        map.put(key, value);
    }

    public Object get(String key) {
        return map.get(key);
    }
}
```

**12. ConcurrentSkipListSet for Concurrent Sorted Set**

```java
import java.util.concurrent.ConcurrentSkipListSet;

public class ConcurrentSortedSet {
    private ConcurrentSkipListSet<String> set = new ConcurrentSkipListSet<>();

    public void add(String item) {
        set.add(item);
    }

    public boolean contains(String item) {
        return set.contains(item);
    }
}
```

**13. ConcurrentLinkedQueue for Concurrent Queue**

```java
import java.util.concurrent.ConcurrentLinkedQueue;

public class ConcurrentQueue {
    private ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<>();

    public void offer(Object item) {
        queue.offer(item);
    }

    public Object poll() {
        return queue.poll();
    }
}
```

**14. ConcurrentLinkedDeque for Concurrent Double-Ended Queue**

```java
import java.util.concurrent.ConcurrentLinkedDeque;

public class ConcurrentDeque {
    private ConcurrentLinkedDeque<Object> deque = new ConcurrentLinkedDeque<>();

    public void offerFirst(Object item) {
        deque.offerFirst(item);
    }

    public Object pollFirst() {
        return deque.pollFirst();
    }
}
```

**15. BlockingQueue for Blocking Queue**

```java
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class BlockingQueue {
    private BlockingQueue<Object> queue = new LinkedBlockingQueue<>();

    public void put(Object item) throws InterruptedException {
        queue.put(item);
    }

    public Object take() throws InterruptedException {
        return queue.take();
    }
}
```

**16. ArrayBlockingQueue for Array-Based Blocking Queue**

```java
import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueue {
    private ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(10);

    public void put(Object item) throws InterruptedException {
        queue.put(item);
    }

    public Object take() throws InterruptedException {
        return queue.take();
    }
}
```

**17. LinkedBlockingQueue for Linked List-Based Blocking Queue**

```java
import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockingQueue {
    private LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>();

    public void put(Object item) throws InterruptedException {
        queue.put(item);
    }

    public Object take() throws InterruptedException {
        return queue.take();
    }
}
```

**18. PriorityBlockingQueue for Priority Blocking Queue**

```java
import java.util.concurrent.PriorityBlockingQueue;

public class PriorityBlockingQueue {
    private PriorityBlockingQueue<Object> queue = new PriorityBlockingQueue<>();

    public void put(Object item) {
        queue.put(item);
    }

    public Object take() throws InterruptedException {
        return queue.take();
    }
}
```

**19. DelayQueue for Delay Queue**

```java
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayQueue {
    private DelayQueue<Delayed> queue = new DelayQueue<>();

    public void put(Delayed delayed) {
        queue.put(delayed);
    }

    public Delayed take() throws InterruptedException {
        return queue.take();
    }
}
```

**20. TransferQueue for Transfer Queue**

```java
import java.util.concurrent.TransferQueue;
import java.util.concurrent.LinkedTransferQueue;

public class TransferQueue {
    private TransferQueue<Object> queue = new LinkedTransferQueue<>();

    public void transfer(Object item) throws InterruptedException {
        queue.transfer(item);
    }

    public Object take() throws InterruptedException {
        return queue.take();
    }
}
```

**21. SynchronousQueue for Synchronous Queue**

```java
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueue {
    private SynchronousQueue<Object> queue = new SynchronousQueue<>();

    public void put(Object item) throws InterruptedException {
        queue.put(item);
    }

    public Object take() throws InterruptedException {
        return queue.take();
    }
}
```

**22. Exchanger for Exchanger**

```java
import java.util.concurrent.Exchanger;

public class Exchanger {
    private Exchanger<Object> exchanger = new Exchanger<>();

    public Object exchange(Object item) throws InterruptedException {
        return exchanger.exchange(item);
    }
}
```

**23. CountDownLatch for CountDown Latch**

```java
import java.util.concurrent.CountDownLatch;

public class CountDownLatch {
    private CountDownLatch latch = new CountDownLatch(1);

    public void countDown() {
        latch.countDown();
    }

    public void await() throws InterruptedException {
        latch.await();
    }
}
```

**24. CyclicBarrier for Cyclic Barrier**

```java
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrier {
    private CyclicBarrier barrier = new CyclicBarrier(10);

    public void await() throws InterruptedException, BrokenBarrierException {
        barrier.await();
    }
}
```

**25. Semaphore for Semaphore**

```java
import java.util.concurrent.Semaphore;

public class Semaphore {
    private Semaphore semaphore = new Semaphore(1);

    public void acquire() throws InterruptedException {
        semaphore.acquire();
    }

    public void release() {
        semaphore.release();
    }
}
```

**26. ReentrantLock for Reentrant Lock**

```java
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLock {
    private ReentrantLock lock = new ReentrantLock();

    public void lock() {
        lock.lock();
    }

    public void unlock() {
        lock.unlock();
    }
}
```

**27. ReadWriteLock for Read-Write Lock**

```java
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLock {
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    public void readLock() {
        lock.readLock().lock();
    }

    public void readUnlock() {
        lock.readLock().unlock();
    }

    public void writeLock() {
        lock.writeLock().lock();
    }

    public void writeUnlock() {
        lock.writeLock().unlock();
    }
}
```

**28. StampedLock for Stamped Lock**

```java
import java.util.concurrent.locks.StampedLock;

public class StampedLock {
    private StampedLock lock = new StampedLock();

    public void readLock() {
        long stamp = lock.tryOptimisticRead();
        if (!lock.validate(stamp)) {
            stamp = lock.readLock();
        }
    }

    public void readUnlock() {
        lock.unlockRead(stamp);
    }

    public void writeLock() {
        long stamp = lock.writeLock();
    }

    public void writeUnlock() {
        lock.unlockWrite(stamp);
    }
}
```

**29. Phaser for Phaser**

```java
import java.util.concurrent.Phaser;

public class Phaser {
    private Phaser phaser = new Phaser();

    public void phase(int phase) {
        phaser.arriveAndAwaitAdvance();
    }
}
```

**30. ForkJoinPool for Fork/Join Pool**

```java
import java.util.concurrent.ForkJoinPool;

public class ForkJoinPool {
    private ForkJoinPool pool = new ForkJoinPool();

    public void submit(Runnable task) {
        pool.submit(task);
    }
}
```

**31. CompletableFuture for CompletableFuture**

```java
import java.util.concurrent.CompletableFuture;

public class CompletableFuture {
    private CompletableFuture<Integer> future = new CompletableFuture<>();

    public void complete(int value) {
        future.complete(value);
    }

    public int get() throws InterruptedException, ExecutionException {
        return future.get();
    }
}
```

**32. Executor for Executor**

```java
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class Executor {
    private Executor executor = Executors.newFixedThreadPool(10);

    public void execute(Runnable task) {
        executor.execute(task);
    }
}
```

**33. ScheduledExecutorService for Scheduled Executor Service**

```java
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;

public class ScheduledExecutorService {
    private ScheduledExecutorService executor = Executors.newScheduledThreadPool(10);

    public void schedule(Runnable task, long delay, TimeUnit unit) {
        executor.schedule(task, delay, unit);
    }
}
```

**34. ThreadFactory for Thread Factory**

```java
import java.util.concurrent.ThreadFactory;

public class ThreadFactory {
    private ThreadFactory factory = new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r);
            thread.setName("CustomThread");
            return thread;
        }
    };

    public Thread newThread(Runnable task) {
        return factory.newThread(task);
    }
}
```

**35. RejectedExecutionHandler for Rejected Execution Handler**

```java
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class RejectedExecutionHandler {
    private RejectedExecutionHandler handler = new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("Task rejected");
        }
    };

    public RejectedExecutionHandler getHandler() {
        return handler;
    }
}
```

**36. ThreadPoolExecutor for Thread Pool Executor**

```java
import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolExecutor {
    private ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>());

    public void execute(Runnable task) {
        executor.execute(task);
    }
}
```

**37. ExecutorCompletionService for Executor Completion Service**

```java
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Executors;

public class ExecutorCompletionService {
    private ExecutorCompletionService<Integer> service = new ExecutorCompletionService<>(Executors.newFixedThreadPool(10));

    public void submit(Callable<Integer> callable) {
        service.submit(callable);
    }
}
```

**38. SynchronousQueue for Synchronous Queue**

```java
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueue {
    private SynchronousQueue<Integer> queue = new SynchronousQueue<>();

    public void put(int value) throws InterruptedException {
        queue.put(value);
    }

    public int take() throws InterruptedException {
        return queue.take();
    }
}
```

**39. ArrayBlockingQueue for Array Blocking Queue**

```java
import java.util.concurrent.ArrayBlockingQueue;

public class ArrayBlockingQueue {
    private ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(10);

    public void put(int value) throws InterruptedException {
        queue.put(value);
    }

    public int take() throws InterruptedException {
        return queue.take();
    }
}
```

**40. LinkedBlockingQueue for Linked Blocking Queue**

```java
import java.util.concurrent.LinkedBlockingQueue;

public class LinkedBlockingQueue {
    private LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

    public void put(int value) {
        queue.put(value);
    }

    public int take() throws InterruptedException {
        return queue.take();
    }
}
```

**41. PriorityBlockingQueue for Priority Blocking Queue**

```java
import java.util.concurrent.PriorityBlockingQueue;

public class PriorityBlockingQueue {
    private PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();

    public void put(int value) {
        queue.put(value);
    }

    public int take() throws InterruptedException {
        return queue.take();
    }
}
```

**42. DelayQueue for Delay Queue**

```java
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayQueue {
    private DelayQueue<Delayed> queue = new DelayQueue<>();

    public void put(Delayed delayed) {
        queue.put(delayed);
    }

    public Delayed take() throws InterruptedException {
        return queue.take();
    }
}
```

**43. TransferQueue for Transfer Queue**

```java
import java.util.concurrent.TransferQueue;
import java.util.concurrent.LinkedTransferQueue;

public class TransferQueue {
    private TransferQueue<Integer> queue = new LinkedTransferQueue<>();

    public void transfer(int value) throws InterruptedException {
        queue.transfer(value);
    }

    public int take() throws InterruptedException {
        return queue.take();
    }
}
```

**44. SynchronousQueue for Synchronous Queue**

```java
import java.util.concurrent.SynchronousQueue;

public class SynchronousQueue {
    private SynchronousQueue<Integer> queue = new SynchronousQueue<>();

    public void put(int value) throws InterruptedException {
        queue.put(value);
    }

    public int take() throws InterruptedException {
        return queue.take();
    }
}
```

**45. Exchanger for Exchanger**

```java
import java.util.concurrent.Exchanger;

public class Exchanger {
    private Exchanger<Integer> exchanger = new Exchanger<>();

    public int exchange(int value) throws InterruptedException {
        return exchanger.exchange(value);
    }
}
```

**46. CountDownLatch for Countdown Latch**

```java
import java.util.concurrent.CountDownLatch;

public class CountDownLatch {
    private CountDownLatch latch = new CountDownLatch(1);

    public void countDown() {
        latch.countDown();
    }

    public void await() throws InterruptedException {
        latch.await();
    }
}
```

**47. CyclicBarrier for Cyclic Barrier**

```java
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrier {
    private CyclicBarrier barrier = new CyclicBarrier(10);

    public void await() throws InterruptedException, BrokenBarrierException {
        barrier.await();
    }
}
```

**48. Semaphore for Semaphore**

```java
import java.util.concurrent.Semaphore;

public class Semaphore {
    private Semaphore semaphore = new Semaphore(1);

    public void acquire() throws InterruptedException {
        semaphore.acquire();
    }

    public void release() {
        semaphore.release();
    }
}
```

**49. ReentrantLock for Reentrant Lock**

```java
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLock {
    private ReentrantLock lock = new ReentrantLock();

    public void lock() {
        lock.lock();
    }

    public void unlock() {
        lock.unlock();
    }
}
```

**50. ReadWriteLock for Read-Write Lock**

```java
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLock {
    private ReadWriteLock lock = new ReentrantReadWriteLock();

    public void readLock() {
        lock.readLock().lock();
    }

    public void readUnlock() {
        lock.readLock().unlock();
    }

    public void writeLock() {
        lock.writeLock().lock();
    }

    public void writeUnlock() {
        lock.writeLock().unlock();

```
