Java Lambda Expression in Multithreading

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

class TestMyThread
{
    public static void main(String args[]) throws Exception 
    {
           Runnable r1=()->{
            try
          {  
              for(int i=1;i<=10;i++)
              {
                 System.out.println("This is child Thread ");
                 Thread.sleep(1000);
              }   
          }
          catch(Exception e)
          {}
           };
         Thread t1=new Thread(r1); 
          t1.start();
         for(int i=1;i<=10;i++)
          {
              System.out.println("This is Main Thread ");
              Thread.sleep(1000);
          }

    }
}





// class MyThread implements Runnable
// {
//       public void run()
//       {
//          try
//         { 
//           for(int i=1;i<=10;i++)
//           {
//               System.out.println("This is child Thread ");
//               Thread.sleep(1000);

//           }
//         }
//         catch(Exception e)   
//         {
//         }
//       }
// }

// class TestMyThread
// {
//     public static void main(String args[]) throws Exception
//     {
//            MyThread M1=new MyThread();
//            Thread T1=new Thread(M1);
//            T1.start();
//          for(int i=1;i<=10;i++)
//           {
//               System.out.println("This is Main Thread ");
//               Thread.sleep(1000);
//           }
//     }
// }

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *