Wednesday, 30 September 2015

Java Daemon Thread

package com.JavaInterview.Programs;
/**
 * *You can make any java thread as daemon thread. Daemon threads acts like service providers
 *  for other threads running in the same process.Daemon threads will be terminated by the 
 *  JVM when there are none of the other threads running, it includes main thread of execution 
 *  as well.
 * 
 */
public class DaemonThread extends Thread{
    
   public DaemonThread(){
       setDaemon(true);
   }
   public void run(){
       System.out.println("Is this thread Daemon? - "+isDaemon());
   }
   public static void main(String a[]){
       DaemonThread dt = new DaemonThread();
       // you can set daemon constrain here also
       // it is like dt.setDeamon(true)
       dt.start();
   }
}

Output:
Is this thread Daemon? - true


Java Thread :Extending Thread Class

package com.JavaInterview.Programs;

/* 
** A thread can be created in java by extending Thread class, where you must override run() method.
* Call start() method to start executing the thread object.
*/
public class ThreadExtendJIP extends Thread {
  
    public ThreadExtendJIP(String name) {
        super(name);
    }

    @Override
    public void run() {
        System.out.println("MyThread - 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("MyThread - END "+Thread.currentThread().getName());
    }

    private void doDBProcessing() throws InterruptedException {
        Thread.sleep(5000);
    }
     
}
 class ThreadRunExample {
  
    public static void main(String[] args){
        Thread t1 = new ThreadExtendJIP("t1");
        Thread t2 = new ThreadExtendJIP("t2");
        System.out.println("Starting MyThreads");
        t1.start();
        t2.start();
        System.out.println("MyThreads has been started");
         
    }
}

Output:
Starting MyThreads
MyThreads has been started
MyThread - START t1
MyThread - START t2
MyThread - END t2
MyThread - END t1


Java Thread :Implementing Runnable Interface

package com.JavaInterview.Programs;

/**
 * *A Thread can be created by extending Thread class also. But Java allows only one class to extend,
 *   it wont allow multiple inheritance. So it is always better to create a thread by implementing 
 *  Runnable interface. Java allows you to implement multiple interfaces at a time.
 * * By implementing Runnable interface, you need to provide implementation for run() method.
 * *To run this implementation class, create a Thread object, pass Runnable implementation class        *   object to its constructor. Call start() method on thread class to start executing run() method.
 * 
 * 
 */
public class RunnableThreadJIP implements Runnable{


    public static int myCount = 0;
    public RunnableThreadJIP(){
         
    }
    public void run() {
        while(RunnableThreadJIP.myCount <= 10){
            try{
                System.out.println("Expl Thread: "+(++RunnableThreadJIP.myCount));
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                System.out.println("Exception in thread: "+ex.getMessage());
            }
        }
    }
}

class MyThread {
    public static void main(String a[]){
        System.out.println("Starting Main Thread...");
        RunnableThreadJIP rt = new RunnableThreadJIP();
        Thread t = new Thread(rt);
        t.start();
        while(RunnableThreadJIP.myCount <= 10){
            try{
                System.out.println("Main Thread: "+(++RunnableThreadJIP.myCount));
                Thread.sleep(100);
            } catch (InterruptedException ex){
                System.out.println("Exception in main thread: "+ex.getMessage());
            }
        }
        System.out.println("End of Main Thread...");
    }
}

Output:
Starting Main Thread...
Main Thread: 1
Expl Thread: 2
Main Thread: 3
Expl Thread: 4
Main Thread: 5
Expl Thread: 6
Main Thread: 7
Expl Thread: 8
Main Thread: 9
Expl Thread: 10
Main Thread: 11
End of Main Thread...


Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...