Java多线程Semaphore工具的使用详解
Semaphore是Java中的一个线程同步工具,可以用于控制线程的并发数,也可以用于多个线程之间的互斥访问。
Semaphore的特性
Semaphore主要有以下特性:
- 控制并发数:Semaphore可以限制并发线程数,保证同时运行的线程数量不超过Semaphore的指定值。
- 互斥访问:Semaphore可以用于控制对于共享资源的互斥访问。
- 不可重入性:Semaphore是不可重入的,即同一个线程多次申请Semaphore锁时会阻塞。
Semaphore的方法
Semaphore类主要有以下方法:
- acquire():申请一个Semaphore的锁。
- acquire(int permits):申请指定数量的Semaphore锁。
- tryAcquire():尝试申请一个Semaphore的锁。
- tryAcquire(long timeout, TimeUnit unit):尝试申请一个Semaphore的锁,最多等待指定时间。
- release():释放一个Semaphore的锁。
- release(int permits):释放指定数量的Semaphore锁。
示例1:控制线程并发数
下面是一个示例,用Semaphore来限制线程的并发数为3:
public class Example1 {
private static final int THREAD_COUNT = 10;
private static final int THREAD_LIMIT = 3;
private static Semaphore semaphore = new Semaphore(THREAD_LIMIT);
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < THREAD_COUNT; i++) {
executorService.execute(() -> {
try {
semaphore.acquire();
System.out.println("Thread " + Thread.currentThread().getName() + " is running");
TimeUnit.SECONDS.sleep(2);
semaphore.release();
System.out.println("Thread " + Thread.currentThread().getName() + " released the lock");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
}
输出结果为:
Thread pool-1-thread-1 is running
Thread pool-1-thread-3 is running
Thread pool-1-thread-2 is running
Thread pool-1-thread-2 released the lock
Thread pool-1-thread-1 released the lock
Thread pool-1-thread-3 released the lock
Thread pool-1-thread-5 is running
Thread pool-1-thread-4 is running
Thread pool-1-thread-6 is running
Thread pool-1-thread-4 released the lock
Thread pool-1-thread-5 released the lock
Thread pool-1-thread-6 released the lock
Thread pool-1-thread-8 is running
Thread pool-1-thread-7 is running
Thread pool-1-thread-9 is running
Thread pool-1-thread-7 released the lock
Thread pool-1-thread-8 released the lock
Thread pool-1-thread-9 released the lock
Thread pool-1-thread-10 is running
Thread pool-1-thread-10 released the lock
示例2:控制对共享资源的互斥访问
下面是一个示例,用Semaphore来控制对于共享资源的互斥访问:
public class Example2 {
private static final int THREAD_COUNT = 10;
private static Semaphore semaphore = new Semaphore(1);
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < THREAD_COUNT; i++) {
executorService.execute(() -> {
try {
semaphore.acquire();
System.out.println("Thread " + Thread.currentThread().getName() + " is accessing the shared resource");
TimeUnit.SECONDS.sleep(2);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
}
}
输出结果为:
Thread pool-1-thread-1 is accessing the shared resource
Thread pool-1-thread-1 released the lock
Thread pool-1-thread-2 is accessing the shared resource
Thread pool-1-thread-2 released the lock
Thread pool-1-thread-3 is accessing the shared resource
Thread pool-1-thread-3 released the lock
Thread pool-1-thread-4 is accessing the shared resource
Thread pool-1-thread-4 released the lock
Thread pool-1-thread-5 is accessing the shared resource
Thread pool-1-thread-5 released the lock
Thread pool-1-thread-7 is accessing the shared resource
Thread pool-1-thread-7 released the lock
Thread pool-1-thread-6 is accessing the shared resource
Thread pool-1-thread-6 released the lock
Thread pool-1-thread-9 is accessing the shared resource
Thread pool-1-thread-9 released the lock
Thread pool-1-thread-10 is accessing the shared resource
Thread pool-1-thread-10 released the lock
Thread pool-1-thread-8 is accessing the shared resource
Thread pool-1-thread-8 released the lock
总结
通过Semaphore可以控制并发线程数,保证程序的可靠性和稳定性。同时,Semaphore还可以用于控制对于共享资源的互斥访问。在使用Semaphore时,应该根据具体场景确定Semaphore的数量和使用方式。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java多线程Semaphore工具的使用详解 - Python技术站