Program to create two threads such that one thread displays the message "How do you do ?" and the other thread displays the message "Fine, Thank you!"
In this program, we have created two threads, first thread will print "How do you do?" and other thread will print "Fine, Thank you!". Here the start( ) method is called in class's constructor. Note that the output of thread programs is not same all the time it may vary. The start( ) method causes the thread to begin execution, the JVM calls the run( ) method of the thread.
PROGRAM
PROGRAM
class MyThread extends Thread {
String msg;
public MyThread(String msg) {
this.msg = msg;
start();
}
public void run() {
System.out.println(msg);
}
public static void main(String[] args) {
MyThread t1 = new MyThread("How do you do ?");
MyThread t2 = new MyThread("Fine, Thank you!");
}
}
OUTPUT
C:\>javac MyThread.java C:\>java MyThread How do you do ? Fine, Thank you!