当前位置: 首页 > news >正文

桃园市网站建设_网站建设公司_跨域_seo优化

网站域名查询注册,新云手机站官网,专业网站建设加盟合作,收录查询apiJava中的某些并发实用程序自然会比其他并发实用程序受到更多关注#xff0c;因为它们可以解决通用问题而不是更具体的问题。 我们大多数人经常遇到执行程序服务和并发集合之类的事情。 其他实用程序不太常见#xff0c;因此有时它们可​​能会使我们逃脱#xff0c;但是请记… Java中的某些并发实用程序自然会比其他并发实用程序受到更多关注因为它们可以解决通用问题而不是更具体的问题。 我们大多数人经常遇到执行程序服务和并发集合之类的事情。 其他实用程序不太常见因此有时它们可​​能会使我们逃脱但是请记住它们是很好的。 CountDownLatch是这些工具之一。 CountDownLatch –更通用的等待/通知机制 各种Java开发人员都应该熟悉等待/通知方法直到达到条件为止。 以下是有关其工作原理的一些示例 public void testWaitNotify() throws Exception {final Object mutex new Object();Thread t new Thread() {public void run() {// we must acquire the lock before waiting to be notifiedsynchronized(mutex) {System.out.println(Going to wait (lock held by Thread.currentThread().getName() ));try {mutex.wait(); // this will release the lock to be notified (optional timeout can be supplied)} catch (InterruptedException e) {e.printStackTrace();} System.out.println(Done waiting (lock held by Thread.currentThread().getName() ));}}};t.start(); // start her up and let her wait()// not normally how we do things, but good enough for demonstration purposesThread.sleep(1000);// we acquire the lock released by wait(), and notify()synchronized (mutex) {System.out.println(Going to notify (lock held by Thread.currentThread().getName() ));mutex.notify();System.out.println(Done notify (lock held by Thread.currentThread().getName() ));}} 输出量 Going to wait (lock held by Thread-0) Going to notify (lock held by main) Done notify (lock held by main) Done waiting (lock held by Thread-0) 实际上 CountDownLatch可以类似于等待/通知仅使用一个通知即可使用-也就是说只要您不希望在获取锁并调用wait之前调用notify时 wait就会停顿。 。 因此它实际上是更宽容的在某些情况下这正是您想要的。 这是一个示例 public void testWaitNotify() throws Exception {final CountDownLatch latch new CountDownLatch(1); // just one timeThread t new Thread() {public void run() {// no lock to acquire!System.out.println(Going to count down...);latch.countDown();}};t.start(); // start her up and let her wait()System.out.println(Going to await...);latch.await();System.out.println(Done waiting!); } 如您所见它比等待/通知更简单并且所需的代码更少。 它还允许我们在调用wait之前调用最终释放该块的条件。 这可能意味着代码更安全。 真实的例子 因此我们知道我们可以将其用作更简单的等待/通知机制但是您可能已经在上面看到了构造函数参数。 在构造函数中指定解锁之前需要递减锁存器的次数。 有什么可能的用途 好吧它可以使进程等待直到采取了一定数量的动作。 例如如果您具有可以通过侦听器或类似方法挂接到的异步进程则可以创建单元测试以验证是否进行了一定数量的调用。 这使我们只需要在正常情况下需要的时间或在保释并假设失败之前的某个限制即可。 最近我遇到了一种情况我必须验证是否已将JMS消息从队列中拉出并正确处理。 这自然是异步的并且不在我的控制范围之内并且也不选择模拟因为它是具有Spring上下文的完全组装的应用程序等等。为了测试这一点我对使用服务进行了微小的更改以允许在邮件已处理。 然后我可以临时添加一个侦听器该侦听器使用CountDownLatch保持测试尽可能接近同步。 这是显示概念的示例 public void testSomeProcessing() throws Exception {// should be called twicefinal CountDownLatch testLatch new CountDownLatch(2);ExecutorService executor Executors.newFixedThreadPool(1);AsyncProcessor processor new AsyncProcessor(new Observer() {// this observer would be the analogue for a listener in your async processpublic void update(Observable o, Object arg) {System.out.println(Counting down...);testLatch.countDown();}});//submit two tasks to be process// (in my real world example, these were JMS messages)executor.submit(processor);executor.submit(processor);System.out.println(Submitted tasks. Time to wait...);long time System.currentTimeMillis();testLatch.await(5000, TimeUnit.MILLISECONDS); // bail after a reasonable timelong totalTime System.currentTimeMillis() - time;System.out.println(I awaited for totalTime ms. Did latch count down? (testLatch.getCount() 0));executor.shutdown(); }// just a process that takes a random amount of time // (up to 2 seconds) and calls its listener public class AsyncProcessor implements CallableObject {private Observer listener;private AsyncProcessor(Observer listener) {this.listener listener;}public Object call() throws Exception {// some processing here which can take all kinds of time...int sleepTime new Random().nextInt(2000);System.out.println(Sleeping for sleepTime ms);Thread.sleep(sleepTime);listener.update(null, null); // not standard usage, but good for a demoreturn null;} } 输出量 Submitted tasks. Time to wait... Sleeping for 739ms Counting down... Sleeping for 1742ms Counting down... I awaited for 2481ms. Did latch count down? true 结论 CountDownLatch就是这样。 它不是一个复杂的主题而且用途有限但是当您遇到类似我的问题时很高兴看到示例并知道它们在您的工具箱中。 将来如果没有其他问题我一定会牢记这一点以便进行更简单的等待/通知。 如果您对此帖子或系列中的其他帖子有疑问或评论请留言。 参考来自Carfey Software博客的 JCG合作伙伴的Java并发第6部分– CountDownLatch 。 相关文章 Java并发教程–信号量 Java并发教程–重入锁 Java并发教程–线程池 Java并发教程–可调用将来 Java并发教程–阻塞队列 Exchanger和无GC的Java Java Fork / Join进行并行编程 使用迭代器时如何避免ConcurrentModificationException 改善Java应用程序性能的快速技巧 翻译自: https://www.javacodegeeks.com/2011/09/java-concurrency-tutorial.html
http://www.ihoyoo.com/news/95131.html

相关文章:

  • 上海网站建设yes404长春网站建设模板制作
  • 四川网站建设seo网站开发vue版本是什么
  • wordpress网站mip改造汕头制作手机网站
  • wordpress网站迁移问题wordpress主题样式乱
  • 精选网站建设排行榜推广普通话的文章
  • 网站建设责任分解一个空间放两个php网站
  • 上海做网站 公司怎么用手机做网站编辑
  • 网站备案的接入商wordpress支持多少会员
  • 快速建站软件排名哪些是 joomla做的网站
  • 建设农产品网站总结ppt国外网站推广平台有哪些公司
  • asp.net网站开发工程师(c网站导航常用关键字
  • 商城网站设计公司网站备案丢失
  • 猫眼网站建设郑州网站建设冫汉狮网络
  • 网站重新备案网站工商网监标
  • 用照片做视频的模板下载网站好网站建设可以抵扣吗
  • 专业网站制作公司招聘wordpress怎么更改域名
  • 网站域名年费多少钱网站开发平均工资
  • 备案号 不放在网站首页天津网站搜索排名优化
  • 做电商网站就业岗位晋升企业营销系统和网站建设
  • 网站后台构建网站建设 技术规范书
  • 上海网站建设|网站制作wordpress删除版权
  • 成都制作网站公司哪家好自适应网站好建们
  • 山东建设厅执业资格注册中心网站小网站设计
  • 扁平设计网站临沧市住房和城乡建设局门户网站
  • 南昌做网站公司有哪些网络的最基本定义
  • 网站做导航的地图导航昆明网站建设公司猎狐科技怎么样
  • 南宁公司网站建设方案wordpress怎么导入html
  • 网站收款即时到账怎么做的wordpress 免费 主题
  • 阿里云 域名 做网站inews wordpress
  • 网站搭建后台WordPress 调整语言