Tuesday, December 20, 2016

Write a JAVA program to create a class MyThread in this class a constructor, call the base class constructor, using super and starts the thread. The run method of the class starts after this. It can be observed that both main thread and created child thread are executed concurrently

Aim: Write a JAVA program to create a class MyThread in this class a constructor, call the base class constructor, using super and starts the thread. The run method of the class starts after this. It can be observed that both main thread and created child thread are executed concurrently.

Program:

class Thread1 extends Thread
{
Thread1()
{
super();
start();
}
public void run()
{
    for ( int i=1; i<=10; i++)
    {
       
        System.out.println( "Message from  Thread1 : " +i);

  try
{

        Thread.sleep (1000);
}
catch(InterruptedException interruptedException)
        {
         
            System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);
        }
    }
}
}
public class ThreadDemo1
{
     public static void main(String args[])throws InterruptedException
     {
        Thread1  firstThread = new Thread1();
for ( int i=1; i<=10; i++)
        {
       
        System.out.println( "Message from  main method : " +i);
        Thread.sleep(500);
 
         }
     }
}


Output:


Labels:

1 Comments:

At April 18, 2020 at 11:44 PM , Blogger N.T.A said...

where is the super class mythread?

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home