evlmangesh 0 Newbie Poster

Hi guys,

This code has been on my back since a few days now and I kinda need help on it ASAP.
Basically what I wanna do is use PipedInputStream and PipedOutputStream to accurately time thread create operations. So I have to calculate System.nanoTime() in the run() method of the thread and send it over to the main thread for computation of its latency where I would already have calculated Beginning time of the thread. Only usage of PIS and POS is allowed using the class implementing runnable is not allowed. How do I do It?

//<editor-fold defaultstate="collapsed" desc="comment">
/*Command line argument program. Input values at runtime. Will give out the thread creation time and valency in nanoseconds.*/

import java.util.*;

    class NewThread implements Runnable  //worker thread 2
{
    public void minmax(double arr[])// prints the smallest and the largest int in an int array
    {
        Arrays.sort(arr);
        double min=arr[0];
        double max=arr[arr.length-1];
        System.out.println("Min is= "+min);
        System.out.println("Max is= "+max);
    }
    void median(double brr[])//Computes the median in a double array
    {
        double med;
        Arrays.sort(brr);
        if(brr.length%2==0)
        {
            med=(brr[brr.length/2]+brr[brr.length/2+1])/2;
        }
        else
        {
            med=(brr[((brr.length/2)+1)/2]);
        }
        System.out.println("Median = "+med);
    }

    void dispAll(double arr[])//displays the contents of a String array

    {
		System.out.println("Actual Values:\n");

        for(int i=0;i<arr.length;i++)
        {
            System.out.println(arr[i]);
        }

    }

    @Override
        public void run() {

    }
}



class NewThread1 extends NewThread implements Runnable //worker thread 2
{
	double BEG_TIME;
	double END_TIME;
	double[] actval;
	double avg=0;

    @Override
    public void run() //computation of nanoTime() in the run() method
    {
	END_TIME=System.nanoTime();
    }
}


// main class starts...........
public class RunTimeThreadCreate
{
	static int verboseflag=0;
	static int errorflag=0;


	static void checkUsage(String a[])
	{
          try{
            String n=a[0];
            int num=Integer.parseInt(a[1]);
            if(a.length<2||a.length>3||!(n.equals("-n")))
            {
                usage();
                            }
			}catch(Exception e)
			{
				usage();
			}
				if(a.length==3)
				verboseflag=1;
	}

	static void usage()
	    {
			errorflag=1;
	        System.out.println("Usage: RunTimeThreadCreate [-n] num [-v]");

	    }

	static void case1(String a[])
	{

		//errorflag=0;
		int i=0;
		double BEG_TIME;
		double END_TIME;
		double avg=0;
		int n=Integer.parseInt(a[1]);
		double[] actval=new double[n];

		 while(i<n)
	  {
		  try
	  {
		   Thread t=new Thread(new NewThread());
			BEG_TIME=System.nanoTime();
			t.start();
			t.join();
			END_TIME=System.nanoTime();
			actval[i]=END_TIME-BEG_TIME;
			avg=avg+actval[i];
			i++;

	 }catch(InterruptedException e)
	  {}

	  }
		  System.out.println("=============TIMING RESULTS==============\n\n");

			System.out.println(" ====CASE 1==== ");
			avg=avg/n;
			NewThread nt=new NewThread();
			if(verboseflag==1)
			{
					nt.dispAll(actval);

			}
			nt.minmax(actval);
			nt.median(actval);
			System.out.println("Average is= "+avg);
            System.out.println();


	}
	//CASE 2
static void case2(String a[])
{
//errorflag=0;
int j=0;
double BEG_TIME;
double END_TIME;
double avg=0;
int n=Integer.parseInt(a[1]);

System.out.println("====CASE 2====");
NewThread1 nt1 = new NewThread1();
nt1.actval=new double[n];
PipedInputStream pis;
PipedOutputStream pos;

	while(j<n)
	{
		try
		{
			Thread t1=new Thread(nt1);
			nt1.BEG_TIME=System.nanoTime();
			t1.start();
			t1.join();
			nt1.actval[j]=nt1.END_TIME-nt1.BEG_TIME;
			nt1.avg=nt1.avg+nt1.actval[j];
			j++;
		}catch(InterruptedException ignore)
		{ }
	}
	if(verboseflag==1)
	{
	nt1.dispAll(nt1.actval);
	}
	if(errorflag==1)
	{
		usage();
	}
nt1.minmax(nt1.actval);
nt1.median(nt1.actval);
nt1.avg=nt1.avg/n;
System.out.println("Average is= "+nt1.avg);
}


    public static void main(String args[])

   {

		checkUsage(args);
			if(errorflag==0)
			{
			case1(args);
			case2(args);
			}
   }
}
/*OUTPUT OF THIS PROGRAM

C:\Java Programs>javac RunTimeThreadCreate.java

C:\Java Programs>java RunTimeThreadCreate -n numtimes [-v]
Usage: RunTimeThreadCreate [-n] num [-v]

C:\Java Programs>java RunTimeThreadCreate -n abc [-v]
Usage: RunTimeThreadCreate [-n] num [-v]

C:\Java Programs>java RunTimeThreadCreate -n 1 2 3 4
Usage: RunTimeThreadCreate [-n] num [-v]

C:\Java Programs>java RunTimeThreadCreate -n 1 -v
=============TIMING RESULTS==============


 ====CASE 1====
Actual Values:

432946.0
Min is= 432946.0
Max is= 432946.0
Median = 432946.0
Average is= 432946.0

====CASE 2====
Actual Values:

255619.0
Min is= 255619.0
Max is= 255619.0
Median = 255619.0
Average is= 255619.0

C:\Java Programs>java RunTimeThreadCreate -n 5 -v
=============TIMING RESULTS==============


 ====CASE 1====
Actual Values:

290679.0
204775.0
195136.0
172368.0
175860.0
Min is= 172368.0
Max is= 290679.0
Median = 175860.0
Average is= 207763.6

====CASE 2====
Actual Values:

247098.0
203168.0
163778.0
171251.0
171181.0
Min is= 163778.0
Max is= 247098.0
Median = 171181.0
Average is= 191295.2

C:\Java Programs>java RunTimeThreadCreate -n 500
=============TIMING RESULTS==============


 ====CASE 1====
Min is= 157632.0
Max is= 354444.0
Median = 192867.0
Average is= 200402.386

====CASE 2====
Min is= 128718.0
Max is= 287117.0
Median = 139473.0
Average is= 144694.526

C:\Java Programs>java RunTimeThreadCreate -n 5000
=============TIMING RESULTS==============


 ====CASE 1====
Min is= 147784.0
Max is= 727816.0
Median = 316102.0
Average is= 297014.4684

====CASE 2====
Min is= 139054.0
Max is= 685841.0
Median = 290155.5
Average is= 274203.6502

C:\Java Programs>java RunTimeThreadCreate -n 10000
=============TIMING RESULTS==============


 ====CASE 1====
Min is= 145549.0
Max is= 786134.0
Median = 203307.5
Average is= 219095.6501

====CASE 2====
Min is= 131581.0
Max is= 876787.0
Median = 177501.5
Average is= 189203.6757
*/

Basically as in case2(), using nt1.actval[j]=nt1.END_TIME-nt1.BEG_TIME; is not allowed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.