Share on Google+Share on Google+

Java MultiThread

Advertisement
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

Advertisements

Posted on: November 7, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.