Home Tutorial Java Thread Java MultiThread

 
 

Java MultiThread
Posted on: November 7, 2009 at 12:00 AM
In this segment of tutorial we will learn about the MultiThread in the java and how it is used.Then We will create an example for the Multi thread and print the output.

  • Java has multithreading feature.
  • Multithreading feature is given by Thread class and Runnable interface
  • Java Multithreading allows to run multiple tasks(threads) at a time.


Java Multi Thread Example
public class multi extends Thread {
	@Override
	public void run() {
		System.out.println();
		for (int x = 1; x <= 3; x++) {
			System.out.println(x + " Thread name"
					+ Thread.currentThread().getName());
		}
	}

	public static void main(String[] args) {
		multi t1 = new multi();
		t1.start();

		multi t2 = new multi();
		t2.start();

		multi t3 = new multi();
		t3.start();
	}
}

Output:

1 Thread nameThread-1 2 Thread nameThread-1 1 Thread nameThread-0 2 Thread nameThread-0 3 Thread nameThread-0 1 Thread nameThread-2 2 Thread nameThread-2 3 Thread nameThread-2 3 Thread nameThread-1

Related Tags for Java MultiThread:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.