You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
704 B

package UnitTest;
import java.util.concurrent.Semaphore;
public class MultiThreadTool {
private Semaphore semaphore;
public MultiThreadTool(int threadCount) {
semaphore = new Semaphore(threadCount);
}
public void execute(Runnable task) {
try {
semaphore.acquire(); // 获取一个许可
Thread thread = new Thread(() -> {
try {
task.run();
} finally {
semaphore.release(); // 释放许可
}
});
thread.start();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}