by Dinesh Thakur Category: Multithreading

One way to create a thread is to create a new class that extends Thread, and then to create an instance of that class. The extending class must override the run() method, which is the entry point for the new thread. It must also call start() to begin execution of the new thread.

class ExtendingThread extends Thread

     {
           String s[]={"Welcome","to","Java","Programming","Language"};
           public static void main(String args[])
              {
             ExtendingThread t=new ExtendingThread("Extending Thread Class");
              }
                public ExtendingThread (String n)
                 {
                    super(n);
                    start();
                }
                public void run()
                  {
                      String name=getName();
                      for(int i=0;i<s.length;i++)
                          {
                         try
                          {
                            sleep(500);
                          }
                            catch(Exception e)
                               {
                               }
                                System.out.println(name+":"+s[i]);
                          }
                      }
      }

Extending Thread Class in Java Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content





Popular Article