• Breaking News

    Saturday 3 October 2015

    Implementing Thread In Java Using Runnable Interface

    Before understand thread we considering real time scenario :-

    Suppose you decided to eat food in restaurant. In restaurant only one waiter and one other customer, waiter is serve only that customer and ignored you.

    When that customer leave restaurant then waiter start serve you. So what is happening here waiter is not able to do multitask.

    Multitasking is that waiter server more than one customer at a time instead of serving one customer at a time.

    Now consider waiter is our processor, customer is our task each task create it own thread and our multitask processor execute more than one thread at time.

    In java we create thread by two ways

    1. Implementing Runnable Interface
    2. Extend Thread Class

    Developer considering first method because if i want to extend another class then it is not possible with Second  method because java does not support multiple inheritance i.e we cannot extend more then one class at a time.

    In This tutorial We considering only Runnable Interface Example

    First we create ImplementRunnableThread class object we can say that is task

    Runnable  firstCustomer = new ImplementRunnableThread("Waiter Serve First Customer",  40);

    Runnable secondCustomer = new ImplementRunnableThread("Waiter Serve Second Customer", 60);

    Now We create task thread

    Thread t1 = new Thread(firstCustomer);

    Thread t1 = new Thread(secondCustomer);

    Then Start thread means waiter start serve customers

    t1.start();

    t2.start();

    which call run method to perform task.


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    package com.kodemaker;
    
    class ImplementRunnableThread implements Runnable {
     
        private String whichCustomerIsCurrentlyServed;    
        private int timeTakenByWaiterToServeOtherCustomer;      
    
        
        public ImplementRunnableThread(String whichCustomerIsCurrentlyServed, int timeTakenByWaiterToServe) {
         this.whichCustomerIsCurrentlyServed = whichCustomerIsCurrentlyServed;
            this.timeTakenByWaiterToServeOtherCustomer = timeTakenByWaiterToServe;
        }
        
    
        public void run() {
            try {
                for (;;) {
                    System.out.println(this.whichCustomerIsCurrentlyServed);
                    //wait untill next customer is served 
                    Thread.sleep(this.timeTakenByWaiterToServeOtherCustomer); 
                }
            } catch (InterruptedException e) {
                return;          
            }
        }
        
    
        public static void main(String[] args) {
         
            Runnable firstCustomer = new ImplementRunnableThread("Waiter Serve First Customer",  40);
            Runnable secondCustomer = new ImplementRunnableThread("Waiter Serve Second Customer", 60);
            
            //first customer arrive
            Thread t1 = new Thread(firstCustomer);
            // waiter start serving first customer
            t1.start();
            
            //Second customer arrive
            Thread t2 = new Thread(secondCustomer);
            // waiter start serving Second customer 
            t2.start();
        }
        
    }
    

    Output of above programme:-


    Waiter Serve First Customer
    Waiter Serve Second Customer
    Waiter Serve First Customer
    Waiter Serve First Customer
    Waiter Serve Second Customer
    Waiter Serve First Customer
    Waiter Serve Second Customer
    Waiter Serve First Customer
    Waiter Serve First Customer
    

    By Output we can see serving to customer is asynchronous  which we will discuss in next tutorial how to make thread synchronous.

    No comments:

    Post a Comment