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
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!

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative