In: Computer Science
import java.io.*;
import java.util.concurrent.TimeUnit;
import java.util.Random;
class Main
{
public static void main(String args[])throws IOException
{
int arrayOfProcesses[] = new int[10];
int i = 0;
while (i < 10)
{
try
{
TimeUnit.MILLISECONDS.sleep(100);
}
catch(InterruptedException e)
{
// this part is executed when an exception (in this example
InterruptedException) occurs
}
arrayOfProcesses[i] = new Random().nextInt(100);
i++;
}
int initialTotalTimeTaken = 0;
i = 0;
while (i < 10)
{
System.out.println("Initial Time taken by " + i + " process is " +
arrayOfProcesses[i] + " milliseconds");
initialTotalTimeTaken += arrayOfProcesses[i];
i++;
}
System.out.println();
i = 0;
while (i < 10)
{
try
{
TimeUnit.MILLISECONDS.sleep(100);
}
catch(InterruptedException e)
{
// this part is executed when an exception (in this example
InterruptedException) occurs
}
arrayOfProcesses[i] = new Random().nextInt(70);
i++;
}
int newTotalTimeTaken = 0;
i = 0;
while (i < 10)
{
System.out.println("New Time taken by " + i + " process is " +
arrayOfProcesses[i] + " milliseconds");
newTotalTimeTaken += arrayOfProcesses[i];
i++;
}
System.out.println("\nTotal time taken initially by all
processes = " + initialTotalTimeTaken + " milliseconds");
System.out.println("Total time taken later by all processes = " +
newTotalTimeTaken + " milliseconds");
}
}