Example usage for java.util.concurrent.atomic AtomicInteger AtomicInteger

List of usage examples for java.util.concurrent.atomic AtomicInteger AtomicInteger

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger AtomicInteger.

Prototype

public AtomicInteger(int initialValue) 

Source Link

Document

Creates a new AtomicInteger with the given initial value.

Usage

From source file:ThreadManager.java

/**
 * Create a new ThreadManager./*www . j a  v a2s .c om*/
 *
 * @param numIndices      the number of values the index should take on (from 0 to numIndices-1)
 * @param task            the task to perform
 */

public ThreadManager(int numIndices, Task task) {
    this.numIndices = numIndices;
    this.task = task;
    nextIndex = new AtomicInteger(numIndices);
    controller = new Object();
    controllerWaiting = false;
    waitingThreads = new HashSet<Thread>();
}

From source file:org.jtheque.features.FeatureServiceTest.java

@Test
public void listenerCalledForMainFeatures() {
    final AtomicInteger addCounter = new AtomicInteger(0);
    final AtomicInteger removeCounter = new AtomicInteger(0);
    final AtomicInteger modifyCounter = new AtomicInteger(0);

    featureService.addFeatureListener(new MyFeatureListener(addCounter, removeCounter, modifyCounter));

    featureService.addMenu("no-module", new MenuMain());

    assertEquals(1, addCounter.get());/*ww  w  . j a  va 2 s.  co  m*/
    assertEquals(0, removeCounter.get());
    assertEquals(0, modifyCounter.get());

    featureService.addMenu("no-module", new MenuMain());

    assertEquals(2, addCounter.get());
    assertEquals(0, removeCounter.get());
    assertEquals(0, modifyCounter.get());
}

From source file:eu.stratosphere.nephele.io.channels.DistributedChannelWithAccessInfo.java

DistributedChannelWithAccessInfo(final FileSystem fs, final Path checkpointFile, final int bufferSize,
        final boolean deleteOnClose) throws IOException {

    this.fs = fs;
    this.checkpointFile = checkpointFile;
    this.channel = new FileChannelWrapper(fs, checkpointFile, bufferSize, (short) 2);
    this.reservedWritePosition = new AtomicLong(0L);
    this.referenceCounter = new AtomicInteger(0);
    this.deleteOnClose = new AtomicBoolean(deleteOnClose);
}

From source file:io.microprofile.showcase.speaker.persistence.SpeakerDAO.java

@PostConstruct
private void initStore() {
    Logger.getLogger(SpeakerDAO.class.getName()).log(Level.INFO, "Initialise speaker DAO from bootstrap data");

    final Set<Speaker> featured = new HashSet<>(0);

    for (final Venue venue : this.venues) {
        featured.addAll(venue.getSpeakers());
    }/*  w w w  .  j a  va 2  s. com*/

    final AtomicInteger idc = new AtomicInteger(0);

    this.bootstrapData.getSpeaker().forEach(bootstrap -> {

        final int intId = Integer.valueOf(bootstrap.getId());

        if (intId > idc.get()) {
            idc.set(intId);
        }

        final String id = String.valueOf(intId);
        final String[] names = bootstrap.getFullName().split(" ");
        final Speaker sp = new Speaker();
        sp.setId(id);
        sp.setNameFirst(names[0].trim());
        sp.setNameLast(names[1].trim());
        sp.setOrganization(bootstrap.getCompany());
        sp.setBiography(bootstrap.getJobTitle());

        sp.setPicture("assets/images/unknown.jpg");

        appendFeatured(featured, sp);

        this.speakers.put(id, sp);
    });

    for (final Speaker fs : featured) {

        boolean found = false;

        for (final Speaker sp : this.speakers.values()) {
            if (fs.getNameFirst().toLowerCase().equals(sp.getNameFirst().toLowerCase())
                    && fs.getNameLast().toLowerCase().equals(sp.getNameLast().toLowerCase())) {
                found = true;
                break;
            }
        }

        if (!found) {
            fs.setId(String.valueOf(idc.incrementAndGet()));
            this.speakers.put(fs.getId(), fs);
        }
    }

    //TODO - Merge back to source json
}

From source file:nl.knaw.huygens.alexandria.endpoint.search.SearchResultPage.java

public SearchResultPage(String baseURI, int pageNumber, SearchResult searchResult) {
    this.baseURI = baseURI;
    this.pageNumber = pageNumber;
    this.lastPageNumber = Math.max(1, searchResult.getTotalPages());
    this.isLast = pageNumber == lastPageNumber;
    this.counter = new AtomicInteger(searchResult.getPageSize() * (pageNumber - 1));
    this.counterFunction = t -> counter.incrementAndGet();
    this.searchInfo = ImmutableMap.<String, Object>builder()//
            .put("id", searchResult.getId())//
            .put("query", searchResult.getQuery())//
            .put("searchDurationInMs", searchResult.getSearchDurationInMs())//
            .put("totalPages", searchResult.getTotalPages())//
            .put("pageSize", searchResult.getPageSize())//
            .put("totalResults", searchResult.getTotalResults())//
            .build();/*from w  w w  .  j a  v a 2 s.  co m*/
}

From source file:de.alexkamp.sandbox.ChrootSandboxFactory.java

@Override
public void deleteSandbox(final SandboxData sandbox) {
    File copyDir = sandbox.getBaseDir();

    try {/* ww w.j  a va 2 s . co m*/
        final AtomicInteger depth = new AtomicInteger(0);
        Files.walkFileTree(copyDir.toPath(), new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes basicFileAttributes)
                    throws IOException {
                int d = depth.getAndIncrement();

                // see whether the mounts are gone
                if (1 == d) {
                    for (Mount m : sandbox) {
                        if (path.endsWith(m.getMountPoint().substring(1))) {
                            if (0 != path.toFile().listFiles().length) {
                                throw new IllegalArgumentException(
                                        path.getFileName() + " has not been unmounted.");
                            }
                        }
                    }
                }

                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes)
                    throws IOException {
                Files.delete(path);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path path, IOException e) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException {
                Files.delete(path);
                depth.decrementAndGet();
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw new SandboxException(e);
    }
}

From source file:com.khartec.waltz.jobs.sample.BusinessRegionProductHierarchyGenerator.java

private static void generateHierarchy(DSLContext dsl) throws IOException {
    System.out.println("Deleting existing  hierarchy ...");
    int deletedCount = dsl.deleteFrom(MEASURABLE)
            .where(MEASURABLE.MEASURABLE_CATEGORY_ID.in(DSL.select(MEASURABLE_CATEGORY.ID)
                    .from(MEASURABLE_CATEGORY).where(MEASURABLE_CATEGORY.EXTERNAL_ID.eq(CATEGORY_EXTERNAL_ID))))
            .and(MEASURABLE.PROVENANCE.eq("demo")).execute();
    System.out.println("Deleted: " + deletedCount + " existing hierarchy");

    Set<String> topLevelRegions = readRegions();
    Set<String> products = readProducts();

    insertCategoryIfNotExists(dsl);/*from  w  w w.  java2  s. c om*/

    final long measurableCategoryId = dsl.select(MEASURABLE_CATEGORY.ID).from(MEASURABLE_CATEGORY)
            .where(MEASURABLE_CATEGORY.EXTERNAL_ID.eq(CATEGORY_EXTERNAL_ID)).fetchAny().value1();

    AtomicInteger insertCount = new AtomicInteger(0);

    Stream.of(businesses).forEach(business -> {
        final long businessId = dsl.insertInto(MEASURABLE)
                .set(createMeasurable(null, business, measurableCategoryId, true)).returning(MEASURABLE.ID)
                .fetchOne().getId();

        insertCount.incrementAndGet();

        topLevelRegions.forEach((region) -> {
            final long regionId = dsl.insertInto(MEASURABLE)
                    .set(createMeasurable(businessId, region, measurableCategoryId, true))
                    .returning(MEASURABLE.ID).fetchOne().getId();
            insertCount.incrementAndGet();

            products.forEach(product -> {
                dsl.insertInto(MEASURABLE).set(createMeasurable(regionId, product, measurableCategoryId, true))
                        .execute();

                insertCount.incrementAndGet();
            });
        });
    });

    System.out.println("Inserted: " + insertCount + " new nodes for hierarchy");
}

From source file:org.jtheque.undo.UndoServiceTest.java

@Test
@DirtiesContext/*w w  w  . j  av a  2s  . co m*/
public void listenerCalled() {
    final AtomicInteger counter = new AtomicInteger(0);

    undoService.addStateListener(new StateListener() {
        @Override
        public void stateChanged(String undoName, boolean canUndo, String redoName, boolean canRedo) {
            counter.incrementAndGet();
        }
    });

    undoService.addEdit(new TestEdit(new AtomicInteger(0), new AtomicInteger(0)));

    assertEquals(1, counter.intValue());

    undoService.addEdit(new TestEdit(new AtomicInteger(0), new AtomicInteger(0)));

    assertEquals(2, counter.intValue());

    undoService.undo();

    assertEquals(3, counter.intValue());

    undoService.redo();

    assertEquals(4, counter.intValue());
}

From source file:fr.aliasource.webmail.proxy.impl.ProxyImpl.java

public ProxyImpl(ProxyConfiguration conf, CompletionRegistry completionRegistry, LocatorRegistry locator) {
    this.completionRegistry = completionRegistry;
    loginService = new LoginImpl(conf, locator);
    clientReferences = new AtomicInteger(0);
}

From source file:org.jtheque.core.LifeCycleTest.java

@Test
@DirtiesContext//from   w w  w.  ja v  a 2s .  c  o m
public void functionListener() {
    final AtomicInteger functionCounter = new AtomicInteger(0);

    lifeCycle.addFunctionListener(new FunctionListener() {
        @Override
        public void functionUpdated(String function) {
            functionCounter.incrementAndGet();
        }
    });

    lifeCycle.setCurrentFunction("new function");

    assertEquals(1, functionCounter.intValue());
}