1.How to Control Main Thread in Java

 /**  
 controlling the main thread..  
 */  
 class CurrentThreadDemo  
 {  
      public static void main(String[] args)   
      {  
           Thread t = Thread.currentThread(); //shows the current Thread with properties{Name, priority,group to which it belongs to}  
           System.out.println("Current Thread is:" +t);  
           t.setName("My Thread");//set name property..(setName())  
           System.out.println("Current Thread is:" +t);//we get getName()..  
           try  
           {  
                for (int n=5;n>0;n-- )  
                {  
                     System.out.println(n);  
                     Thread.sleep(1000); //it throws Interrupted Execution Exception..  
                }  
           }  
           catch (InterruptedException e)  
           {  
                System.out.println("Method Thread interrupted");  
           }  
      }  
 }  

Post a Comment

0 Comments