Search Java Code Snippets


  Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.





Java - Code Samples

 Sample 1. Write a Program to show Java thread usage by extending Thread class

public class MyClass {
   static class MyThreadClass extends Thread{

      @Override
      public void run() {
         System.out.println("Hello");
         try {
            Thread.sleep(1000);
            System.out.println("Hello Again");
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
         
      }
      
   }
   
   public static void main(String[] args){
      MyThreadClass myThreadClass = new MyThreadClass();
      myThreadClass.start();
      
      MyThreadClass myThreadClass2 = new MyThreadClass();
      myThreadClass2.start();
   }
}

   Like      Feedback     Threads  Thread class



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner