Example usage for java.util.concurrent Semaphore Semaphore

List of usage examples for java.util.concurrent Semaphore Semaphore

Introduction

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

Prototype

public Semaphore(int permits, boolean fair) 

Source Link

Document

Creates a Semaphore with the given number of permits and the given fairness setting.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    Semaphore sem = new Semaphore(1, true);
    Thread thrdA = new Thread(new MyThread(sem, "Message 1"));
    Thread thrdB = new Thread(new MyThread(sem, "Message 2"));

    thrdA.start();//  www . j  a  va 2  s.com
    thrdB.start();

    thrdA.join();
    thrdB.join();

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Semaphore sem = new Semaphore(1, true);
    Thread thrdA = new Thread(new SyncOutput(sem, "Message 1"));
    Thread thrdB = new Thread(new SyncOutput(sem, "Message 2!"));

    thrdA.start();//w ww  .ja  va  2 s .c o  m
    thrdB.start();

    thrdA.join();
    thrdB.join();

}

From source file:org.zkybase.kite.throttle.ThrottleTemplate.java

/**
 * @param limit// w ww.j a va  2 s.  c om
 * @throws IllegalArgumentException if limit < 1
 */
public ThrottleTemplate(int limit) {
    if (limit < 1) {
        throw new IllegalArgumentException("limit must be >= 1");
    }
    this.limit = limit;
    this.semaphore = new Semaphore(limit, true);
}

From source file:org.zkybase.kite.guard.ConcurrencyThrottleTemplate.java

/**
 * Creates a concurrency throttle with the given limit. The throttle rejects requests in excess of the limit.
 * // ww  w. jav  a2  s  . c  om
 * @param limit concurrency limit
 * @throws IllegalArgumentException if limit &lt; 1
 */
public ConcurrencyThrottleTemplate(int limit) {
    if (limit < 1) {
        throw new IllegalArgumentException("limit must be >= 1");
    }
    this.limit = limit;
    this.semaphore = new Semaphore(limit, true);
}

From source file:org.opendaylight.sxp.route.core.RouteReactorZipImpl.java

/**
 * @param delegate service used for delegation
 *//*w  w w.j av a  2 s .  c o  m*/
public RouteReactorZipImpl(RouteReactor delegate) {
    this.delegate = Objects.requireNonNull(delegate);
    compressionQueue = new ArrayBlockingQueue<>(1, true);
    queueGuard = new Semaphore(1, true);
    updateRouteTask = createUpdateRoutingTask();
}

From source file:org.mapfish.print.output.NativeProcessOutputFactory.java

public NativeProcessOutputFactory(int maxProcesses) {
    runningProcesses = new Semaphore(maxProcesses, true);
    if (java.lang.management.ManagementFactory.getOperatingSystemMXBean().getName().toLowerCase()
            .contains("win")) {
        cmd = "convert";
    } else {/*from   ww w  .j  av  a2s . c om*/
        cmd = "/usr/bin/convert";
    }

    cmdArgs.add("-density");
    cmdArgs.add("@@dpi@@x@@dpi@@");
    cmdArgs.add("-append");
    cmdArgs.add("@@sourceFile@@");
    cmdArgs.add("@@targetFile@@");

    formats.add("jpg");
    formats.add("gif");
    formats.add("png");
    formats.add("bmp");
    formats.add("tif");
    formats.add("tiff");
}

From source file:edu.illinois.enforcemop.examples.jbosscache.PessimisticSyncReplTxTest.java

@Before
public void setUp() throws Exception {
    t1_ex = t2_ex = null;// w w  w .  j  av  a 2s .  c  o  m
    lock = new Semaphore(1, true);
    initCaches(Configuration.CacheMode.REPL_SYNC);
}

From source file:org.videolan.myvlc.core.gui.NewSidebarAdapter.java

public NewSidebarAdapter() {
    mInflater = LayoutInflater.from(MyVLCApp.getAppContext());
    mMapFragment = new HashMap<String, Fragment>(lstEntrie.size());
    mMapFragmentAdded = new HashMap<String, Boolean>(lstEntrie.size());
    mSemaphore = new Semaphore(1, true);
}

From source file:org.videolan.vlc.gui.SidebarAdapter.java

public SidebarAdapter() {
    mInflater = LayoutInflater.from(VLCApplication.getAppContext());
    mFragments = new HashMap<String, Fragment>(entries.size());
    mFragmentAdded = new HashMap<String, Boolean>(entries.size());
    mSemaphore = new Semaphore(1, true);
}

From source file:com.indeed.imhotep.web.ExecutionManager.java

private synchronized Semaphore getUserSemaphore(String username) {
    Semaphore semaphore = userToLock.get(username);
    if (semaphore == null) {
        semaphore = new Semaphore(maxQueriesPerUser, true);
        userToLock.put(username, semaphore);
    }//from   w  ww . ja v  a  2s  . co m
    return semaphore;
}