Create a Thread in Java (by extending Thread Class)

class MyThread extends Thread {

	public static int count = 0;
	public MyThread() {
	}

	public MyThread(String name) {
		super(name);
	}

	public void run() {
		while (count <= 10) {
		  try {
			System.out.println(Thread.currentThread().getName() +" count: " + count);
			count++;
			Thread.sleep(100);
		   } catch (InterruptedException iex) {
			  iex.printStackTrace();
		   }
		}
	}
}

public class ThreadExample {

	public static void main(String a[]) {

		System.out.println("Simple Thread Example by extending Thread class..");
		
		MyThread myThread = new MyThread("ThreadOne");
		myThread.start();

	}
}

Output
Simple Thread Example by extending Thread class.. ThreadOne count: 0 ThreadOne count: 1 ThreadOne count: 2 ThreadOne count: 3 ThreadOne count: 4 ThreadOne count: 5 ThreadOne count: 6 ThreadOne count: 7 ThreadOne count: 8 ThreadOne count: 9 ThreadOne count: 10










Related Examples


Write a program to check if an input String is Palindrome or not?
Calculate the factorial of a given number using Java?
How to reverse a number in Java?
Write a program to convert from decimal to binary?
Producer Consumer Example in Thread
Write a program to reverse a String?








Receive Latest Java Examples in Your Email: