Random Execution

Multithreading is used to divide a program in to several parts which can be executed in parallel. Java supports multithreading which is absolutely true but Java as a programming language cannot execute the instruction in microprocessor in parallel(by switching). Java relies on operating system for that. So, what we are going to prove here is: Execution/Switching of the threads are done by Operation System at run time. So there will be random execution sequence for us every time we run the program. You can see the output displayed below:

class RandomExecution implements Runnable
{
	Thread t;
	RandomExecution()
	{
		t = new Thread(this);
		t.start();
		for(int i=0;i<10;i++)
		{
			System.out.println("Parent:"+i);	
		}	
			
	}
	public static void main(String[] s)
	{
		RandomExecution obj = new RandomExecution();	
	}	
	public void run()
	{
		for(int i=0;i<10;i++)
		{
			System.out.println("Run:"+i);	
		}	
	}
}

Output:
Random printing example




Random printing example