Core Java - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Multithreading

Java Multithreading

shape Description

Java Multithreading, Doing some work or process is called Thread. Multi Threading is nothing but process of managing and executing multiple tasks at a time. Before discussing more about user has to learn the terminology as follows.

Thread Life Cycle

shape Description

Java Multithreading, While starting any thread, thread can be any one of the state as follows. According to sun micro thread life cycle only follows 4 states, for the better understanding purpose Running state is also added. New: When creating an object to the Thread class then thread will be in New state, but this process will be started before invoking start(). Runnable: The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread. Running: Thread is in Running state if thread scheduler selected it. Non-Runnable(Blocked):Here some methods will be performed like sleep(). Terminated: Thread goes to dead state after the completion of task.

Creating A Thread

shape Description

Developer can create the thread in two ways as follows.

By extending Thread class

Java Multithreading, Following is an example to create a Thread by extending Thread class. creatingthread.java [java]class creatingthread extends Thread{ public void run(){ System.out.println("Hello, Thread is running..."); } public static void main(String args[]){ creatingthread splesson=new creatingthread(); splesson.start(); } } [/java] Where developer provided method called public void run(), this needs to be used. [java]public void run(){ System.out.println("Hello, Thread is running..."); } [/java] To start the thread developer has to use start(). [java]splesson.start(); [/java] Output When compile the code following is the result will be displayed. [java]Hello, Thread is running...[/java]

By implementing the Runnable interface

Following is an example to create a Thread by implementing the Runnable inerface . creatingthread.java [java]class creatingthread implements Runnable{ public void run(){ System.out.println("Hello, thread is running..."); } public static void main(String args[]){ creatingthread m1=new creatingthread(); Thread t1 =new Thread(m1); t1.start(); } } [/java] Java Multithreading, The difference between extends Thread and implements Runnable is that once if the developer declare the extends then there will be no scope to extend other classes, but it is possible in Runnable interface. Output When compile the code following is the result will be generated. [java]Hello, thread is running...[/java]

Difference between implements Runnable and extends Thread

  • Java supports single inheritance so user can only extend single class.
  • Using the Runnable instance to encapsulate the code which should run in parallel provides better reusability. User can pass that Runnable to any other thread or thread pool.
  • When user extends Thread class, after that user can’t extend any other class which required.
  • When user extends Thread class, each of thread creates unique object and associate with it.
  • When user implements Runnable, it shares the same object to multiple threads.
  • Thread Synchronization

    shape Description

    Java Multithreading, If a thread is acting on any object and preventing another thread to act upon the same object is called Thread Synchronization, it is used to remove consistency problem. Following is an example which describes the problem with synchronization and with out synchronization. withoutsynchronization.java [java]class Table{ void printTable(int n){//method not synchronized for(int i=1;i<=5;i++){ System.out.println(n*i); try{ Thread.sleep(600); }catch(Exception e){System.out.println(e);} } } } class MyThread1 extends Thread{ Table t; MyThread1(Table t){ this.t=t; } public void run(){ t.printTable(5); } } class MyThread2 extends Thread{ Table t; MyThread2(Table t){ this.t=t; } public void run(){ t.printTable(100); } } class withoutsynchronization{ public static void main(String args[]){ Table obj = new Table();//only one object MyThread1 t1=new MyThread1(obj); //created object for MyThread1 class. MyThread2 t2=new MyThread2(obj); //created object for MyThread2 class. t1.start(); //through an object thread will be started. t2.start(); } }[/java] Where developer did not use synchronized method so when compile the code result will be as follows. [java]5 100 10 200 300 15 400 20 500 25 [/java] Once place the synchronized before void printTable(int n) result will be as follows. [java]5 10 15 20 25 100 200 300 400 500 [/java]

    Priority of a Thread

    shape Description

    Priorities are spoken to by a number in the vicinity of 1 and 10. In most of the cases, thread schedular plans the threadas indicated by their priority. In any case, it is not ensured in light of the fact that it relies on upon JVM determination that which booking it picks. The following are the three priorities of thread.
    • public static int MIN_PRIORITY
    • public static int NORM_PRIORITY
    • public static int MAX_PRIORITY
    The following is an example. [java]package com; class ThreadPriority extends Thread{ public void run(){ System.out.println("running thread name is:"+Thread.currentThread().getName()); System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); } public static void main(String args[]){ ThreadPriority m1=new ThreadPriority(); ThreadPriority m2=new ThreadPriority(); m1.setPriority(Thread.MIN_PRIORITY); m2.setPriority(Thread.MAX_PRIORITY); m1.start(); m2.start(); } } [/java] Output: The result will be as follows. [java]running thread name is:Thread-0 running thread name is:Thread-1 running thread priority is:10 running thread priority is:1[/java]

    Summary

    shape Key Points

    • Daemon thread in java is a service provider thread.
    • Synchronization is used to prevent thread interference.