0% found this document useful (0 votes)
34 views

Process Scheduled Program

process scheduled program in java

Uploaded by

Imran Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Process Scheduled Program

process scheduled program in java

Uploaded by

Imran Aslam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

Scanner;

public class Driver {


static long start;

public static void startTimer() {


start = System.nanoTime();
}

public static double stopTimer() {


double time = (double)(System.nanoTime() - start);
start = 0;
return time;
}

public static void main(String args[]) {


Scanner scan= new Scanner(System.in);
System.out.println("Enter the size of Array :");
int size;
size = scan.nextInt();
int numArray[]= new int [size+1];

for (int i=1 ; i<=size; i++ ) {

System.out.println("Enter the Number "+ i + " :");


numArray[i]= scan.nextInt();
}

Sum sumObject = new Sum();


Average averageObject = new Average();
Maximum maxObject = new Maximum();

// creating thread to calculate sum of all entered numbers


Thread thrd1 = new Thread(new Summation(numArray, sumObject));

// creating thread to calculate average of all entered numbers


Thread thrd2 = new Thread(new Averaging(numArray, averageObject));

// creating thread to calculate average of all entered numbers


Thread thrd3 = new Thread(new Maximizing(numArray, maxObject));

new java.util.Timer().schedule(new java.util.TimerTask() {


@Override
public void run() {
thrd1.start();
startTimer();
try {
thrd1.join();
System.out.println("The sum of entered numbers is " +
sumObject.getSum()
+ "\n and the Time Complexity of Summation is "
+ stopTimer() + " nanoseconds\n\n");
} catch (InterruptedException ie) {
}
}
}, 3000);
new java.util.Timer().schedule(new java.util.TimerTask() {
@Override
public void run() {
thrd2.start();
startTimer();
try {
thrd2.join();
System.out.println("The average of entered numbers is
" + averageObject.getAverage()
+ "\n and the Time Complexity of Averaging is " +
stopTimer() + " nanoseconds\n\n");
} catch (InterruptedException ie) {
}
}
}, 6000);

new java.util.Timer().schedule(new java.util.TimerTask() {


@Override
public void run() {
thrd3.start();
startTimer();
try {
thrd3.join();
System.out.println("The maximum of entered numbers is
" + maxObject.getMaximum()
+ "\n and the Time Complexity of Finding Maximum is " +
stopTimer() + " nanoseconds\n\n");
} catch (InterruptedException ie) {
}
}
}, 9000);

// Sum
class Sum {
private int sum;

public int getSum() {


return sum;
}

public void setSum(int sum) {


this.sum = sum;
}
}

class Summation implements Runnable {


private int[] numArray = new int[10];
private Sum sumValue;

public Summation(int[] numArray, Sum sumValue) {


this.numArray = numArray;
this.sumValue = sumValue;
}
public void run() {
int sum = 0;
for (int i = 0; i < numArray.length; i++) {
sum += numArray[i];
}
sumValue.setSum(sum);
}
}

// Average
class Average {
private int average;

public int getAverage() {


return average;
}

public void setAverage(int average) {


this.average = average;
}
}

class Averaging implements Runnable {


private int[] numArray = new int[10];
private Average averageValue;

public Averaging(int[] numArray, Average averageValue) {


this.numArray = numArray;
this.averageValue = averageValue;
}

public void run() {


int average = 0;
for (int i = 0; i < numArray.length; i++) {
average += numArray[i];
}
average /= numArray.length;
averageValue.setAverage(average);
}
}

// Maximum
class Maximum {
private int maximum;

public int getMaximum() {


return maximum;
}

public void setMaximum(int maximum) {


this.maximum = maximum;
}
}

class Maximizing implements Runnable {


private int[] numArray = new int[10];
private Maximum maxValue;

public Maximizing(int[] numArray, Maximum maxValue) {


this.numArray = numArray;
this.maxValue = maxValue;
}

public void run() {


int max = numArray[0];
for (int i = 1; i < numArray.length; i++) {
if (numArray[i] > max) {
max = numArray[i];
}
}
maxValue.setMaximum(max);
}
}

You might also like