public class JavaThreadExample {
	public static int counter = 0;
	public static void main(String[] args) {
		Thread t1 = new Thread(new MyThread("thread 1"));
		Thread t2 = new Thread(new MyThread("thread 2"));
		t1.start();
		t2.start();
		System.out.println("threads started ...");
	}
}
class MyThread implements Runnable {
	protected String text;
	public MyThread(String text) {
		this.text = text;
	}
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println("hello from " + text + " (counter="
					+ JavaThreadExample.counter + ")");
			JavaThreadExample.counter++;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException ie) {
				System.out.println("An InterruptedException occurred\n"
						+ ie.toString() + "\n" + ie.getMessage());
			}
		}
	}
}