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
Use the drop-down menus to complete the steps to share a presentation through OneDrive.
charle [14.2K]

Answer:

Share

OneDrive

In real time

Send

Explanation:

5 0
3 years ago
The means by which you interact with any program on a computer is called the ____. Answer
Elodia [21]
User interface is the answer
4 0
3 years ago
In traditional programming, probably the most often used error-handling outcome was to ____.Group of answer choicesterminate the
iVinArrow [24]

In traditional programming, probably the most often used error-handling outcome was to terminate the program in which the offending statement occurred.

<h3>What is a traditional programming?</h3>

Traditional programming is known to be a form of manual way that one or a user makes a program.

Note that in the case above, In traditional programming, probably the most often used error-handling outcome was to terminate the program in which the offending statement occurred.

Learn more about programming from

brainly.com/question/23275071

#SPJ1

4 0
2 years ago
a. displays the sum of all even numbers between 2 and 100 (inclusive). b. displays the sum of all squares between 1 and 100 (inc
LuckyWell [14K]

Answer:

The program required is in the explanation segment.

Explanation:

Program :

import math

# a. displays the sum of all even numbers between 2 and 100 (inclusive).

print("All even numbers from 2 to 100 inclusive ")

sum=0

i=2

while i<=100:

if i %2 ==0:

sum=sum+i

print(i,end=" ")

i=i+1

print("\nThe sum of all even numbers between 2 and 100 (inclusive) :",sum);

#b. displays the sum of all squares between 1 and 100 (inclusive).

print("\nAll squares numbers from 1 to 100 inclusive:")

i=1

sum=0

while i<=100:

print(i*i,end=" ")

i=i+1

sum=sum+(i*i)

print("\n\nThe sum of all squares between 1 and 100 (inclusive) is :",sum)

#c. displays the powers of 2 from 1 up to 256.

print("\nAll powers of 2 from 2 ** 0 to 2 ** 8:")

i=0

while True:

p=math.pow(2,i)

if p>256:

break

print("2 ** ",i," is ",int(p))

i=i+1

#d. displays the sum of all odd numbers between a and b (inclusive), where a and b are inputs

print("\nCompute the sum of all odd integers between two intgers ")

a=int(input("Enter an integer:"))

b=int(input("Enter another integer: "))

count = 0

temp=a

sum=0

while a<=b:

if a%2!=0:

print(a,end=" ")

sum=sum+a

a=a+1

print("\nThe total of the odd numbers from ", temp ," to ", b ,"is",sum)

#e.displays the sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17.)

print("\nCompute the sum of the odd digits in an integer ")

n=int(input("Enter an integer:"))

count=0

sum=0

temp=n

while n!=0:

rem = n%10

if rem%2!=0:

sum=sum+rem

count=count+1

n=int(n/10)

print("Sum of the odd digits is ",sum)

print("The total of the odd digits in ",temp," is ",count)

4 0
4 years ago
Write a program that has a conversation with the user. The program must ask for both strings and numbers as input. The program m
konstantin123 [22]

Answer:

yo im sorry eat my cookie

Explanation

doorkoeeworkwoeroewkrwerewrwe

5 0
4 years ago
Other questions:
  • What is one example of technology influencing health​
    9·1 answer
  • What are the main purposes of regulatory policies? Check all that apply.
    5·2 answers
  • Almost immediately after a server migration project, employees are complaining that they can't reach the Internet. You sit down
    8·1 answer
  • Netflix shows to watch?
    11·2 answers
  • Name all mario kart games in order
    15·2 answers
  • Robyn needs to ensure that a command she frequently uses is added to the Quick Access toolbar. This command is not found in the
    9·1 answer
  • What are the benefits and risks of a client-server network?
    5·1 answer
  • Help pweeze this is due today :(<br><br> I will give u brainliest just pweeze, I need this answer :(
    8·1 answer
  • Why womt this code work????
    10·1 answer
  • Which backup requires a small amount of space and is considered to have an involved restoration process?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!