-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart4.java
More file actions
96 lines (89 loc) · 2.63 KB
/
Part4.java
File metadata and controls
96 lines (89 loc) · 2.63 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
package lab_9_pack;
import java.util.ArrayList;
import java.util.Random;
//The percentage of speed multithreading has over single threading on this system is 27%
//10.197.1.8:8080
//contains the matrix and sum of random numbers in matrix
class MatrixHolder{
public int[][] matrix=new int[650][1000000];
public int sum;
public MatrixHolder(){
Random rand=new Random();
//initialize matrix with random numbers
for(int x=0;x<matrix.length;++x){
for(int y=0;y<matrix[x].length;++y){
int randomNum=rand.nextInt(200);
matrix[x][y]=randomNum;
}
}
}
public void resetSum(){
sum=0;
}
}
//thread that compute sum for entire matrix
class SingleThread extends Thread {
public SingleThread(){}
public void run(MatrixHolder matrixHolder){
for(int x=0;x<matrixHolder.matrix.length;++x){
for(int y=0;y<matrixHolder.matrix[x].length;++y){
matrixHolder.sum+=(int)Math.log(matrixHolder.matrix[x][y]);
}
}
}
}
//thread that computes sum for a row of a matrix
class MultiThread extends Thread {
private MatrixHolder m;
private int r;
private int thisSum;
public MultiThread(MatrixHolder matrixHolder,int row){
m=matrixHolder;
r=row;
}
public void logArray(){
for(int y=0;y<m.matrix[r].length;++y){
thisSum+=(int)Math.log(m.matrix[r][y]);
}
}
public void run(){
logArray();
}
public int getSum(){
return thisSum;
}
}
public class Part4 {
public static void main(String[] args) {
MatrixHolder matrixHolder=new MatrixHolder();
ArrayList<MultiThread> array=new ArrayList<MultiThread>();
try{
long startTime=System.nanoTime();
SingleThread singleThread=new SingleThread();
singleThread.run(matrixHolder);
singleThread.join();
long endTime=System.nanoTime();
System.out.println("Single thread sum: "+matrixHolder.sum);
System.out.println("Computation took "+((endTime-startTime)/1000000)+" milliseconds");
System.out.println("");
matrixHolder.resetSum();
startTime=System.nanoTime();
//create same number of threads as number of rows in matrix
for(int i=0;i<matrixHolder.matrix.length;++i){
array.add(new MultiThread(matrixHolder,i));
array.get(i).start();
}
//start each thread at same time
for(int i=0;i<matrixHolder.matrix.length;++i){
array.get(i).join();
matrixHolder.sum+=array.get(i).getSum();
}
endTime=System.nanoTime();
System.out.println("Multi thread sum: "+matrixHolder.sum);
System.out.println("Computation took "+((endTime-startTime)/1000000)+" milliseconds");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}