当前位置:网站首页>Common tool classes under JUC package
Common tool classes under JUC package
2022-07-25 09:12:00 【Skinny monkey 117】
Catalog
juc Common tool classes under package
juc Common tool classes under package
1. Semaphore ——Semaphore
2. Counter ——CountDownLatch3. Recycling fence ——CyclicBarrier
4. Switch between two threads ——Exchanger
Semaphore ——Semaphore
Semaphore Semaphore It's a counter , Indicates the number of currently available resources .
About semaphores Semaphore There are two core operations
P- Apply for resource operation
V- Release resource operationSemaphore Of PV Addition and subtraction operations are atomic , It can be used directly in multi-threaded scenarios
import java.util.concurrent.Semaphore; /** * Use of semaphores No arguments */ public class SemaphoreTest { public static void main(String[] args) { // Pass in the number of available resources in the construction parameters // Available resources are 5 Semaphore semaphore = new Semaphore(5); Runnable runnable = new Runnable() { @Override public void run() { try { System.out.println(Thread.currentThread().getName() + " Prepare to apply for resources "); // P operation , Every time you apply 1 A resource // Currently, if the available resources are 0, call acquire Your thread will block , Until the occupying thread releases resources semaphore.acquire(); System.out.println(Thread.currentThread().getName() + " Resource acquisition success "); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " Release resources "); // V operation , Every release 1 A resource semaphore.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }; for (int i = 0; i < 10; i++) { Thread t = new Thread(runnable, String.valueOf(i + 1)); t.start(); } } }
The current number of available resources is 5, At this time only 5 Threads got resources successfully . Other threads will block in acquire method ,
Wait for the occupying thread to release resources .import java.util.concurrent.Semaphore; /** * Use of semaphores Ginseng **/ public class SemaphoreTest2 { public static void main(String[] args) { // Pass in the number of available resources in the construction parameters // Available resources are 6 individual Semaphore semaphore = new Semaphore(6); Runnable runnable = new Runnable() { @Override public void run() { try { System.out.println(Thread.currentThread().getName() + " Prepare to apply for resources "); // P operation , Apply for two resources at a time semaphore.acquire(2); System.out.println(Thread.currentThread().getName() + " Resource acquisition success "); Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + " Release resources "); // V operation , Default release 2 Resources occupied semaphore.release(2); }catch (InterruptedException e) { e.printStackTrace(); } } }; for (int i = 0; i < 10; i++) { Thread t = new Thread(runnable,String.valueOf(i + 1)); t.start(); } } }
Counter ——CountDownLatch
CountDownLatch - Large size join Method
call await Method thread , You need to wait for other threads to reduce the counter to 0, To resume execution .import java.util.Random; import java.util.concurrent.CountDownLatch; /** * Use CountDownLatch.await The thread of method will block , Until all waiting threads finish executing * Large advanced version join Method */ public class CountDownLatchTest { public static void main(String[] args) throws InterruptedException { // The number of threads to wait for , Must wait for this 10 After all the sub threads are executed, the execution will be resumed CountDownLatch latch = new CountDownLatch(10); Runnable runnable = new Runnable() { @Override public void run() { try { Thread.sleep(new Random().nextInt(1000)); System.out.println(Thread.currentThread().getName() + " Reach the end point "); // Counter - 1 latch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } } }; for (int i = 0; i < 10; i++) { Thread t = new Thread(runnable, " Athletes " + i); t.start(); } // main Threads are referee threads , Need to wait for all athletes to finish before resuming execution // Until all threads call countdown Method decrements the counter to 0 Carry on latch.await(); System.out.println(" The game is over "); } }
边栏推荐
- 全网最简约的sklearn环境配置教程(百分百成功)
- Comments on specific applications of camera
- 51 single chip microcomputer key control LED light status
- Wechat sports ground reservation applet graduation design of applet completion works (3) background function
- Sticky.js page scrolling div fixed position plug-in
- Solving a random number problem
- JDBC的API解析
- Graduation design of wechat small program ordering system of small program completion works (5) assignment
- Silicon Valley classroom lesson 15 - Tencent cloud deployment
- [graduation project] cinema booking management system based on micro Service Framework
猜你喜欢

Wechat sports ground reservation applet graduation design of applet completion works (3) background function

360度拖拽全景图插件tpanorama.js

Django4.0 + web + MySQL 5.7 realize simple login operation

Overview of redis/mysql knowledge
![[STL]list模拟实现](/img/92/2a78382700c1ebf299c6505d962c9c.png)
[STL]list模拟实现

JS small game source code magic tower breakthrough Download

Solve the syntaxerror: unexpected end of JSON input

51 single chip microcomputer key control LED light status

整理 华为AP-3010DN_V2配置创建wifi
[learn rust together] a preliminary understanding of rust package management tool cargo
随机推荐
Table table expansion internal row switching effect
How to realize the drop-down option box of wechat applet
富文本样式文字图片处理
How to connect tdengine with idea database tool?
Wechat sports ground reservation applet graduation project of applet completion works (1) development outline
BGP border gateway protocol basic knowledge points
The development of art NFT
JDBC的API解析
Silicon Valley class lesson 11 - official account news and wechat authorization
51 MCU internal peripherals: timer and counter
A page widgetization practice
Intelligent operation and maintenance scenario analysis: how to detect abnormal business system status through exception detection
为什么要使用MQ消息中间件?这几个问题必须拿下!
Unity ugui interaction (new ideas)
Solve the syntaxerror: unexpected end of JSON input
Collection of common algorithm questions in test post interview
优炫数据库对数据的加密是如何做的?
Live broadcast preview | how to build an enterprise cloud management platform in the cloudy era?
黑马程序员JDBC
Sort out Huawei ap-3010dn_ V2 configuration create WiFi






