Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework 5 first commit #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/main/java/ArrayManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public class ArrayManager {

private static final int size = 10000000;
private static final int h = size / 2;
private float[] arr = new float[size];

private void addOneForAllValues(){
for (int i=0;i<arr.length;i++){
arr[i] = 1;
}
}

public void fillArray(){
addOneForAllValues();
long start = System.currentTimeMillis();
for (int i=0;i<arr.length;i++){
arr[i] = (float)(arr[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
}
System.out.println(System.currentTimeMillis() - start);
}

public void synchFillArray(){
float[] a1 = new float[h];
float[] a2 = new float[h];
addOneForAllValues();
long start = System.currentTimeMillis();
System.arraycopy(arr,0,a1,0,h);
System.arraycopy(arr,h,a2,0,h);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<a1.length;i++){
a1[i] = (float)(arr[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
}
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<a1.length;i++){
a2[i] = (float)(arr[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
}
}
});

thread.start();
thread2.start();

System.arraycopy(a1, 0, arr, 0, h);
System.arraycopy(a2, 0, arr, h, h);

System.out.println("synchronyzed method finish");
System.out.println(System.currentTimeMillis() - start);
}
}
5 changes: 4 additions & 1 deletion src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
public class Main {

public static void main(String[] args){
System.out.println("test");

ArrayManager arrayManager = new ArrayManager();
arrayManager.fillArray();
arrayManager.synchFillArray();
}
}