简体   繁体   English

如何在完成4个独立线程后安排打印语句?

[英]How do I schedule a print statement upon completion of 4 independent threads?

I have a simple java application which calculates prime numbers up to a certain user-given number and prints out the numbers. 我有一个简单的Java应用程序,该应用程序可以计算最多一个由用户提供的数字的质数,并打印出这些数字。 I've built upon this to include four separate threads which iterate through 4 separate ranges of numbers. 我以此为基础,包括四个单独的线程,这些线程遍历4个单独的数字范围。 Once all 4 threads have completed their iterations I want to print the final ArrayList. 一旦所有4个线程都完成了迭代,我想打印最终的ArrayList。

I'm currently getting a ConcurrentModificationException because after I start the 4 threads, the next line of code is the print statement which is trying to print the ArrayList which is at that moment being modified by at least one of the still active threads. 我当前正在收到ConcurrentModificationException,因为在启动4个线程之后,下一行代码是print语句,该语句试图打印ArrayList,当时该数组正被至少一个仍处于活动状态的线程修改。

Therefore, I want to be able to have the print statement execute after all 4 threads have died. 因此,我希望能够在所有4个线程都死亡之后执行print语句。 Furthermore, I would like to do this without using a spinning loop. 此外,我希望不使用旋转循环来执行此操作。 I have nothing in particular against using a spinning loop except that I imagine there is a better way to do this and I would probably have to assign greater priorities to the 4 threads in order to prevent the spinning loop from using up a significant amount of the CPU. 我没有特别反对使用旋转循环,除了我想有一种更好的方法可以这样做,而且我可能必须为4个线程分配更大的优先级,以防止旋转循环消耗大量的线程。中央处理器。

Use a CountDownLatch initialized to 4; 使用一个CountDownLatch初始化为4; the print thread awaits the latch, and the worker threads call countdown on the latch before they terminate. 打印线程awaits锁存器,并且工作线程在终止之前调用锁存器上的countdown

Be sure to properly synchronize your ArrayList as well if four threads are modifying it at once; 如果有四个线程一次修改ArrayList ,请确保正确同步它。 you may want to use a ConcurrentLinkedQueue instead (or else use a different ArrayList for each thread) 您可能要改用ConcurrentLinkedQueue (或者为每个线程使用不同的ArrayList

Use a CountdownLatch . 使用CountdownLatch The Javadoc for that class tells how to have 该类的Javadoc告诉如何

  1. The main thread creates the latch with the number of threads. 主线程使用线程数创建闩锁。
  2. The main thread starts all the working threads. 主线程启动所有工作线程。
    1. Each thread has a reference to the latch. 每个线程都有对闩锁的引用。
    2. It counts the latch down when it finishes its work. 完成工作时,它将倒计时。
  3. The main thread waits for the latch to count down to 0. 主线程等待锁存器计数到0。
  4. The main thread does the printing job. 主线程完成打印工作。

If you use a java.util.concurrent.BlockingQueue , each thread could put() an item on the blocking queue when it is finished. 如果您使用java.util.concurrent.BlockingQueue ,则每个线程可以在完成时put()一个)项目放到阻塞队列中。

Before the print statement, the code could do a take() from the blocking queue and only proceed when the take() has returned once for each thread. 在print语句之前,该代码可以从阻塞队列中执行take() ,并且仅当take()为每个线程返回一次后才继续执行。 The printing thread will block on the take() while there is nothing there for it to take. 打印线程将在take()上阻塞,而没有任何要接收的线程。 That will guarantee that the printing doesn't commence until all the threads have finished. 这样可以确保直到所有线程完成后才开始打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM