24 Oct 2015

Patterns with Thread

This is a simple java program in which patterns are displayed character by character with the help of thread.

Code:

public class PatternWithThread extends Thread{
int no = 1;
char ch = 'a';   
    public void run(){
        try{
             for(int i = 1;i <= 5;i ++)
             {
                 for(int j = 1;j <= i;j ++)
                {               
                Thread.sleep(250);
                System.out.print(i);
                }
            System.out.println("");
            }

        System.out.println("");
            
             for(int i = 1;i <= 5;i ++)
            {
                 for(int j = 1;j <= i;j ++)
                {               
                Thread.sleep(250);
                System.out.print("*");
                }
             System.out.println("");
           }

         System.out.println("");
           
            for(int i = 1;i <= 5;i ++)
           {
                for(int j = 0;j < i;j ++)
               {               
               Thread.sleep(250);
               System.out.print((i+j)%2);
               }
           System.out.println("");
           }

         System.out.println("");
           
            for(int i = 1;i <= 5;i ++)
            {
                 for(int j = 1;j <= i;j ++)
                {               
                Thread.sleep(250);
                       if(i%2 != 0)
                      {
                      System.out.print(no);
                      no++;
                      }
                      else
                      {
                      System.out.print(ch);
                      ch++;   
                      }
                }
            System.out.println("");
            }
      }
      catch(Exception e){}   
 }   
 public static void main(String args[])
 {
 PatternWithThread obj = new PatternWithThread();
 obj.start();   
 }
}

Snapshot: