Java Single Threaded – Execution time

Previous
Next

Execution time of Single Threaded application:

  • If the application contains only one thread, the execution becomes sequential.
  • To understand the execution time of application, we use pre-defined method of System class.
  • When only one thread executes in the application, the logic executes sequentially.
/*
class System
{
	public static long currentTimeMillis()
	{
		Returns the current time in milliseconds. 
	}
}
*/
class PrintNumbers
{
	static void print1to50()
	{
		for (int i=1 ; i<=50 ; i++)
		{
			System.out.println("i value : " + i);
			try{
				Thread.sleep(100);
			}catch(Exception e){ }
		}
	}
	static void print50to1()
	{
		for (int j=50 ; j>=1 ; j--)
		{
			System.out.println("j value : " + j);
			try{
				Thread.sleep(100);
			}catch(Exception e){ }
		}
	}
}

class ExecutionTime 
{
	public static void main(String[] args) 
	{
		long s = System.currentTimeMillis();
		PrintNumbers.print1to50();
		PrintNumbers.print50to1();
		long e = System.currentTimeMillis();

		System.out.println("Time taken : " + (e-s)/1000 + " Seconds");
	}
}
Previous
Next

Add Comment

Courses Enquiry Form