When threads are not lightweight process in java, in fact they become heavy weight process





Threads are lightweight process only if threads of same process are executing concurrently. But if threads of different processes are executing concurrently then threads are heavy weight process.



Threads as heavy weight process : In the below program we will create Thread-1 and Thread-2 on different processes i.e. on MyRunnable1 and MyRunnable2.
class MyRunnable1 implements Runnable{
   public void run(){
          for(int i=0;i<5;i++){
                 try {
                       Thread.sleep(100);
                 } catch (InterruptedException e) {
                       e.printStackTrace();
                 }
                 System.out.println("i="+i+" ,ThreadName="+Thread.currentThread().getName());
          }         
   }
}
class MyRunnable2 implements Runnable{
   public void run(){
          for(int i=0;i<5;i++){
                 try {
                       Thread.sleep(100);
                 } catch (InterruptedException e) {
                       e.printStackTrace();
                 }
                 System.out.println("i="+i+" ,ThreadName="+Thread.currentThread().getName());
          }         
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass {
   public static void main(String...args){
          Thread thread1=new Thread(new MyRunnable1(),"Thread-1");
          Thread thread2=new Thread(new MyRunnable2(),"Thread-2");
          thread1.start();
          thread2.start();
   }
}
/*OUTPUT
i=0 ,ThreadName=Thread-1
i=0 ,ThreadName=Thread-2
i=1 ,ThreadName=Thread-1
i=1 ,ThreadName=Thread-2
i=2 ,ThreadName=Thread-1
i=2 ,ThreadName=Thread-2
i=3 ,ThreadName=Thread-1
i=3 ,ThreadName=Thread-2
i=4 ,ThreadName=Thread-1
i=4 ,ThreadName=Thread-2
*/



Threads as light weight process : In the below program we will create Thread-1 and Thread-2 on same process i.e. on MyRunnable.
class MyRunnable implements Runnable{
   public void run(){
          for(int i=0;i<5;i++){
                 try {
                       Thread.sleep(100);
                 } catch (InterruptedException e) {
                       e.printStackTrace();
                 }
                 System.out.println("i="+i+" ,ThreadName="+Thread.currentThread().getName());
          }         
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass {
   public static void main(String...args){
          MyRunnable myRunnable=new MyRunnable();
          Thread thread1=new Thread(myRunnable,"Thread-1");
          Thread thread2=new Thread(myRunnable,"Thread-2");
          thread1.start();
          thread2.start();
   }
}
/*OUTPUT
i=0 ,ThreadName=Thread-1
i=0 ,ThreadName=Thread-2
i=1 ,ThreadName=Thread-2
i=1 ,ThreadName=Thread-1
i=2 ,ThreadName=Thread-2
i=2 ,ThreadName=Thread-1
i=3 ,ThreadName=Thread-2
i=3 ,ThreadName=Thread-1
i=4 ,ThreadName=Thread-2
i=4 ,ThreadName=Thread-1
*/





RELATED LINKS>

Thread basics >

What is thread in java

Thread states/ Thread life cycle in java

Difference between Process and Thread in java

Implementing Threads in java by implementing Runnable interface and extending Thread class

Threads implement their own stack - demonstration using program and diagram

Differences between implementing Runnable interface and extending Thread class

Thread behaviour is unpredictable

When threads are not lightweight process in java, in fact they become heavy weight process



eEdit
Must read for you :