Java AtomicInteger getInstanceCounter(String name)

Here you can find the source of getInstanceCounter(String name)

Description

Returns a new counter for instance with core-name 'name'.

License

Open Source License

Parameter

Parameter Description
name proposed name of the instance.

Return

instance count for known instances with name 'name'.

Declaration

private static int getInstanceCounter(String name) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    /**/*from w  ww . j  av  a2 s  .  co m*/
     * Internal storage for instance counters.
     */
    private static ConcurrentMap<String, AtomicInteger> instanceCounters = new ConcurrentHashMap<String, AtomicInteger>();

    /**
     * Returns a new counter for instance with core-name 'name'. We support multiple producers with same name by adding a minus <number> to the name, like Service, Service-1 etc.
     * @param name proposed name of the instance.
     * @return instance count for known instances with name 'name'.
     */
    private static int getInstanceCounter(String name) {
        AtomicInteger counter = instanceCounters.get(name);
        if (counter != null)
            return counter.incrementAndGet();

        counter = new AtomicInteger(0);
        AtomicInteger old = instanceCounters.putIfAbsent(name, counter);
        if (old != null)
            counter = old;
        return counter.incrementAndGet();
    }
}

Related

  1. getExceptionCountMap()
  2. getFailedMiddlePartNumbers(PartListing partListing)
  3. getGetPendingWrTransaction()
  4. getGuid(Object clazz)
  5. getId()
  6. getLock()
  7. getNextCounter(AtomicInteger counter)
  8. getNextId()
  9. getNextTempQueueName()