Friday, 28 February 2014

Introduction To Threads in java

Simply Thread is called as a light weight Process.

Each thread has its own local variables, program counter, and its life cycle independent on other threads.

Even a simple Hello world application also multi threaded in Java. Whenever you starts an application, main thread is created and starts its execution, and garbage collector thread used to run in back ground.

By default main thread present in an application

Example
class MainThread{
 public static void main(String args[]){
  System.out.println(Thread.currentThread().getName());
 }
}

Output
main
   
Why to use Threads
a. To make the User Interface more responsive
b. To utilize the resources efficiently
c. To take the advantage of multiprocessor systems
d. To perform background processing tasks
e. To perform other operations, while one operation is waiting for I/O, or blocked etc.,


Extract info from stack trace element                                                 Create Thread by Extending Thread class                                                 Home

No comments:

Post a Comment