AnswerMain.java 882 B

123456789101112131415161718192021222324252627282930313233
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.concurrent.atomic.AtomicLong;
  4. public class Main {
  5. public static final int BIG_NR = 2000000;
  6. public static AtomicLong bigSum = new AtomicLong();
  7. public static void main(String[] args) {
  8. List<Thread> threads = new ArrayList<Thread>();
  9. for (int i = 0; i < 50; i++) {
  10. Runnable task = new Sum(BIG_NR);
  11. Thread worker = new Thread(task);
  12. worker.start();
  13. threads.add(worker);
  14. }
  15. int running = 0;
  16. do {
  17. running = 0;
  18. for (Thread thread : threads) {
  19. if (thread.isAlive()) {
  20. running++;
  21. }
  22. }
  23. System.out.println("Remaining threads: " + running);
  24. } while (running > 0);
  25. System.out.println(Main.bigSum);
  26. }
  27. }