Package java.util.concurrent.atomic

Examples of java.util.concurrent.atomic.AtomicInteger


      throw new PersistenceException(e);
    }
  }

  private void disposeIfNecessary(IndexSearcher currentSearcher) {
    AtomicInteger referenceCount = fSearchers.get(currentSearcher);
    if (referenceCount.decrementAndGet() == 0 && fSearcher != currentSearcher) {
      try {
        /*
         * May be called by getCurrentSearcher at the same time, but safe
         * because dispose is safe to be called many times for the same
         * searcher.
 
View Full Code Here


    }
  }

  private IndexSearcher createIndexSearcher() throws CorruptIndexException, IOException {
    IndexSearcher searcher = new IndexSearcher(IndexReader.open(fDirectory));
    fSearchers.put(searcher, new AtomicInteger(0));
    return searcher;
  }
View Full Code Here

          IndexReader currentReader = fSearcher.getIndexReader();
          IndexReader newReader = currentReader.reopen();
          if (newReader != currentReader) {

            IndexSearcher newSearcher = new IndexSearcher(newReader);
            fSearchers.put(newSearcher, new AtomicInteger(1));

            /*
             * Assign to field before we check the referenceCount to ensure that
             * disposeIfNecessary will dispose the searcher if it has the last
             * reference, is yet to check if fSearcher has been changed (if this
             * was done after referenceCount.get() == 0, we could leak a
             * searcher).
             */
            fSearcher = newSearcher;

            AtomicInteger referenceCount = fSearchers.get(currentSearcher);
            if (referenceCount != null && referenceCount.get() == 0) {
              /*
               * May be called by disposeIfNecessary at the same time, but safe
               * because dispose is safe to be called many times for the same
               * searcher.
               */
 
View Full Code Here

        /*
         * We block until the current reader has been closed or can be closed.
         * Most times we should be able to succeed without having to sleep.
         */
        while (true) {
          AtomicInteger refCount = fSearchers.get(currentSearcher);
          if (refCount == null)
            break;
          else if (refCount.get() == 0) {
            /*
             * This may be called at the same time from disposeIfNecessary, but
             * that's fine.
             */
            dispose(currentSearcher);
View Full Code Here

    @Test
    public void bufferingData() {
        final TestPipe pipe = new TestPipe("bufferingData");
      
        final AtomicBoolean signalFired = new AtomicBoolean(false);
        final AtomicInteger signalValue = new AtomicInteger(-1);
        final AtomicReference<GstObject> signalSource = new AtomicReference<GstObject>();
        final int PERCENT = 95;
        Bus.BUFFERING signal = new Bus.BUFFERING() {

            public void bufferingData(GstObject source, int percent) {
                signalFired.set(true);
                signalValue.set(percent);
                signalSource.set(source);
                pipe.quit();
            }
        };
        pipe.getBus().connect(signal);
        gst.gst_element_post_message(pipe.src, gst.gst_message_new_buffering(pipe.src, PERCENT));
        pipe.play().run();
        pipe.getBus().disconnect(signal);
        pipe.dispose();
        assertTrue("BUFFERING signal not received", signalFired.get());
        assertEquals("Wrong percent value received for signal", PERCENT, signalValue.get());
        assertEquals("Incorrect source object on signal", pipe.src, signalSource.get());
    }
View Full Code Here

    @Test
    public void testElementAddedCallback() throws Exception {
        Bin bin = new Bin("test");
        final Element e1 = ElementFactory.make("fakesrc", "source");
        final Element e2 = ElementFactory.make("fakesink", "sink");
        final AtomicInteger added = new AtomicInteger(0);
       
        bin.connect(new Bin.ELEMENT_ADDED() {
            public void elementAdded(Bin bin, Element elem) {
                if (elem == e1 || elem == e2) {
                    added.incrementAndGet();
                }
            }
        });
        bin.addMany(e1, e2);
       
        assertEquals("Callback not called", 2, added.get());
    }
View Full Code Here

    @Test
    public void testElementRemovedCallback() {
        Bin bin = new Bin("test");
        final Element e1 = ElementFactory.make("fakesrc", "source");
        final Element e2 = ElementFactory.make("fakesink", "sink");
        final AtomicInteger removed = new AtomicInteger(0);
       
        bin.connect(new Bin.ELEMENT_ADDED() {
            public void elementAdded(Bin bin, Element elem) {
                if (elem == e1 || elem == e2) {
                    removed.incrementAndGet();
                }
            }
        });
        bin.addMany(e1, e2);
       
        assertEquals("Callback not called", 2, removed.get());
    }
View Full Code Here

  @GwtIncompatible("Thread")
  public void testSupplierThreadSafe(
      Function<Supplier<Boolean>, Supplier<Boolean>> memoizer)
      throws Throwable {
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicReference<Throwable> thrown =
        new AtomicReference<Throwable>(null);
    final int numThreads = 3;
    final Thread[] threads = new Thread[numThreads];
    final long timeout = TimeUnit.MINUTES.toNanos(1);

    final Supplier<Boolean> supplier = new Supplier<Boolean>() {
      boolean isWaiting(Thread thread) {
        switch (thread.getState()) {
          case BLOCKED:
          case WAITING:
          case TIMED_WAITING:
          return true;
          default:
          return false;
        }
      }

      int waitingThreads() {
        int waitingThreads = 0;
        for (Thread thread : threads) {
          if (isWaiting(thread)) {
            waitingThreads++;
          }
        }
        return waitingThreads;
      }

      @Override
      public Boolean get() {
        // Check that this method is called exactly once, by the first
        // thread to synchronize.
        long t0 = System.nanoTime();
        while (waitingThreads() != numThreads - 1) {
          if (System.nanoTime() - t0 > timeout) {
            thrown.set(new TimeoutException(
                "timed out waiting for other threads to block" +
                " synchronizing on supplier"));
            break;
          }
          Thread.yield();
        }
        count.getAndIncrement();
        return Boolean.TRUE;
      }
    };

    final Supplier<Boolean> memoizedSupplier = memoizer.apply(supplier);

    for (int i = 0; i < numThreads; i++) {
      threads[i] = new Thread() {
        @Override public void run() {
          assertSame(Boolean.TRUE, memoizedSupplier.get());
        }
      };
    }
    for (Thread t : threads) {
      t.start();
    }
    for (Thread t : threads) {
      t.join();
    }

    if (thrown.get() != null) {
      throw thrown.get();
    }
    assertEquals(1, count.get());
  }
View Full Code Here

          }
          @Override
          public int getCount() {
            int count = mapEntry.getValue().get();
            if (count == 0) {
              AtomicInteger frequency = backingMap.get(getElement());
              if (frequency != null) {
                count = frequency.get();
              }
            }
            return count;
          }
        };
View Full Code Here

    }
  }

  @Override public int count(@Nullable Object element) {
    try {
      AtomicInteger frequency = backingMap.get(element);
      return (frequency == null) ? 0 : frequency.get();
    } catch (NullPointerException e) {
      return 0;
    } catch (ClassCastException e) {
      return 0;
    }
View Full Code Here

TOP

Related Classes of java.util.concurrent.atomic.AtomicInteger

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.