by Dinesh Thakur

In this Java Example, a reference to the current thread (the main thread) is obtained by calling currentThread(), and this reference is stored in a local variable t. Then, we display the information about the thread. After that, a loop counts from 0 to 10, pausing one second between each line. The pause is accomplished by the sleep() method. The argument to sleep() specifies the delay period in milliseconds. The sleep() method might throw an InterruptedException if some other thread interrupts the sleeping one.

 

  import java.io.*;

  class currentThreadJavaExample
     {
        public static void main(String args[])
          {
             Thread t= Thread.currentThread();
              int i;
              System.out.println("Current thread is:"+t);
               for(i=0;i<=10;i++)
                  {
                         System.out.print(i + " ");
                       try
                       {
                          t.sleep(1000);
                       }
                         catch(InterruptedException e)
                         {}
                  }
          }
     }

currentThread() in Java Example