长沙网站制作案例wordpress整站备份
web/
2025/10/9 1:27:05/
文章来源:
长沙网站制作案例,wordpress整站备份,网页界面设计的尺寸,微信h5制作Java中的某些并发实用程序自然会比其他并发实用程序受到更多关注#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.mzph.cn/web/89369.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!