主程序
package lock;
import java.util.concurrent.Semaphore;
public class TestLock {
public static void main(String[] args) {
Semaphore semaphore = new Semaphore(2);
for (int j = 0; j < 10; j++) {
new Thread(() -> {
try {
semaphore.acquire(1);
Thread.sleep(2000);
System.out.println(111);
semaphore.release(1);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
}
}).start();
}
}
}