Java Thread: CountDownLatch 例子

public class CountDownLatchTest {
 
 @Test
 public void ThreadControllTest() throws InterruptedException{
  // Suppose we have total 10 thread waiting to execute
  final int TOTAL_THREAD_NUM = 10;
  
  // Leverage our system that only allow 5 threads are running in the same time.
  final int COUNT_LATCH = 5; 
  
  // The threads control tool that can control how many threads can runnning at same time.
  CountDownLatch countDownLatch = null;
  
  for( int i=0; i<TOTAL_THREAD_NUM; i++ ){
   Thread.sleep(1000); // give much time gap to see the running results.
   
   // initialize the CounDownLatch Object by COUNT_LATCH gaps.   
   if( i % 5 == 0 || i == 0){
    countDownLatch =  new CountDownLatch(COUNT_LATCH);
   }
   Thread thread = new MockThread(countDownLatch);
   
   thread.start();
   
  }
  
 }
 
}  

class MockThread extends Thread{
 CountDownLatch _latch;
 
 public MockThread(CountDownLatch latch){
  _latch = latch;
 }
 
 @Override
 public void run() {
  // TODO Auto-generated method stub
  try {
   System.out.println("Thread initial, waiting~~~"); 
   // waiting here and reduce 1 for the COUNT_LATCH num. 
   // Until COUNT_LATCH has reduce to 0, continue.
   _latch.countDown();
   _latch.await();
   
   System.out.println("Thread started, starting~~~"); 
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

 

从共现矩阵到共现图一步到位——详解版 1.共现矩阵 判断大量不同文本之间,相邻词的联系,两词在所有文本中相邻的次数在矩阵中用数值表示。(参考此) 例子如下: 1.1 文本 • I like deep learning. • I like NLP. • I enjoy flying. 1.2 共现矩阵 2.共现图 2.1 数据预处理(节点和边) 获取文件node.csv和edge.csv。代码详见github仓库 2.2 共现图的制作 2.2.1 gephi的安装(参考此) (1)先下载gephi (2) 再配置java环境(下载java1.8 阅读详情

相关推荐

DNF服务器搭建服务端架设教程

第三步:打开winscp,上传Script.pvf(版本)、publickey.pem(密钥)、df_game_r(等级补丁)到home/neople/game目录下。第四步:打开putty输入./stop,./stop(两次,如果是首次架设不用./stop),./run,运行刚才上传的三个游戏文件。③是本地配置登录器的配置文件,自动生成,可以删除,但是还是会根据你生成的登录器生成这个文件,类似日志。⑤密钥中所上传的三个文件其中之一,重中之重,上传到服务器的密钥必须和登录器中的密钥为一个。

V13807970340的博客 6257

以考带学操作系统(进程)

进程→前趋图: 1、定义:前趋图是一个有向无循环图,图由节点和节点间的有向边组成,节点代表各程序段的操作,而节点间的有向边表示两程序段操作之间存在的前趋关系("→")。 2、表示方法:两程序段Pi和Pj的前趋关系表示为:Pi→Pj 其中:Pi是Pj的前趋,Pj是Pi的后继,即Pi执行完毕Pj才能由执行。 例子:若有运算式:S=A+B×3÷X+X×9,要求画出此运算过程的前趋图。

Jessica 3368

SpyGlass CDC实战:如何用sgdc约束文件解决跨时钟域报错(附常见错误排查)

本文详细介绍了如何使用SpyGlass CDC工具编写sgdc约束文件解决跨时钟域(CDC)报错问题。从基础语法到实战案例,涵盖时钟声明、复位约束、路径豁免等关键技巧,特别针对SPI断续时钟等特殊场景提供解决方案,并附常见错误排查指南,帮助工程师高效完成CDC验证。

weixin_28744423的博客 625

java 跨线程调用_[译]如何用Java实现跨线程通信

正常来说每一个子线程只需要完成它自己的任务,但有些时候,我们也许需要多个线程一起来完成一件任务,这就需要线程间通信这篇文章涉及到的类和方法有:thread.join(), object.wait(), object.notify(), CountdownLatch, CyclicBarrier, FutureTask, Callable……文章中涉及到的代码在这里我会用一系列的例子来说明如何用在J...

weixin_39760368的博客 409

手机直连卫星上网,3GPP R17/R18到底做了哪些技术升级?(IoT/NR NTN详解)

本文深入解析了3GPP R17/R18标准中手机直连卫星上网的技术升级,重点介绍了IoT NTN与NR NTN两项关键技术。R17通过星历辅助补偿算法和多普勒频移控制,实现了智能手机直连卫星的突破;R18则进一步优化移动性、支持高频段和星上本地交换,提升用户体验。这些技术为物联网和5G/6G通信开辟了新可能。

weixin_33701251的博客 617

汇川EASY系列之以太网通讯(MODBUS_TCP做主站)

汇川Easy系列以太网通讯中(MODBUSTCP,plc做主站),终于可以不用使用指令就可以完成了,全程通过简单的配置就可通讯。本文将通过EASY系列PLC与调试助手之间完成此操作。

这是一个长期关注工控行业技术的博客,实现0-1的突破,期待您的关注和留言 3302

java怎么实现线程_浅析Java中如何实现线程之间通信

正常情况下,每个子线程完成各自的任务就可以结束了。不过有的时候,我们希望多个线程协同工作来完成某个任务,这时就涉及到了线程间通信了。本文涉及到的知识点:thread.join(), object.wait(), object.notify(), CountdownLatch, CyclicBarrier, FutureTask, Callable 等。下面我从几个例子作为切入点来讲解下 Java ...

weixin_35661054的博客 117

‌NTRU算法详细教程 | 2025年最新版(基于NIST后量子密码标准更新)

‌:NTRU是当前抗量子加密的黄金标准,‌。

blns_yxl的博客 1803

Java 里如何实现线程间通信

  Java 里如何实现线程间通信   原文出处: wingjay http://www.importnew.com/26850.html 正常情况下,每个子线程完成各自的任务就可以结束了。不过有的时候,我们希望多个线程协同工作来完成某个任务,这时就涉及到了线程间通信了。 本文涉及到的知识点:thread.join(), object.wait(), object.notify(), ...

ustcyy91的博客 323

MediaCodec视频解码流程详解及参考demo

本文主要讲解mediacodec的视频解码流程,及demo参考例子。

分享音视频技术 7874

Quartus (Quartus Prime 18.1)的安装及仿真步骤

前两天做计组的课设需要用到Quartus 这个软件,但安装有点麻烦,在网上搜了很多教程,发现都是旧版本的,最后弄了好半天才把环境搭建好。所以,分享一下它的搭建过程。 因为我不经常用它,所以安装的版本是精简版的其他版本的也应该差不多以下是需要下载的安装包: 准备: Quartus Prime 精简版,最新发布: v18.1 1. QuartusLiteSetup-18.1.0.625-wi...

weixin_41082948的博客 8万+

Java多线程、主线程等待所有子线程执行完毕、共享资源

1.Java创建与启动线程 Java提供两种方式创建和启动线程:1.直接Thread类,2.实现Runable接口。 1.1 继承Thread类 public class myThread extends Thread { public void run(){ for(int i=0;i<5;i++){ System.out.println(this.getN...

weixin_30950887的博客 122

基金估值核算系统三国杀:恒生 VS 赢时胜 VS 融选!

本文对比分析了恒生电子、赢时胜和融选科技三家金融科技公司的基金估值系统。恒生电子作为行业龙头,在公募基金领域占据较大份额,其新一代估值系统6.0采用微服务架构,自动化程度高;赢时胜在中型公募和券商资管领域优势明显,其V5.5系统在信创适配和复杂产品处理方面表现突出;融选科技作为新兴厂商,提供轻量化解决方案,适合中小型机构。文章从市场格局、系统定位、技术架构、成本构成和用户反馈五个维度进行深入比较,为金融机构选型提供参考。当前估值系统市场呈现"三国鼎立"格局,各具特色,选型需结合机构规模、

magbox520的博客 826

java多线程对CountDownLatch的使用实例

介绍 CountDownLatch是一个同步辅助类,它允许一个或多个线程一直等待直到其他线程执行完毕才开始执行。 用给定的计数初始化CountDownLatch,其含义是要被等待执行完的线程个数。 每次调用CountDown(),计数减1 主程序执行到await()函数会阻塞等待线程的执行,直到计数为0 实现原理 计数器通过使用锁(共享锁、排它锁)实现 实例1 场景:模拟10人赛跑...

weixin_30511107的博客 79

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

并发编程之:Thread.join()和CountDownLatch

大家好,我是小黑,一个在互联网苟且偷生的农民工。 先问大家一个问题,在主线程中创建多个线程,在这多个线程被启动之后,主线程需要等子线程执行完之后才能接着执行自己的代码,应该怎么实现呢? Thread.join() 看过我 并发编程之:线程 的朋友应该知道怎么做,在Thread类中有一个方法join(),这个方法是一个阻塞方法,当前线程会等待调动join()方法的线程死亡之后再继续执行。 我们通过代码来看看执行结果。 public class JoinDemo { public sta

syh121的博客 353

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

java — Thread之CountDownLatch的使用

CountDownLatch的简单使用 CountDownLatch的简单介绍: CountDownLatch类是一个同步计数器,构造时传入int参数,该参数就是计数器的初始值,每调用一次countDown()方法,计数器减1,计数器大于0 时,await()方法会阻塞程序继续执行 CountDownLatch是一个倒计数的锁存器,当计数减至0时触发特定的事件。 CountDownLatch被用来同步一个或多个任务,强制它们等待由其他任务执行的一组操作完成。你可以向CountDownLatch对象设置一个

weixin_53341657的博客 494

深度学习工具包 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")

Java并发工具类CountDownLatch源码中的例子

Java并发工具类CountDownLatch源码中的例子 实例一 原文描述 /** * <p><b>Sample usage:</b> Here is a pair of classes in which a group * of worker threads use two countdown latches: * <ul> * <...

weixin_30828379的博客 123

CountDownLatch例子

public class CountDownLatchTest { public static void main(String[] args) throws InterruptedException { final CountDownLatch countDownLatch = new CountDownLatch(10); new Thread(new...

jingzhi_f 180

java的CountDownLatch使用

基本使用 import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; public class ListAdd2 { private volatile static List list = new ArrayList(); public void add(){ list.add("cxf"); } public int size(){ return l

秃了也弱了 6517

java thread signal_java — Thread之CountDownLatch的使用

CountDownLatch的简单使用CountDownLatch的简单介绍:CountDownLatch类是一个同步计数器,构造时传入int参数,该参数就是计数器的初始值,每调用一次countDown()方法,计数器减1,计数器大于0 时,await()方法会阻塞程序继续执行CountDownLatch是一个倒计数的锁存器,当计数减至0时触发特定的事件。CountDownLatch被用来同步一个...

weixin_34083807的博客 175

[Java] CountDownLatch 模拟高并发

参考: Class CountDownLatch Coordinating threads with CountDownLatch一、简介CountDownLatch 是 java.util.concurrent 包下的一个同步辅助类,它能使一个或多个线程在其他的线程的一系列操作完成之前一直等待,初始化值为计数器大小(即线程数量)。二、使用场景 同时启动多个线程; 多个线程操作完成之前一直等待。

列苗 6893

java多线程Thread-线程调度类CountDownLatch计数器

背景 说起Java,不得不说到他在线程上的优越性,由于封装好了,对于使用者是大大的提升了可读性和理解性。正好我也想记录下关于多线程的基本用法。相关知识 说到多线程,讲讲它的含义,首先,进程就是正在运行的程序,而一般程序是按照一定顺序执行的,这个执行就称为执行路径/控制单元/线程。 有多种叫法,我只说明下。然而一个CPU一个时间段内只能执行一个进程,所以我们开启大量的程序同时使用的时候,就会出现争夺

饶为先生的博客 1307

java多线程中的 CountDownLatch、CyclicBarrier和Thread.join()

java多线程中的 CountDownLatch、CyclicBarrier和Thread.join()

weixin_45215156的博客 457

countDownLatch.await()与thread.join()区别

在编写多线程的工作中,有个常见的问题:主线程(main) 启动好几个子线程(task)来完成并发任务,主线程要等待所有的子线程完成之后才继续执行main的其它任务。 默认主线程退出时其它子线程不会停,如果想让main退出时其它子线程终止,可以用subThread.setDaemon(true) 设置子线程为“守护线程”。 如果要在主线程等待所有子线程完成后,还要执行其它操作(比如:结果合并)....

u012501054的博客 1万+

什么是 Java 的 CountDownLatch?

类名作用:让一组线程等待,直到指定数量的事件或任务完成(计数器归零)。核心特性初始化时指定计数器值。线程通过减少计数器,await()等待计数器为零。

qq_63553317的博客 734

java多线程Thread join与CountDownLatch源码分析

在这个部分总结了2019年到目前为止Java常见面试问题,取其面试核心编写成这份文档笔记,从中分析面试官的心理,摸清面试官的“套路”,可以说搞定90%以上的Java中高级面试没一点难度。本节总结的内容涵盖了:消息队列、Redis缓存、分库分表、读写分离、设计高并发系统、分布式系统、高可用系统、SpringCloud微服务架构等一系列互联网主流高级技术的知识点。(上述只是一个整体目录大纲,每个点里面都有如下所示的详细内容,从面试问题——分析面试官心理——剖析面试题——完美解答的一个过程)

2401_85377226的博客 1128
上一篇: 数据库事务隔离级别和锁实现机制
下一篇: Sysdeo Eclipse Tomcat Launcher plugin 使用总结
伤神v
博客等级 码龄19年 17粉丝 129原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值