Can we call run() method directly?

Previous
Next

Can we call run() method directly?

  • Yes allowed but the logic executes from the same thread only.
  • Run() contains the logic that should execute parallel from independent memory.
  • Start() method is pre-defined in Thread class.
  • Start() method contains logic to allocate separate thread memory and executes run() logic from allocated space.
class Custom extends Thread
{
	public void run()
	{
		for (int i=1 ; i<=10 ; i++)
		{
			System.out.println("Custom : " + i);
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){ }
		}
	}
}
class Default 
{
	public static void main(String[] args) 
	{
		Custom th = new Custom();
		th.run();
		
		for (int j=1 ; j<=10 ; j++)
		{
			System.out.println("Default : " + j);
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){ }
		}
	}
}
Previous
Next

Add Comment

Courses Enquiry Form