JUC学习笔记(7)—CountDownLatch

JUC学习 - CountDownLatch 一、JUC中的CountDownLatch 思考一个需求 假如有这样一个需求,当我们需要解析一个Excel里多个sheet的数据时,可以考虑使用多线程,每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要统计解析总耗时。分析一下:解析每个sheet耗时可能不一样,总耗时就是最长耗时的那个操作。 我们能够想到的最简单的做法是使用join,代码如下: public class ThreadTest { public static class T extends T 阅读详情

 让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒。
 
 CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,这些线程会阻塞。其它线程调用countDown方法会将计数器减1(调用countDown方法的线程不会阻塞), 当计数器的值变为0时,因await方法阻塞的线程会被唤醒,继续执行。 解释:5个同学陆续离开教室后值班同学才可以关门。也即    秦灭6国,一统华夏main主线程必须要等前面5个线程完成全部工作后,自己才能开干

 

public class CountDownLatchDemo
{
	public static void main(String[] args) throws InterruptedException
	{
		//秦灭6国,一统华夏
		CountDownLatch cdl = new CountDownLatch(6);
		for (int i = 1; i <=6; i++) 
		{
			new Thread(() -> {
				System.out.println(Thread.currentThread().getName()+"\t 国被灭");
				cdl.countDown();
			},CountryEnums.forEachCountryEnums(i).getRetMsg()).start();
		}
		cdl.await();
		System.out.println(Thread.currentThread().getName()+"*********秦灭6国,一统华夏");		
	}
}

public enum CountryEnums {
	ONE(1,"韩"),TWO(2,"魏"),THREE(3,"赵"),FOUR(4,"燕"),FIVE(5,"齐"),SIX(6,"楚");
	
	private Integer retCode;
	private String retMsg;
	
	
	private CountryEnums(Integer retCode, String retMsg) {
		this.retCode = retCode;
		this.retMsg = retMsg;
	}
	
	public Integer getRetCode() {
		return retCode;
	}
	public void setRetCode(Integer retCode) {
		this.retCode = retCode;
	}
	public String getRetMsg() {
		return retMsg;
	}
	public void setRetMsg(String retMsg) {
		this.retMsg = retMsg;
	}
	
	public static CountryEnums forEachCountryEnums(Integer index) {
		for (CountryEnums enums : values()) {
			if(enums.getRetCode() == index) {
				return enums;
			}
		}
		return null;
	}
	

}

 

MTK Linux Charger驱动分析(二) - power_supply_core.c 的核心作用可以概括为:提供统一的电源管理框架;管理电源设备注册、属性访问、事件通知;封装sysfs 接口和 uevent 通知,屏蔽硬件细节;驱动开发者只需要实现,不关心底层通知和接口细节。简单来说,它是电源子系统的“大脑和中枢”,具体的电池/充电器只是“信息源”。 阅读详情

相关推荐

【WSL2 Win10】解决子系统中nividia-smi出现的Failed to initialize NVML GPU access blocked by the operating systeM

环境:WIN10专业版 WSL2 Ubuntu x64架构 尝试在WSL2中装CUDA,装好以后在wsl系统中输入nvidia-smi命令,结果报错。但是在Win10的CMD中正常显示 Failed to initialize NVML: GPU access blocked by the operating system Failed to properly shut down NVML: GPU access blocked by the operating system在网上搜了一下,没太多类似的情况

6746

JUC之常用辅助类CountDownLatch

CountDownLatch: 1.背景: countDownLatch是在java1.5被引入,跟它一起被引入的工具类还有CyclicBarrier、Semaphore、concurrentHashMap和BlockingQueue。 存在于java.util.cucurrent包下。 2.概念: countDownLatch这个类使一个线程等待其他线程各自执行完毕后再执行。 CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信(而不是用作互斥的作用).

weixin_38568503的博客 351

Ubuntu20.04配置PX4环境启动或编译jmavsim 出现的错误及解决办法

错误1:ant命令未找到

Iamsonice的博客 8856

JUC之countDownLatch详解

CountDownLatch 一:CountDownLatch介绍 CountDownLatch 是一个多线程控制工具类,用来控制线程等待,它可以让某个线程等待直到倒计时结束,再执行。 二:案例 API: public CountDownLatch (int count) 模拟一个场景,现在有3个服务,我们必须等待3个服务全部完成之后才能调用主线程 public class CountDow...

mindcarver 1022

2026年高教社杯数学建模国赛A题解题思路:从物理方程推导到MATLAB高效求解的全流程实战指南

□所有代码能够从头运行到尾,不报错(清除workspace重新跑一遍)□论文中的每个图号、表号、公式编号与正文引用一致□附录中附上核心代码(注意:不是全部代码,而是关键求解器和后处理部分)□支撑材料压缩包包含完整代码、数据文件、README说明□摘要字数严格控制在1000字以内,突出创新点和核心结果□。

YOLO 110

宇树科技嵌入式面试题及参考答案(春晚机器人的公司)

内存对齐是指在内存中为数据对象分配存储空间时,按照一定的规则将数据存储在特定的地址上,以提高内存访问效率。不同的数据类型在内存中都有其特定的对齐要求,例如,int类型通常要求 4 字节对齐,double类型通常要求 8 字节对齐。这意味着数据的存储地址必须是其对齐字节数的整数倍。如果不进行内存对齐,处理器在访问数据时可能需要进行多次内存访问,从而降低了访问效率。指令可以用于控制结构体内存布局,它允许程序员指定结构体成员的对齐方式。其基本用法是在结构体定义之前使用来指定对齐字节数n,其中n。

大模型大数据攻城狮的专栏 3226

JUC工具类: CountDownLatch详解

文章目录一、CountDownLatch介绍二、CountDownLatch源码分析1、类的继承关系2、类的内部类3、类的属性4、类的构造函数5、核心函数 - await函数6、核心函数 - countDown函数三、CountDownLatch示例四、更深入理解(通过面试题理解)面试问题1、什么是CountDownLatch?2、CountDownLatch底层实现原理?3、CountDownLatch一次可以唤醒几个任务?4、多个 CountDownLatch有哪些主要方法?5、 CountDownLa

qq_38327769的博客 1031

南方电网专业面试题(电气类-继电保护)(目录)

南方电网专业面试题(电气类-继电保护)(目录) 这些面试题围绕继电保护的基本概念、工作原理、分类、性能要求,以及变压器、断路器、母线保护、自动重合闸等电力系统关键设备的保护机制和操作原则展开,旨在评估应聘者对电力系统保护和控制策略的深入理解和实际应用能力。

weixin_44808274的博客 2159

Java JUC之CountDownLatch、CyclicBarrier以及Semaphore

对JUC中的CountDownLatch、CyclicBarrier以及Semaphore的学习,予以记录!i < N;* }* }//模拟班长最后锁门问题//模拟5位同学分别进行离开教室的操作i < 6;i++) {System.out.println(Thread.currentThread().getName()+" 同学走了");//最后输出班长锁门的情况。

qq_43357394的博客 421

【Python机器学习】主成分分析(PCA):高维数据的“瘦身术“

主成分分析(PCA)的历史可以追溯到1901年,当时Karl Pearson首次提出了这个概念。在计算机还不存在的年代,PCA是一个纯粹的数学构想,用来简化多变量数据的分析。几十年后,当计算机出现时,PCA成为了数据分析中最早实现的算法之一。从那时起,超过一个世纪的发展中,PCA一直保持着其核心的理论框架和实用价值,这本身就是对这个方法深刻科学性的证明。在现代深度学习时代,许多人可能会认为PCA已经过时了。但事实恰好相反。

nmdbbzcl的博客 981

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

JUC多线程:CountDownLatch、CyclicBarrier、Semaphore同步器原理总结

1、CountDownLatch,闭锁,主要作用是使一个或一组线程在其他线程执行完毕之前,一直处于等待状态,直到其他线程执行完成后再继续执行。 2、CyclicBarrier,循环栅栏,通过 CyclicBarrier 可以实现一组线程之间的相互等待,当所有线程都到达屏障点之后再执行后续的操作。 3、Semaphore 信号量,主要用于控制并发访问共享资源的线程数量,依赖 AQS 的变量 state 作为许可证 permit,通过控制许可证的数量,来保证线程之间的配合。

张维鹏的博客 1万+

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

JUC学习笔记及拓展

JUC学习笔记及拓展Java JUC1 Java JUC简介2 volatile 关键字-内存可见性2.1 内存可见性2.2 volatile 关键字3 原子变量与CAS算法3.1 原子变量3.1.1 i++的原子性问题3.1.2 原子变量3.2 CAS算法3.2.1 ABA问题3.2.2 CAS在JAVA中底层的实现3.3 原子性与可见性区别4 ConcurrentHashMap4.1 锁粒度4.2 锁分段4.3 其它4.4 写入并复制5 CountDownLatch(闭锁)5.1 概念5.2 方法介绍6

yZzc_XQ的博客 3924

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

JUC包下CountDownLatch学习笔记

JUC包下CountDownLatch学习笔记   CountDownLatch的作用是能使用多个线程进来之后,且线程任务执行完毕之后,才执行,   闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态。通俗的讲就是,一个闭锁相当于一扇大门,在大门打开之前所有线程都被阻断,一旦大门打开所有线程都将通过,但是一旦大门打开,所...

maxchenBug的博客 391

CountDownLatch详细分析,如何理解CountDownLatch,java并发编程中使用CountDownLatch,什么是JUC包中的CountDownLatch

juc、java、并发编程

zhangfuping123456789的博客 1万+

JUC工具类:CountDownLatch详解

从源码可知,其底层是由AQS提供支持,所以其数据结构可以参考AQS的数据结构,而AQS的数据结构核心就是两个虚拟队列: 同步队列sync queue 和条件队列condition queue,不同的条件会有不同的条件队列。CountDownLatch典型的用法是将一个程序分为n个互相独立的可解决任务,并创建值为n的CountDownLatch。当每一个任务完成时,都会在这个锁存器上调用countDown,等待问题被解决的任务调用这个锁存器的await,将他们自己拦住,直至锁存器计数结束。

珍惜当下 1128

JUC工具类-CountDownLatch

是一个用于线程同步的工具类,允许一个或多个线程等待其他线程完成操作。它包含一个计数器,初始值通过构造函数设定。当其他线程调用方法后,计数器减一;当计数器值为 0 时,所有调用await()被阻塞的线程被唤醒。CountDownLatch 虽然看似简单,但其背后的原理体现了 Java 并发编程的设计精髓。通过本篇内容,相信你不仅掌握了它的使用方式,更理解了它为何如此设计、如何保证线程安全、以及如何灵活应用。

左灯右行的爱情 942

聊聊 JUC 下的 CountDownLatch、CyclicBarrier、Phaser 和 Semaphore

聊聊 JUC 下的 CountDownLatch、CyclicBarrier、Phaser 和 Semaphore

lybugproducer的博客 1131

【源码】JUC —— CountDownLatch 浅析

【源码】JUC —— CountDownLatch前言SynctryAcquireSharedtryReleaseShared构造方法实现(委托)demo总结 前言 CountDownLatch ,主要用于如下场景:待线程阻塞到一定数量后,统一唤醒。有点跟 Semaphore 反着来的意思 先养剧,再看剧 Sync 基于 AQS 实现,内部类 Sync 继承 AQS 实现相关方法 Sync(int count) { setState(count); }

小水牛的博客 393

java juc Semaphore CountDownLatch CyclicBarrier

在集群环境下,Java中的JUC(Java Util Concurrency)工具类仍然能够用于控制并发。JUC提供了一系列的工具和框架,用于处理并发编程中的各种问题,包括同步、线程池、原子操作、并发集合等。LockSemaphore请注意,虽然JUC工具类可以在一定程度上帮助你处理并发问题,但在分布式环境中,涉及到更多的问题,例如分布式锁、一致性、事务性等,可能需要使用分布式系统的解决方案,例如ZooKeeper、Redisson等。

tanwu1的博客 515

【JUC】CountDownLatch源码分析

1.什么是CountDownLatch 之前写了一篇关于JUC包里关于并发编程工具类的笔记,但过后总感觉不太舒服,不通透,所以,这次就想着先把整个CountDownLatch源码撸一遍。 至于CountDownLatch是什么,撸代码前,可先看:【JUC】CountDownLatch/CyclicBarrier/Semaphore 2.开撸 2.1 类图 先从类图开始,一步一步看。 idea找到...

aaa_bbb_ccc_123_456的博客 2503

JUC并发工具-CountDownLatch、Semaphore、CyclicBarrier

本文分析了Java并发编程中的三个核心同步工具类:CountDownLatch、Semaphore和CyclicBarrier的实现原理。CountDownLatch基于AQS共享锁实现,通过countDown()递减计数器,await()阻塞直到计数器归零;Semaphore通过AQS管理有限资源,acquire()获取资源,release()释放资源;CyclicBarrier使用ReentrantLock和Condition实现可复用的线程屏障,await()会阻塞直到所有线程到达屏障点。三者都通过C

qq_74092815的博客 1121

JUC之CountDownLatch

CountDownLatch----倒计数 countDownLatch这个类使一个线程等待其他线程各自执行完毕后再执行。 是通过一个计数器来实现的,计数器的初始值是线程的数量。每当一个线程执行完毕后,计数器的值就-1,当计数器的值为0时,表示所有线程都执行完毕,然后在闭锁上等待的线程就可以恢复工作了。 package Lock; import java.util.concurrent.C...

ABU_MOlI_ITUI的博客 5223

JUC —— CountDownLatch 的使用

文章目录什么是 CountDownLatch?示例CountDownLatch 的构造函数静态内部类 SyncCountDownLatch 常用的方法await() 方法acquireSharedInterruptibly(int arg)tryAcquireShared(int arg)tryAcquireShared(int acquires)doAcquireSharedInterrupti...

Woo_home的博客 4971

JUC详细介绍涵盖AQS、ReentrantLock、ReentrantReadWriteLock、CountDownLatch等各种常用锁分析

JUC介绍

s.lm的博客 530
上一篇: JUC学习笔记(6)—创建线程的新方法Callable接口
下一篇: JUC学习笔记(8)—CyclicBarrier
木木那年
博客等级 码龄12年 37粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值