Choose Your Language

Saturday 29 March 2014

When finally block is NOT called?

Finally is the block of code that executes always. The code in finally block will execute even if an exception is occurred. Finally block is NOT called in following conditions

1.public class TryCatchFinally {
    public static void main(String ar[]){
        try{
               System.out.println("try block");
              }
        catch(Exception e){
                System.out.println("catch block");
                System.exit(0);
        }finally{
            int x=0;
            if(x>1){
            System.out.println("finally block");
            }
        }
    }
}
output:
try block

2. public class TryCatchFinally {
    public static void main(String ar[]){
        try{
           System.out.println("try block");     
           System.exit(0);
        }catch(Exception e){
                System.out.println("catch block");
                System.exit(0);
        }finally{          
            System.out.println("finally block");          
        }
    }
}
output:
try block
3. public class TryCatchFinally {
    public static void main(String ar[]){
        try{
            int x=0;
            if(x<1){
                    throw new Exception();
                }     
             
        }catch(Exception e){
                System.out.println("catch block");
                System.exit(0);
        }finally{
            int x=0;
            if(x>1){
            System.out.println("finally block");
            }
        }
    }
}
output:
catch block

4. public class TryCatchFinally {
    public static void main(String ar[]){
        try{
            int x=0;
            if(x<1){
            throw new Exception();
            }
        }catch(Exception e){
                System.out.println("catch block");
                System.exit(0);
        }finally{          
            System.out.println("finally block");          
        }
    }
}
output:
catch block


Thursday 27 March 2014

garbage collection in java

public class GarbageCollectionSample {
    public static void main(String ar[]){      
        GarbageCollection garbageCollection=new GarbageCollection("BLACK");
        System.out.println(garbageCollection.displayColor());
        garbageCollection.finalize();
        System.out.println(garbageCollection.displayColor());    
    }
}
class GarbageCollection {
private String color;

    public GarbageCollection(String color) {
        this.color = color;
    }
        public String displayColor(){   
         return color;
        }
    @Override
    public void finalize() {
            this.color="No Color";     
    }  
}

output:
BLACK
No Color

Monday 17 March 2014

Run one thread after another in java

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Aravind Sankaran
 */
public class OneThreadRunAfterAnother {
    public static void main(String aravind[]){
        Thread one=new Thread(new Runnable() {

            @Override
            public void run() {             
                for(int x=0;x<=5;x++){
                System.out.println("Aravind");
                }
                System.out.println("");
            }
        });
        one.start();
        Thread two=new SecondThread(one);
        two.start();
    }
}

class SecondThread extends Thread{
    Thread predecessor;
    public SecondThread(Thread predecessor) {
        this.predecessor=predecessor;       
    }
   public void run(){
        if(predecessor!=null&&predecessor.isAlive()){
            try {
                predecessor.join();
                for(int x=0;x<=5;x++){
                System.out.println("Sankaran");
                 }
            } catch (InterruptedException ex) {
             
            }
        }else{
           
        }
    }
}

output:
Aravind
Aravind
Aravind
Aravind
Aravind
Aravind

Sankaran
Sankaran
Sankaran
Sankaran
Sankaran
Sankaran

Traffic Light Control System