Skip to content

Commit 2975712

Browse files
committed
домашняя работа
1 parent 0de6b41 commit 2975712

5 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
package ru.geekbrains.java2.lesson_05;
22

3+
import java.util.Arrays;
4+
35
public class Homework {
6+
static final int size = 10000000;
7+
8+
public static void main(String[] args) {
9+
long startTime;
10+
11+
float[] arr = new float[size];
12+
Arrays.fill(arr, 1f);
13+
14+
startTime = System.currentTimeMillis();
15+
MonothreadedCalculate.calc(arr.clone());
16+
System.out.println("Вычисление в однопоточном методе заняло " +
17+
(float) (System.currentTimeMillis() - startTime) / 1000 + " сек.");
18+
19+
startTime = System.currentTimeMillis();
20+
TwothreadedCalculate.calc(arr.clone());
21+
System.out.println("Вычисление в двухпоточном методе заняло " +
22+
(float) (System.currentTimeMillis() - startTime) / 1000 + " сек.");
23+
24+
startTime = System.currentTimeMillis();
25+
MultithreadedCalculate.calc(arr.clone());
26+
System.out.println("Вычисление в многопоточном методе заняло " +
27+
(float) (System.currentTimeMillis() - startTime) / 1000 + " сек.");
28+
}
429
}
30+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ru.geekbrains.java2.lesson_05;
2+
3+
public class MonothreadedCalculate {
4+
public static void calc(float arr[]) {
5+
for (int i = 0; i < arr.length; i++) {
6+
arr[i] = (float) (arr[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
7+
}
8+
}
9+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ru.geekbrains.java2.lesson_05;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
6+
public class MultithreadedCalculate {
7+
public static void calc(float arr[]) {
8+
int partSize = 100000; // РЕГУЛИРОВАТЬ КОЛ-ВО ПОТОКОВ МОЖНО ЭТОЙ ВЕЛИЧИНОЙ
9+
ArrayList<float[]> arrayList = new ArrayList<>();
10+
11+
int currentLocatin = 0;
12+
int nextLocation = partSize;
13+
14+
while (currentLocatin < arr.length) {
15+
if (nextLocation < arr.length)
16+
arrayList.add(Arrays.copyOfRange(arr, currentLocatin, nextLocation));
17+
else
18+
arrayList.add(Arrays.copyOfRange(arr, currentLocatin, arr.length));
19+
currentLocatin = nextLocation;
20+
nextLocation += partSize;
21+
}
22+
23+
ArrayList<Thread> threadList = new ArrayList<>();
24+
for (float[] a : arrayList) {
25+
Thread tmp = new Thread(new ThreadOfCalculation(a));
26+
threadList.add(tmp);
27+
tmp.start();
28+
}
29+
30+
for (Thread h : threadList) {
31+
try {
32+
h.join();
33+
} catch (InterruptedException e) {
34+
e.printStackTrace();
35+
}
36+
}
37+
38+
currentLocatin = 0;
39+
for (float[] a : arrayList) {
40+
System.arraycopy(a, 0, arr, currentLocatin, a.length);
41+
// раскоментить цикл ниже для вывода границ склейки массивов
42+
/*for (int i = currentLocatin; i < currentLocatin + partSize; i++) {
43+
System.out.println("arr[" + i + "]: " + arr[i]);
44+
if (i > currentLocatin + 2 && i < currentLocatin + partSize - 3) i = currentLocatin + partSize - 3;
45+
}*/
46+
currentLocatin += partSize;
47+
}
48+
}
49+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.geekbrains.java2.lesson_05;
2+
3+
public class ThreadOfCalculation implements Runnable {
4+
private float arr[];
5+
6+
public ThreadOfCalculation(float[] arr) {
7+
this.arr = arr;
8+
}
9+
10+
@Override
11+
public void run() {
12+
for (int i = 0; i < arr.length; i++) {
13+
arr[i] = (float) (arr[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
14+
}
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package ru.geekbrains.java2.lesson_05;
2+
3+
import java.util.Arrays;
4+
5+
public class TwothreadedCalculate {
6+
public static void calc(float arr[]){
7+
int half = arr.length / 2;
8+
float[] arr1 = Arrays.copyOfRange(arr, 0, half);
9+
float[] arr2 = Arrays.copyOfRange(arr, half, arr.length);
10+
Thread th1 = new Thread(new ThreadOfCalculation(arr1));
11+
Thread th2 = new Thread(new ThreadOfCalculation(arr2));
12+
th1.start();
13+
th2.start();
14+
try {
15+
th1.join();
16+
th2.join();
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
21+
System.arraycopy(arr1, 0, arr, 0, half);
22+
System.arraycopy(arr2, 0, arr, half, half);
23+
}
24+
}

0 commit comments

Comments
 (0)