Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package com.epam.hushchyn.preprod.task6.controller;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. import com.epam.hushchyn.preprod.task6.MyThread;
  10. // import com.epam.hushchyn.preprod.task6.MyThread2;
  11.  
  12. public class UI {
  13.     private List<Long> list = Collections.synchronizedList(new ArrayList<>());
  14.  
  15.     private List<List<Long>> localeList = new ArrayList<>();
  16.  
  17.     private long startNumber;
  18.  
  19.     private long finishNumber;
  20.  
  21.     private long s;
  22.  
  23.     private long f;
  24.  
  25.     private int threadCount;
  26.  
  27.     public UI() {
  28.     }
  29.  
  30.     public void start() {
  31.         menu();
  32.  
  33.         System.out.println("Start");
  34.         long before = System.currentTimeMillis();
  35.         initThread(startNumber, finishNumber, threadCount);
  36.  
  37.         while (Thread.activeCount() > 1) {
  38.         }
  39.         Collections.sort(list, new Comparator<Long>() {
  40.             public int compare(Long o1, Long o2) {
  41.                 return o1.compareTo(o2);
  42.             }
  43.         });
  44.         System.out.println(list);
  45.         System.out.println(System.currentTimeMillis() - before);
  46.  
  47.         before = System.currentTimeMillis();
  48.         initThread(startNumber, finishNumber, threadCount, localeList);
  49.         while (Thread.activeCount() > 1) {
  50.         }
  51.         for (List<Long> long1 : localeList) {
  52.             list.addAll(long1);
  53.         }
  54.         System.out.println(System.currentTimeMillis() - before);
  55.         System.out.println(list.size());
  56.     }
  57.  
  58.     private void initThread(long start, long finish, int threads) {
  59.         list.clear();
  60.         s = 0;
  61.         f = 0;
  62.         for (int i = 0; i < threads; i++) {
  63.             inter(start, finish, threads);
  64.             new Thread(new MyThread(s, f, list)).start();
  65.         }
  66.     }
  67.  
  68.     private void initThread(long start, long finish, int threads,
  69.             List<List<Long>> localeList) {
  70.         list.clear();
  71.         s = 0;
  72.         f = 0;
  73.         for (int i = 0; i < threads; i++) {
  74.             inter(start, finish, threads);
  75.             localeList.add(new ArrayList<Long>());
  76.             new Thread(new MyThread(s, f, localeList.get(i))).start();
  77.         }
  78.  
  79.     }
  80.  
  81.     private void inter(long start, long finish, int threads) {
  82.         long delta = (finish - start) / threads;
  83.         s = start + (Thread.activeCount() - 1) * delta;
  84.         f = s + delta;
  85.         if (finish - f < delta) {
  86.             f = finish;
  87.         }
  88.     }
  89.  
  90.     private void menu() {
  91.         try (Scanner sc = new Scanner(System.in)) {
  92.             System.out.println("Enter start number:");
  93.             startNumber = Long.parseLong(sc.nextLine());
  94.             System.out.println("Enter finish number:");
  95.             finishNumber = Long.parseLong(sc.nextLine());
  96.             System.out.println("Enter threads count:");
  97.             threadCount = Integer.parseInt(sc.nextLine());
  98.         }
  99.  
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement