Java MultiThread


 

Java MultiThread

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.

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

Ads