Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level17.lesson02.task02;
- import java.util.ArrayList;
- import java.util.List;
- /* Вместе быстрее? Ща проверим :)
- 1. Разберись, что и как работает
- 2. Создай public static нить SortThread,
- которая в методе run отсортирует статический массив
- testArray используя метод sort
- */
- public class Solution {
- public static int countThreads = 10;//static field (size of )
- public static int[] testArray = new int[1000];//static массив, который нужно посортировать
- static {
- for (int i = 0; i < Solution.testArray.length; i++) {
- testArray[i] = i;
- } //раскладыват int от 0 до 999 в масиве testArray
- }
- public static void main(String[] args) throws InterruptedException {
- initThreads();
- /*
- * 1. Создает массив на <Thread> размером countThreads (static field)
- * 2.1 Добавляет в массив 10 new SortThread()
- * 2.2 Стартует все new SortThread() по очереди
- * 2.3 Вызывает join() для каждого элемента по очереди
- * */
- }
- public static void initThreads() throws InterruptedException {
- List<Thread> threads = new ArrayList<Thread>(countThreads);
- for (int i = 0; i < countThreads; i++) threads.add(new SortThread());// добавляет new SortThread в List
- for (Thread thread : threads) thread.start();//стартует метод run (сортирует массив на 1000 занчений int)
- for (Thread thread : threads) thread.join();//ждет завержение каждого элемента
- }
- /*
- * в методе run (class SortThread extends Thread) отсортирует статический массив
- testArray используя метод sort
- * */
- public static void sort(int[] array) {
- for (int i = 0; i < array.length - 1; i++) {
- for (int j = i + 1; j < array.length; j++) {
- if (array[i] < array[j]) {
- int k = array[i];
- array[i] = array[j];
- array[j] = k;
- }
- }
- }
- }
- public static class SortThread extends Thread {
- @Override
- public void run()
- {
- sort(testArray);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement