-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
143 lines (107 loc) · 4.23 KB
/
Solution.java
File metadata and controls
143 lines (107 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//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<Thread> threads = new ArrayList<Thread>(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<Thread> threads = new ArrayList<Thread>(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;
}
}
}
}
}
*/