//I have spoken these things to you in figures of speech. But the time is coming when I will no more speak to you in figures of speech, but will tell you plainly about the Father. (John 16:25)
package com.javarush.task.task17.task1702;
import java.util.ArrayList;
import java.util.List;
/*
ÐмеÑÑе бÑÑÑÑее? Ща пÑовеÑим :)
*/
public class Solution {
public static int countThreads = 10;
public static int[] testArray = new int[1000];
static {
for (int i = 0; i < Solution.testArray.length; i++) {
testArray[i] = i;
}
}
public static void main(String[] args) throws InterruptedException {
StringBuffer expectedResult = new StringBuffer();
for (int i = 1000 - 1; i >= 0; i--) {
expectedResult.append(i).append(" ");
}
initThreads();
StringBuffer result = new StringBuffer();
for (int i = 0; i < testArray.length; i++) {
result.append(testArray[i]).append(" ");
}
System.out.println(result);
System.out.println((result.toString()).equals(expectedResult.toString()));
}
public static void initThreads() throws InterruptedException {
List threads = new ArrayList(countThreads);
for (int i = 0; i < countThreads; i++) threads.add(new SortThread());
for (Thread thread : threads) thread.start();
for (Thread thread : threads) thread.join();
}
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() {
Solution.sort(testArray);
}
}
}
/*
ÐмеÑÑе бÑÑÑÑее? Ща пÑовеÑим :)
1. РазбеÑиÑÑ, ÑÑо и как ÑабоÑаеÑ.
2. Создай public static ниÑÑ SortThread, коÑоÑÐ°Ñ Ð² меÑоде run оÑÑоÑÑиÑÑÐµÑ ÑÑаÑиÑеÑкий маÑÑив testArray иÑполÑзÑÑ Ð¼ÐµÑод sort.
ТÑебованиÑ:
1.âÐлаÑÑ Solution должен ÑодеÑжаÑÑ public static клаÑÑ SortThread.
2.âÐлаÑÑ SortThread должен бÑÑÑ Ð½Ð¸ÑÑÑ.
3.âРмеÑоде run клаÑÑа SortThread должен вÑзÑваÑÑ Ð¼ÐµÑод sort() Ñ Ð¿Ð°ÑамеÑÑом testArray.
4.âÐÑогÑамма должна вÑводиÑÑ ÑекÑÑ Ð½Ð° ÑкÑан.
package com.javarush.task.task17.task1702;
import java.util.ArrayList;
import java.util.List;
*
ÐмеÑÑе бÑÑÑÑее? Ща пÑовеÑим :)
*
public class Solution {
public static int countThreads = 10;
public static int[] testArray = new int[1000];
static {
for (int i = 0; i < Solution.testArray.length; i++) {
testArray[i] = i;
}
}
public static void main(String[] args) throws InterruptedException {
StringBuffer expectedResult = new StringBuffer();
for (int i = 1000 - 1; i >= 0; i--) {
expectedResult.append(i).append(" ");
}
initThreads();
StringBuffer result = new StringBuffer();
for (int i = 0; i < testArray.length; i++) {
result.append(testArray[i]).append(" ");
}
System.out.println(result);
System.out.println((result.toString()).equals(expectedResult.toString()));
}
public static void initThreads() throws InterruptedException {
List threads = new ArrayList(countThreads);
for (int i = 0; i < countThreads; i++) threads.add(new SortThread());
for (Thread thread : threads) thread.start();
for (Thread thread : threads) thread.join();
}
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;
}
}
}
}
}
*/