@@ -1109,13 +1109,58 @@ public CountDownLatch(int count) { }; // 参数count为计数值
11091109然后下面这 3 个方法是 CountDownLatch 类中最重要的方法:
11101110
11111111``` java
1112- `public ` `void ` `await() ``throws` `InterruptedException { }; ``// 调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行``public` `boolean` `await(``long` `timeout, TimeUnit unit) ``throws` `InterruptedException { }; ``//和await()类似,只不过等待一定的时间后count值还没变为0的话就会继续执行``public` `void` `countDown() { }; ``//将count值减1`
1112+ // 调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行
1113+ public void await() throws InterruptedException { };
1114+ // 和await()类似,只不过等待一定的时间后count值还没变为0的话就会继续执行
1115+ public boolean await(long timeout, TimeUnit unit) throws InterruptedException { };
1116+ // 将count值减1
1117+ public void countDown() { };
11131118```
11141119
11151120下面看一个例子大家就清楚 CountDownLatch 的用法了:
11161121
11171122``` java
1118- `public` `class` `Test {`` ``public` `static` `void` `main(String[] args) { `` ``final` `CountDownLatch latch = ``new` `CountDownLatch(``2``);`` ` ` ``new` `Thread(){`` ``public` `void` `run() {`` ``try` `{`` ``System.out.println(``"子线程"``+Thread.currentThread().getName()+``"正在执行"``);`` ``Thread.sleep(``3000``);`` ``System.out.println(``"子线程"``+Thread.currentThread().getName()+``"执行完毕"``);`` ``latch.countDown();`` ``} ``catch` `(InterruptedException e) {`` ``e.printStackTrace();`` ``}`` ``};`` ``}.start();`` ` ` ``new` `Thread(){`` ``public` `void` `run() {`` ``try` `{`` ``System.out.println(``"子线程"``+Thread.currentThread().getName()+``"正在执行"``);`` ``Thread.sleep(``3000``);`` ``System.out.println(``"子线程"``+Thread.currentThread().getName()+``"执行完毕"``);`` ``latch.countDown();`` ``} ``catch` `(InterruptedException e) {`` ``e.printStackTrace();`` ``}`` ``};`` ``}.start();`` ` ` ``try` `{`` ``System.out.println(``"等待2个子线程执行完毕..."``);`` ``latch.await();`` ``System.out.println(``"2个子线程已经执行完毕"``);`` ``System.out.println(``"继续执行主线程"``);`` ``} ``catch` `(InterruptedException e) {`` ``e.printStackTrace();`` ``}`` ``}``}`
1123+ public class Test {
1124+ public static void main (String [] args ) {
1125+ final CountDownLatch latch = new CountDownLatch (2 );
1126+ new Thread () {
1127+ public void run () {
1128+ try {
1129+ System . out. println(" 子线程" + Thread . currentThread(). getName() + " 正在执行" );
1130+ Thread . sleep(3000 );
1131+ System . out. println(" 子线程" + Thread . currentThread(). getName() + " 执行完毕" );
1132+ latch. countDown();
1133+ } catch (InterruptedException e) {
1134+ e. printStackTrace();
1135+ }
1136+ }
1137+
1138+ ;
1139+ }. start();
1140+ new Thread () {
1141+ public void run () {
1142+ try {
1143+ System . out. println(" 子线程" + Thread . currentThread(). getName() + " 正在执行" );
1144+ Thread . sleep(3000 );
1145+ System . out. println(" 子线程" + Thread . currentThread(). getName() + " 执行完毕" );
1146+ latch. countDown();
1147+ } catch (InterruptedException e) {
1148+ e. printStackTrace();
1149+ }
1150+ }
1151+
1152+ ;
1153+ }. start();
1154+ try {
1155+ System . out. println(" 等待2个子线程执行完毕..." );
1156+ latch. await();
1157+ System . out. println(" 2个子线程已经执行完毕" );
1158+ System . out. println(" 继续执行主线程" );
1159+ } catch (InterruptedException e) {
1160+ e. printStackTrace();
1161+ }
1162+ }
1163+ }
11191164```
11201165
11211166执行结果:
@@ -1182,13 +1227,28 @@ Semaphore 就是操作系统中的信号量,可以控制对互斥资源的访
11821227Semaphore 类位于 java.util.concurrent 包下,它提供了2个构造器:
11831228
11841229``` java
1185- `public ` `Semaphore(``int ` `permits) { ``// 参数permits表示许可数目,即同时可以允许多少线程进行访问`` ``sync = ``new` `NonfairSync(permits);``}``public` `Semaphore(``int` `permits, ``boolean` `fair) { ``//这个多了一个参数fair表示是否是公平的,即等待时间越久的越先获取许可`` ``sync = (fair)? ``new` `FairSync(permits) : ``new` `NonfairSync(permits);``}`
1230+ public Semaphore(int permits) {
1231+ // 参数permits表示许可数目,即同时可以允许多少线程进行访问
1232+ sync = new NonfairSync (permits);
1233+ }
1234+
1235+ public Semaphore(int permits, boolean fair) {
1236+ // 这个多了一个参数fair表示是否是公平的,即等待时间越久的越先获取许可
1237+ sync = (fair) ? new FairSync (permits) : new NonfairSync (permits);
1238+ }
11861239```
11871240
11881241下面说一下 Semaphore 类中比较重要的几个方法,首先是 acquire()、release() 方法:
11891242
11901243``` java
1191- `public ` `void ` `acquire() ``throws` `InterruptedException { } ``// 获取一个许可``public` `void` `acquire(``int` `permits) ``throws` `InterruptedException { } ``//获取permits个许可``public` `void` `release() { } ``//释放一个许可``public` `void` `release(``int` `permits) { } ``//释放permits个许可`
1244+ // 获取一个许可
1245+ public void acquire() throws InterruptedException { }
1246+ // 获取permits个许可
1247+ public void acquire(int permits) throws InterruptedException { }
1248+ // 释放一个许可
1249+ public void release() { }
1250+ // 释放permits个许可
1251+ public void release(int permits) { }
11921252```
11931253
11941254 acquire() 用来获取一个许可,若无许可能够获得,则会一直等待,直到获得许可。
@@ -1198,7 +1258,14 @@ Semaphore 类位于 java.util.concurrent 包下,它提供了2个构造器:
11981258这 4 个方法都会被阻塞,如果想立即得到执行结果,可以使用下面几个方法:
11991259
12001260``` java
1201- `public ` `boolean ` `tryAcquire() { }; ``// 尝试获取一个许可,若获取成功,则立即返回true,若获取失败,则立即返回false``public` `boolean` `tryAcquire(``long` `timeout, TimeUnit unit) ``throws` `InterruptedException { }; ``//尝试获取一个许可,若在指定的时间内获取成功,则立即返回true,否则则立即返回false``public` `boolean` `tryAcquire(``int` `permits) { }; ``//尝试获取permits个许可,若获取成功,则立即返回true,若获取失败,则立即返回false``public` `boolean` `tryAcquire(``int` `permits, ``long` `timeout, TimeUnit unit) ``throws` `InterruptedException { }; ``//尝试获取permits个许可,若在指定的时间内获取成功,则立即返回true,否则则立即返回false`
1261+ // 尝试获取一个许可,若获取成功,则立即返回true,若获取失败,则立即返回false
1262+ public boolean tryAcquire() { };
1263+ // 尝试获取一个许可,若在指定的时间内获取成功,则立即返回true,否则则立即返回false
1264+ public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException { };
1265+ // 尝试获取permits个许可,若获取成功,则立即返回true,若获取失败,则立即返回false
1266+ public boolean tryAcquire(int permits) { };
1267+ // 尝试获取permits个许可,若在指定的时间内获取成功,则立即返回true,否则则立即返回false
1268+ public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException { };
12021269```
12031270
12041271 另外还可以通过 availablePermits() 方法得到可用的许可数目。
@@ -1208,7 +1275,36 @@ Semaphore 类位于 java.util.concurrent 包下,它提供了2个构造器:
12081275 假若一个工厂有 5 台机器,但是有 8 个工人,一台机器同时只能被一个工人使用,只有使用完了,其他工人才能继续使用。那么我们就可以通过 Semaphore 来实现:
12091276
12101277``` java
1211- `public` `class` `Test {`` ``public` `static` `void` `main(String[] args) {`` ``int` `N = ``8``; ``//工人数`` ``Semaphore semaphore = ``new` `Semaphore(``5``); ``//机器数目`` ``for``(``int` `i=``0``;i<N;i++)`` ``new` `Worker(i,semaphore).start();`` ``}`` ` ` ``static` `class` `Worker ``extends` `Thread{`` ``private` `int` `num;`` ``private` `Semaphore semaphore;`` ``public` `Worker(``int` `num,Semaphore semaphore){`` ``this``.num = num;`` ``this``.semaphore = semaphore;`` ``}`` ` ` ``@Override`` ``public` `void` `run() {`` ``try` `{`` ``semaphore.acquire();`` ``System.out.println(``"工人"``+``this``.num+``"占用一个机器在生产..."``);`` ``Thread.sleep(``2000``);`` ``System.out.println(``"工人"``+``this``.num+``"释放出机器"``);`` ``semaphore.release(); `` ``} ``catch` `(InterruptedException e) {`` ``e.printStackTrace();`` ``}`` ``}`` ``}``}`
1278+ public class Test {
1279+ public static void main (String [] args ) {
1280+ int N = 8 ; // 工人数
1281+ Semaphore semaphore = new Semaphore (5 ); // 机器数目
1282+ for (int i = 0 ; i < N ; i++ ) new Worker (i, semaphore). start();
1283+ }
1284+
1285+ static class Worker extends Thread {
1286+ private int num;
1287+ private Semaphore semaphore;
1288+
1289+ public Worker (int num , Semaphore semaphore ) {
1290+ this . num = num;
1291+ this . semaphore = semaphore;
1292+ }
1293+
1294+ @Override
1295+ public void run () {
1296+ try {
1297+ semaphore. acquire();
1298+ System . out. println(" 工人" + this . num + " 占用一个机器在生产..." );
1299+ Thread . sleep(2000 );
1300+ System . out. println(" 工人" + this . num + " 释放出机器" );
1301+ semaphore. release();
1302+ } catch (InterruptedException e) {
1303+ e. printStackTrace();
1304+ }
1305+ }
1306+ }
1307+ }
12121308```
12131309
12141310执行结果:
0 commit comments