1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
ch4aika [34]
3 years ago
10

Create a Java program with threads that looks through a vary large array (100,000,000 elements) to find the smallest number in t

hat array. You should track the current lowest value seen in a single, shared value. You should also track the time taken, comparing the amount of time for 1, 2, 4 and more threads. Fairly precise timing can be obtained by using the System.nanoTime() method. For example, it's taking my poor computer over 1 second to fill my array with random numbers:
Computers and Technology
1 answer:
olchik [2.2K]3 years ago
6 0

Answer:

See explaination

Explanation:

import java.util.Random;

public class Sample{

static class MinMax implements Runnable{

int []arr;

int start,end,min,max;

MinMax(int[]arr, int start,int end){

this.start=start;

this.end=end;

min=Integer.MAX_VALUE;

max=Integer.MIN_VALUE;

this.arr=arr;

}

atOverride

public void run() {

for(int i=start;i<=end;i++){ //search min and max form strant to end index

min=Math.min(min,arr[i]);

max=Math.max(max, arr[i]);

}

}

}

public static void main(String[] args) throws Exception{

long beginTime = System.nanoTime();

Random gen = new Random();

int n=100000000;

int[] data = new int[n]; //generate and fill random numbers

for(int i = 0; i < data.length; i++) {

data[i] = gen.nextInt()%1000000;

}

long endTime = System.nanoTime();

System.out.println("Done filling the array. That took " + (endTime - beginTime)/1000000000f + " seconds.");

//-----------------------------------------

System.out.println("Using 1 thread"); //1 thread

MinMax m1=new MinMax(data,0,n-1); //class object

Thread t1=new Thread(m1); //new thread

beginTime=System.nanoTime(); //start timer

t1.start(); //start thread

t1.join(0); //wait until thread finishes

endTime=System.nanoTime(); //end timer

System.out.println("Min,Max: "+m1.min+","+m1.max); //print minimum and maximum

System.out.println("Time using 1 thread " + (endTime - beginTime)/1000000000f + " seconds."); //print time taken

//-----------------------------------------

System.out.println("Using 2 thread");

m1=new MinMax(data,0,n/2);

MinMax m2=new MinMax(data,n/2+1,n-1);

t1=new Thread(m1);

Thread t2=new Thread(m2);

beginTime=System.nanoTime();

t1.start();

t2.start();

t1.join(0);

t2.join(0);

endTime=System.nanoTime();

System.out.println("Min,Max: "+ Math.min(m1.min,m2.min)+","+Math.max(m1.max,m2.max));

System.out.println("Time using 2 thread " + (endTime - beginTime)/1000000000f + " seconds.");

//-----------------------------------------

System.out.println("Using 3 thread");

m1=new MinMax(data,0,n/3);

m2=new MinMax(data,n/3+1,2*n/3);

MinMax m3=new MinMax(data,2*n/3+1,n-1);

t1=new Thread(m1);

t2=new Thread(m2);

Thread t3=new Thread(m3);

beginTime=System.nanoTime();

t1.start();

t2.start();

t3.start();

t1.join(0);

t2.join(0);

t3.join(0);

endTime=System.nanoTime();

System.out.println("Min,Max: "+ Math.min(Math.min(m1.min,m2.min),m3.min)+","+Math.max(Math.max(m1.max,m2.max),m3.max));

System.out.println("Time using 3 thread " + (endTime - beginTime)/1000000000f + " seconds.");

//-----------------------------------------

System.out.println("Using 4 thread");

m1=new MinMax(data,0,n/4);

m2=new MinMax(data,n/4+1,2*n/4);

m3=new MinMax(data,2*n/4+1,3*n/4);

MinMax m4=new MinMax(data,3*n/4+1,n-1);

t1=new Thread(m1);

t2=new Thread(m2);

t3=new Thread(m3);

Thread t4=new Thread(m4);

beginTime=System.nanoTime();

t1.start();

t2.start();

t3.start();

t4.start();

t1.join(0);

t2.join(0);

t3.join(0);

t4.join(0);

endTime=System.nanoTime();

System.out.println("Min,Max: "+ Math.min(Math.min(m1.min,m2.min),Math.min(m3.min,m4.min))+","+Math.max(Math.max(m1.max,m2.max),Math.max(m3.max,m4.max)));

System.out.println("Time using 4 thread " + (endTime - beginTime)/1000000000f + " seconds.");

}

}

You might be interested in
What are general purpose computer and special purpose computer?​
icang [17]

Answer:

General purpose computers are designed to be able to perform variety of tasks when loaded with appropriate programs, while special purpose computers are designed to accomplish a single task.

3 0
3 years ago
Read 2 more answers
What is control structure write it's types​ .
MAXImum [283]

Answer

<u>Defination</u><u>-</u><u>:</u>

A control statement is a statement and a statement whose execution its control.

<u>Types-:</u>

  • Selection Statement
  • Iteration Statement
  • Unconditional branching Statement
4 0
3 years ago
Given the IPv4 address in CIDR notation 215.200.110.50/25, identify the subnet ID that this address belongs to.
stich3 [128]
215.200.110.00, 215.100.110.64, 215.200.110.128,215.100.110.192
7 0
2 years ago
Which statement best describes top-down programming design?
MariettaO [177]

B is the answer (i think)

5 0
3 years ago
Dean Smith is dissatisfied with the time it takes to get a new faculty ID made and believes more servers will speed up service a
Juliette [100K]
The answer is :

X = 4

Explanation:
6 0
3 years ago
Other questions:
  • Describe the difference between gui and cli​
    9·1 answer
  • Let’s say you are given the task of retouching a famous model’s photograph. To what extent will you retouch the image? In your o
    12·2 answers
  • Think of a game you are familiar with and identify at least three team responsibilities which were required to make the game, on
    5·1 answer
  • Give two reasons to break up
    11·2 answers
  • In order to create a horizontal 2-inch-thick dotted green line exactly 2 inches away from the top of the canvas, a graphic artis
    14·2 answers
  • In ____, data can move in both directions at the same time, such as with a telephone.
    11·1 answer
  • Why is know app downloading in my android phone even if I have 900 MB ???
    8·2 answers
  • What does RoHS stand for and why is RoHS compliance important?
    9·1 answer
  • Peyton is having trouble searching for information on butter lettuce. Every time he searches for it, he gets some results for bu
    6·2 answers
  • The following is a function:
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!