Multithreading in java

When more than one task is executing concurrently within a single program is called multithreading in Java . Thread in Java is controlled by java.lang.Thread class. Each program can be divided into number of small process . Each small process is called a single thread . Thread is a lightweight process. Thread can be created using this two way .
  • implementing Runnable interface 
  • extending Thread class 
  Advantage of Thread : 
  •       Threads are independent 
  •        Each task of a program can be executed and shared separated memory area. 
  •        It's a lightweight process . 
  •       Communication coast is low between the Threads .
  Life cycle of a thread :
                                



 Example using Runnable interface :


 public class HelloRunnable implements Runnable {

    public void run() {
        System.out.println("Hello from a runnable thread!");
    }

    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
    }

}


Example using Thread interface :


public class HelloThread extends Thread {

    public void run() {
        System.out.println("Hello from a thread!");
    }

    public static void main(String args[]) {
        (new HelloThread()).start();
    }

}


Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java 
          Youtube : Android & Java Tutorial 

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Constructor in Java