package com.yhm.testSpringMvc.Thread;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class SemaphoreTest {
public static void main(String[] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
Semaphore semaphore = new Semaphore(3);
for (int i = 0; i < 999; i++) {
executorService.submit(new Runnable() {
@Override
public void run() {
int time = new Random().nextInt(9999);
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "_睡眠了" + time);
semaphore.release();
}
});
}
executorService.shutdown();
}
}
执行结果:
D:\JAVA\jdk8_64\bin\java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:58069,suspend=y,server=n -javaagent:C:\Users\Lenovo.IntelliJIdea2017.3\system\captureAgent\debugger-agent.jar=C:\Users\Lenovo\AppData\Local\Temp\capture11664.props -Dfile.encoding=UTF-8 -classpath "D:\JAVA\jdk8_64\jre\lib\charsets.jar;D:\JAVA\jdk8_64\jre\lib\deploy.jar;D:\JAVA\jdk8_64\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk8_64\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk8_64\jre\lib\ext\dnsns.jar;D:\JAVA\jdk8_64\jre\lib\ext\jaccess.jar;D:\JAVA\jdk8_64\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk8_64\jre\lib\ext\localedata.jar;D:\JAVA\jdk8_64\jre\lib\ext\nashorn.jar;D:\JAVA\jdk8_64\jre\lib\ext\sunec.jar;D:\JAVA\jdk8_64\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk8_64\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk8_64\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk8_64\jre\lib\ext\zipfs.jar;D:\JAVA\jdk8_64\jre\lib\javaws.jar;D:\JAVA\jdk8_64\jre\lib\jce.jar;D:\JAVA\jdk8_64\jre\lib\jfr.jar;D:\JAVA\jdk8_64\jre\lib\jfxswt.jar;D:\JAVA\jdk8_64\jre\lib\jsse.jar;D:\JAVA\jdk8_64\jre\lib\management-agent.jar;D:\JAVA\jdk8_64\jre\lib\plugin.jar;D:\JAVA\jdk8_64\jre\lib\resources.jar;D:\JAVA\jdk8_64\jre\lib\rt.jar;D:\ideaPros\codeGitee\ACM\target\classes;D:\repo\junit\junit\4.12\junit-4.12.jar;D:\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\repo\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar;D:\repo\org\springframework\spring-core\4.2.5.RELEASE\spring-core-4.2.5.RELEASE.jar;D:\repo\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\repo\org\springframework\spring-beans\4.2.5.RELEASE\spring-beans-4.2.5.RELEASE.jar;D:\repo\org\springframework\spring-context\4.2.5.RELEASE\spring-context-4.2.5.RELEASE.jar;D:\repo\org\springframework\spring-aop\4.2.5.RELEASE\spring-aop-4.2.5.RELEASE.jar;D:\repo\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;D:\repo\org\springframework\spring-expression\4.2.5.RELEASE\spring-expression-4.2.5.RELEASE.jar;D:\repo\org\springframework\spring-tx\4.2.5.RELEASE\spring-tx-4.2.5.RELEASE.jar;D:\repo\org\springframework\spring-web\4.2.5.RELEASE\spring-web-4.2.5.RELEASE.jar;D:\repo\org\springframework\spring-webmvc\4.2.5.RELEASE\spring-webmvc-4.2.5.RELEASE.jar;D:\repo\org\springframework\spring-test\4.2.5.RELEASE\spring-test-4.2.5.RELEASE.jar;D:\repo\org\slf4j\slf4j-log4j12\1.6.6\slf4j-log4j12-1.6.6.jar;D:\repo\org\slf4j\slf4j-api\1.6.6\slf4j-api-1.6.6.jar;D:\repo\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\repo\cglib\cglib-nodep\3.2.2\cglib-nodep-3.2.2.jar;D:\repo\org\aspectj\aspectjweaver\1.7.4\aspectjweaver-1.7.4.jar;D:\repo\com\alibaba\dubbo\2.5.3\dubbo-2.5.3.jar;D:\repo\org\springframework\spring\2.5.6.SEC03\spring-2.5.6.SEC03.jar;D:\repo\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;D:\repo\org\jboss\netty\netty\3.2.5.Final\netty-3.2.5.Final.jar;D:\chengxu\intelliJIDEA\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar" com.yhm.testSpringMvc.Thread.SemaphoreTest
Connected to the target VM, address: '127.0.0.1:58069', transport: 'socket'
pool-1-thread-6_睡眠了 1945
pool-1-thread-5_睡眠了 204
pool-1-thread-1_睡眠了 3311
pool-1-thread-2_睡眠了 4171
pool-1-thread-7_睡眠了 5469
pool-1-thread-4_睡眠了 8175
pool-1-thread-3_睡眠了 6203
pool-1-thread-10_睡眠了 3853
pool-1-thread-9_睡眠了 5954
pool-1-thread-8_睡眠了 7652
pool-1-thread-11_睡眠了 8733
pool-1-thread-12_睡眠了 7894
pool-1-thread-13_睡眠了 9047
pool-1-thread-14_睡眠了 6079
pool-1-thread-15_睡眠了 4987
pool-1-thread-16_睡眠了 7699
pool-1-thread-18_睡眠了 4287
pool-1-thread-19_睡眠了 2121
pool-1-thread-17_睡眠了 8863
pool-1-thread-20_睡眠了 5647
pool-1-thread-22_睡眠了 5091
pool-1-thread-23_睡眠了 4330
pool-1-thread-21_睡眠了 9225
pool-1-thread-25_睡眠了 1597
pool-1-thread-26_睡眠了 2666
pool-1-thread-27_睡眠了 2710
pool-1-thread-24_睡眠了 7631
pool-1-thread-30_睡眠了 3075
pool-1-thread-29_睡眠了 7822
pool-1-thread-28_睡眠了 8538
pool-1-thread-32_睡眠了 3205
pool-1-thread-33_睡眠了 3575
pool-1-thread-31_睡眠了 8938
pool-1-thread-35_睡眠了 5823
pool-1-thread-37_睡眠了 1192
pool-1-thread-36_睡眠了 4203
pool-1-thread-34_睡眠了 8229
pool-1-thread-39_睡眠了 1466
pool-1-thread-40_睡眠了 2624
pool-1-thread-42_睡眠了 1775
pool-1-thread-38_睡眠了 5461
pool-1-thread-41_睡眠了 4901
pool-1-thread-44_睡眠了 3605
pool-1-thread-43_睡眠了 5416
pool-1-thread-47_睡眠了 1754
pool-1-thread-48_睡眠了 261
pool-1-thread-46_睡眠了 7617
pool-1-thread-45_睡眠了 5976
pool-1-thread-49_睡眠了 3919
cpool-1-thread-52_睡眠了 3054
pool-1-thread-53_睡眠了 1236
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于