by Dinesh Thakur Category: Multithreading

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

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