Advertisement
project_number_03

WithoutThreads

Jan 28th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Date d1 = new Date();
  7.         CounterClass c1 = new CounterClass(10000000, 40000000);
  8.         while (true){
  9.             if(!c1.getTh().isAlive()){
  10.                 System.out.println("Длина списка: " + c1.getList().size());
  11.                 Date d2 = new Date();
  12.                 long time = d2.getTime() - d1.getTime();
  13.                 System.out.println("Время выполнения: " + time + " мс"); //у меня ~700 мс
  14.                 break;
  15.             }
  16.         }
  17.     }
  18. }
  19.  
  20. public class CounterClass implements Runnable {
  21.     private int beginIndex;
  22.     private int endIndex;
  23.     private ArrayList<Integer> list = new ArrayList<>();
  24.     private Thread th;
  25.  
  26.     public CounterClass(int beginIndex, int endIndex) {
  27.         this.beginIndex = beginIndex;
  28.         this.endIndex = endIndex;
  29.         th = new Thread(this);
  30.         th.start();
  31.     }
  32.  
  33.     @Override
  34.     public void run() {
  35.         for(int i = beginIndex; i <= endIndex; i++){
  36.             if(i % 11 == 0 || i % 13 == 0 || i % 17 == 0) list.add(i);
  37.         }
  38.     }
  39.  
  40.     public Thread getTh() {
  41.         return th;
  42.     }
  43.  
  44.     public ArrayList<Integer> getList() {
  45.         return list;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement