Multithreading in Java

Multithreading in java is a process of executing multiple threads simultaneously.

Life Cycle

  1. New
  2. Runnable
  3. Running
  4. Non-Runnable (Blocked)
  5. Terminated

Life Cycle of Thread

Create thread

  1. By extending Thread class
    This essentially is another implementation of runnable interface.

thread runs by calling start function

1
2
3
4
5
6
7
8
9
class Multi extends Thread{  
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
  1. By implementing Runnable interface
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    class Multi3 implements Runnable{  
    public void run(){
    System.out.println("thread is running...");
    }
    public static void main(String args[]){
    Multi3 m1=new Multi3();
    Thread t1 =new Thread(m1);
    t1.start();
    }
    }
  2. By Callable and Future
    To be finished.

    Thread method

    ####non-static method used frequently
  • start() note that we can not start a thread twice
  • run()
  • (final) setName(String name)
  • (final) setPriority(int priority)
    3 constants defined in Thread class:
    1. Default priority of a thread is 5 (NORM_PRIORITY)
    2. the value of MIN_PRIORITY is 1
    3. the value of MAX_PRIORITY is 10
  • (final) setDaemon(boolean on)
    set as daemon before start method
  • (final) join(long millisec)
    The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
  • interrupt()
  • (final) isAlive()
    ####static method
  • yield()
  • sleep(long millisec)
    The Thread class provides two methods for sleeping a thread:
    1. public static void sleep(long miliseconds)throws InterruptedException
    2. public static void sleep(long miliseconds, int nanos)throws InterruptedException
  • holdsLock(Object x)
  • currentThread()
    The currentThread() method returns a reference of currently executing thread.
  • dumpStack()
    ####Example 1 (join and sleep method)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    class TestJoinMethod1 extends Thread{  
    public void run(){
    for(int i=1;i<=5;i++){
    try{
    Thread.sleep(500);
    }catch(Exception e){System.out.println(e);}
    System.out.println(i);
    }
    }
    public static void main(String args[]){
    TestJoinMethod1 t1=new TestJoinMethod1();
    TestJoinMethod1 t2=new TestJoinMethod1();
    TestJoinMethod1 t3=new TestJoinMethod1();
    t1.start();
    try{
    t1.join();
    }catch(Exception e){System.out.println(e);}

    t2.start();
    t3.start();
    }
    }
    output:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    1
    2
    3
    4
    5
    1
    1
    2
    2
    3
    3
    4
    4
    5
    5
    As you know well that at a time only one thread is executed. If you sleep a thread for the specified time, the thread scheduler picks up another thread and so on.
    ####Example 2 (join and sleep method)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    class TestJoinMethod2 extends Thread{  
    public void run(){
    for(int i=1;i<=5;i++){
    try{
    Thread.sleep(500);
    }catch(Exception e){System.out.println(e);}
    System.out.println(i);
    }
    }
    public static void main(String args[]){
    TestJoinMethod2 t1=new TestJoinMethod2();
    TestJoinMethod2 t2=new TestJoinMethod2();
    TestJoinMethod2 t3=new TestJoinMethod2();
    t1.start();
    try{
    t1.join(1500);
    }catch(Exception e){System.out.println(e);}

    t2.start();
    t3.start();
    }
    }
    output:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    1
    2
    3
    1
    4
    1
    2
    5
    2
    3
    3
    4
    4
    5
    5
    In the above example,when t1 is completes its task for 1500 miliseconds(3 times) then t2 and t3 starts executing.