无场景不成方圆
应用场景1:用于做流量控制,特别是公用资源比较有限的场景,比如数据库连接。假如有一个需求,要读取几万个文件的数据,因为都是IO密集型任务,我们可以启动几十个线程并发的读取,但如果读到内存后,还需要存储到数据库中,而数据库的连接数只有10个,这时我们必须控制只有10个线程同时获取数据库连接保存数据,否则就会报无法获取数据库连接。这个时候,就可以用Semaphore来做流量控制。
应用场景1代码如下:
package mytest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreTest {
private static final int THREAD_COUNT = 30;
private static ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
private static Semaphore s = new Semaphore(10);
public static void main(String[] args) {
for (int i = 0; i < THREAD_COUNT; i++) {
threadPool.execute(new Runnable() {
@Override
public void run() {
try {
s.acquire();
System.out.println("save data");
} catch (InterruptedException e) {
// TODO: handle exception
} finally {
s.release();
}
}
});
}
threadPool.shutdown();
}
}
应用场景2:办公场所有3台打印机,但是员工的数量很多,此时就应该限制同时使用打印机的人数,此时可以使用Semaphore来控制使用打印机的员工数到3个人,但是获取打印机的过程需要同步执行,避免两个人获取到同一台打印机
应用场景2代码如下:
package mytest;
import java.util.Random;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintQueue {
private boolean freePrinters[];
private Lock lockPrinters;
private Semaphore semaphore;
public PrintQueue() {
semaphore = new Semaphore(3);
freePrinters = new boolean[3];
for (int i = 0; i < 3; i++) {
freePrinters[i] = true;
}
lockPrinters = new ReentrantLock();
}
public void printJob(Object document) {
try {
semaphore.acquire();
int assignedPrinter = getPrinter();
TimeUnit.SECONDS.sleep(new Random().nextInt(20));
freePrinters[assignedPrinter] = true;
} catch (Exception e) {
// TODO: handle exception
} finally {
semaphore.release();
}
}
private int getPrinter() {
int ret = -1;
lockPrinters.lock();
try {
for (int i = 0; i < freePrinters.length; i++) {
if (freePrinters[i]) {
ret = i;
freePrinters[i] = false;
break;
}
}
} finally {
lockPrinters.unlock();
}
return ret;
}
}
场景1引自《Java并发编程的艺术》
场景2引自《Java7并发编程 实战手册》