Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java Thread Example

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 1.22k
    Comment on it

    Thread: Thread is a lightweight process. Thread requires less memory space and resources to create and exists in the process, thread shares data and resources of the process.

    Every Java application has at least one Thread (main thread). There are some threads running in the background by default like garbage collector, memory management etc. But as a application's point of view we can create more then one threads as per our requirement.

    Java provides two ways to create a thread programmatically.

    1. Implementing the java.lang.Runnable interface.
    2. Extending the java.lang.Thread class.

    Example of creating thread by implementing Runnable interface:

    RunnableThreadExample.java

    package com.evon.threads; 
    
    public class RunnableThreadExample implements Runnable { 
    
        @Override 
        public void run() { 
            System.out.println("Runnable Thread processing - START "+Thread.currentThread().getName()); 
            try { 
                Thread.sleep(1000); 
                //Get database connection, delete unused data from DB 
                doDBProcessing(); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println(" Runnable Thread processing - END "+Thread.currentThread().getName()); 
        } 
    
        private void doDBProcessing() throws InterruptedException { 
            Thread.sleep(5000); 
        }  
    }
    

    Example of creating thread by extending Thread class :

    ThreadExample2.java

    package com.evon.threads; 
    
    public class ThreadExample2 extends Thread { 
    
        public ThreadExample(String name) { 
            super(name); 
        } 
    
        @Override 
        public void run() { 
            System.out.println(" Thread Example 2 - START "+Thread.currentThread().getName()); 
            try { 
                Thread.sleep(1000); 
                //Get database connection, delete unused data from DB 
                doDBProcessing(); 
            } catch (InterruptedException e) { 
                e.printStackTrace(); 
            } 
            System.out.println(" Thread Example 2 - END "+Thread.currentThread().getName()); 
        } 
    
        private void doDBProcessing() throws InterruptedException { 
            Thread.sleep(5000); 
        }      
    }
    

    MainThread.java

    package com.evon.threads; 
    
    public class MainThread { 
    
        public static void main(String[] args){ 
            Thread t1 = new Thread(new RunnableThreadExample(), "Thread1"); 
            Thread t2 = new Thread(new RunnableThreadExample(), "Thread2"); 
            System.out.println("Starting Runnable threads"); 
            t1.start(); 
            t2.start(); 
            System.out.println("Runnable Threads has been started"); 
            Thread t3 = new ThreadExample2("Thread3"); 
            Thread t4 = new ThreadExample2("Thread4"); 
            System.out.println("Starting ThreadExample2"); 
            t3.start(); 
            t4.start(); 
            System.out.println("ThreadExample2 has been started"); 
         } 
    }
    

    Output :

    Starting Runnable threads 
    Runnable Threads has been started 
    Runnable Thread processing - START Thread1 
    Runnable Thread processing - START Thread2 
    Starting ThreadExample2 
    Thread Example 2- START Thread3 
    ThreadExample2 has been started 
    Thread Example 2 - START Thread4 
    Runnable Thread processing - END Thread2 
    Thread Example 2 - END Thread3 
    Thread Example 2 - END Thread4 
    Runnable Thread processing - END  Thread1
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: