import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MutexExample { private Lock lock = new ReentrantLock(); public void doSomething() { lock.lock(); try { } finally { lock.unlock(); } } } import java.util.concurrent.Semaphore; public class SemaphoreExample { private Semaphore semaphore = new Semaphore(3); public void doSomething() { try { semaphore.acquire(); } catch (InterruptedException e) { } finally { semaphore.release(); } } } import java.util.concurrent.CountDownLatch; public class CountDownLatchExample { private CountDownLatch latch = new CountDownLatch(5); public void doSomething() { try { latch.countDown(); latch.await(); } catch (InterruptedException e) { } } }


上一篇:
下一篇:
切换中文