OCJP

Multi threading basics in Java

Java supports execution of multiple processes at a same time. Thread is in-build class defined in java.lang package. There is run() that holds the process/statments to be executed at same time. There two methods to develop a thread, we will discuss later in our blog. Below is the example of Extending Threads

class Thread1 extends Thread{
	public void run()
	{
		for(int i=1;i<=100;i++){
		System.out.print(" "+i);
		}
	}
}

class MultiThreadDemo1{
	public static void main(String []a){
	
	Thread1 thr1 = new Thread1();
	Thread1 thr2 = new Thread1();
	
	
	thr1.start();
	thr2.start();
	
	}
}

Here Thread1 class extends Thread and there is run() in same class. To execute the process we have to call start(). start() will execute run().

Output of Multithreading Program is not same as each time, it depends on time provided for each process by processor.

Do not forget to subscribe to get more posts related to programming.

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?