import java.util.Random;


public class ThreadExample {
	public static void main(String[] args) {
		Thread thread1 = new MyThread();
		Thread thread2 = new Thread(new MyRunnable());
		
		thread1.start();
		thread2.start();
		try { 
			Thread.sleep(Math.abs(new Random().nextInt() % 1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("All threads started");
		
		// Anonymous inner class version of option 2.
		Thread thread3 = new Thread(new Runnable() {
			public void run() {
				System.out.println("Anonymous class thread here");
			}
		});
		
		thread3.start();
	}
}

// First option, subclass Thread.
class MyThread extends Thread {
	public void run() {
		try { 
			Thread.sleep(Math.abs(new Random().nextInt() % 1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("This message is from the Thread subclass");
	}
}

// Second option, implement Runnable.
// This could also be an anonymous inner class.
class MyRunnable implements Runnable {
	public void run() {
		try { 
			Thread.sleep(Math.abs(new Random().nextInt() % 1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("This message is from the Runnable implementation");
	}
}

