Paw*_*wan 4 java concurrency synchronization
class Semaphore {
private int count=100;
public Semaphore(int n) {
this.count = n;
}
public synchronized void acquire() {
while(count == 0) {
try {
wait();
} catch (InterruptedException e) {
//keep trying
}
}
count--;
}
public synchronized void release() {
count++;
notify(); //alert a thread that's blocking on this semaphore
}
}
Run Code Online (Sandbox Code Playgroud)
目前我支持100个用户.如果一个请求来自于JSP(客户端),并通过这个类去,将线程(从JSP请求)wait和notify自动?
我强烈建议使用标准库中的java.util.concurrent.Semaphore,而不是编写自定义Semaphore实现.只提到为什么这通常是一个更好的想法的一个原因:notify不提供任何FIFO保证(来自Object.notify()):
选择是任意的,由实施决定