本文主要是介绍【Java并发编程十】同步控制二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Semaphore 信号量
Semaphore可以减少负载,使限制同时运行的线程数量。Semaphore的执行过程是先获取一定数量的线程,执行完一个释放一个semaphore,再去执行一个新的线程。
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class myTest implements Runnable{// semaphore表示信号量final Semaphore semaphore = new Semaphore(5);public static void main(String[] args) {// 线程池ExecutorService executorService = Executors.newFixedThreadPool(20);final myTest test = new myTest();for(int i=0; i<20; i++) {executorService.submit(test);}}@Overridepublic void run() {try {long startTime = System.currentTimeMillis();// semaphore数量超过限制的数量,自身便会阻塞semaphore.acquire();Thread.sleep(2000);semaphore.release();long endTime = System.currentTimeMillis();System.out.println(Thread.currentThread().getId()+" cost " + (endTime-startTime) + " ms");} catch (InterruptedException e) {throw new RuntimeException(e);}}
}/**
由于设置semaphore最大数量为5,所以同一时刻最多有五个线程同时运行。
25 cost 2010 ms
27 cost 2010 ms
26 cost 2010 ms
24 cost 2010 ms
28 cost 2010 ms
33 cost 4010 ms
30 cost 4010 ms
29 cost 4019 ms
31 cost 4018 ms
32 cost 4018 ms
38 cost 6012 ms
35 cost 6012 ms
37 cost 6019 ms
43 cost 6019 ms
36 cost 6028 ms
*/
CountDownLatch 倒计时器
CountDowmLatch是每次等到一定数量,end.await() 才会放行。
package myTest;import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;public class myTest implements Runnable{static final CountDownLatch end = new CountDownLatch(10);static final myTest test = new myTest();public static void main(String[] args) throws InterruptedException {ExecutorService executorService = Executors.newFixedThreadPool(10);for(int i=0; i<20; i++) {executorService.submit(test);}// 等待所有线程就绪end.await();System.out.println("开始");executorService.shutdown();}@Overridepublic void run() {try {long startTime = System.currentTimeMillis();Thread.sleep(new Random().nextInt(10)*100);long endTime = System.currentTimeMillis();System.out.println("共执行 "+ (endTime-startTime) + " ms");end.countDown();} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
CyclicBarrier 循环栅栏
CyclicBarrier可以重复被利用,而CountDownLatch不行。
CyclicBarrier正如它的名字一样,循环栅栏,栅栏是一个阻挡别人进入的障碍物,CyclicBarrier和CountDownLatch一样有一个计数器,不过CountDownLatch的计数器被定义了之后就只能被一直减少,最后减少到0时,完全结束,而CyclicBarrier的计数器则是从0开始增加,直到指定数值时开始放行,然后计数器归零,并等待下一波线程的访问。
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;public class myTest implements Runnable{static final CyclicBarrier end = new CyclicBarrier(5);static final myTest test = new myTest();public static void main(String[] args) throws InterruptedException {ExecutorService executorService = Executors.newFixedThreadPool(20);for(int i=0; i<20; i++) {executorService.submit(test);}executorService.shutdown();}@Overridepublic void run() {try {long startTime = System.currentTimeMillis();Thread.sleep(new Random().nextInt(10)*100);end.await();System.out.println(Thread.currentThread().getId()+"集合完成");end.await();long endTime = System.currentTimeMillis();System.out.println(Thread.currentThread().getId() + " 执行完毕: "+ endTime + " ms");} catch (InterruptedException e) {throw new RuntimeException(e);} catch (BrokenBarrierException e) {throw new RuntimeException(e);}}
}
这篇关于【Java并发编程十】同步控制二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!