Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.epam.hushchyn.preprod.task6.controller;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Scanner;
- import com.epam.hushchyn.preprod.task6.MyThread;
- // import com.epam.hushchyn.preprod.task6.MyThread2;
- public class UI {
- private List<Long> list = Collections.synchronizedList(new ArrayList<>());
- private List<List<Long>> localeList = new ArrayList<>();
- private long startNumber;
- private long finishNumber;
- private long s;
- private long f;
- private int threadCount;
- public UI() {
- }
- public void start() {
- menu();
- System.out.println("Start");
- long before = System.currentTimeMillis();
- initThread(startNumber, finishNumber, threadCount);
- while (Thread.activeCount() > 1) {
- }
- Collections.sort(list, new Comparator<Long>() {
- public int compare(Long o1, Long o2) {
- return o1.compareTo(o2);
- }
- });
- System.out.println(list);
- System.out.println(System.currentTimeMillis() - before);
- before = System.currentTimeMillis();
- initThread(startNumber, finishNumber, threadCount, localeList);
- while (Thread.activeCount() > 1) {
- }
- for (List<Long> long1 : localeList) {
- list.addAll(long1);
- }
- System.out.println(System.currentTimeMillis() - before);
- System.out.println(list.size());
- }
- private void initThread(long start, long finish, int threads) {
- list.clear();
- s = 0;
- f = 0;
- for (int i = 0; i < threads; i++) {
- inter(start, finish, threads);
- new Thread(new MyThread(s, f, list)).start();
- }
- }
- private void initThread(long start, long finish, int threads,
- List<List<Long>> localeList) {
- list.clear();
- s = 0;
- f = 0;
- for (int i = 0; i < threads; i++) {
- inter(start, finish, threads);
- localeList.add(new ArrayList<Long>());
- new Thread(new MyThread(s, f, localeList.get(i))).start();
- }
- }
- private void inter(long start, long finish, int threads) {
- long delta = (finish - start) / threads;
- s = start + (Thread.activeCount() - 1) * delta;
- f = s + delta;
- if (finish - f < delta) {
- f = finish;
- }
- }
- private void menu() {
- try (Scanner sc = new Scanner(System.in)) {
- System.out.println("Enter start number:");
- startNumber = Long.parseLong(sc.nextLine());
- System.out.println("Enter finish number:");
- finishNumber = Long.parseLong(sc.nextLine());
- System.out.println("Enter threads count:");
- threadCount = Integer.parseInt(sc.nextLine());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement