Saturday, September 21

Adding a post to blogger using Google API

                                     Adding a post to blogger using Google API






 How to create a post on blogger using Google API.

Google API jar file required : gdata-core1.0.jar  . Download this jar and add to the class library.





-->

A sample Code here...

import com.google.gdata.client.GoogleService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.util.ServiceException;

import java.io.IOException;
import java.net.URL;

public class Blogger{

    public static void createPost(GoogleService myService) throws ServiceException, IOException {

         // Create the entry to insert
        Entry myEntry = new Entry();


// POST Title
        myEntry.setTitle(new PlainTextConstruct("INDIA DEFEATED PAKISTAN "));


// Post description
        myEntry.setContent(new PlainTextConstruct("INDIA Defeated pakistan in only 4 seconds . Hindustan Jindabad "));




// Blogger URL


        URL postUrl = new URL("http://allindiatelevision.blogspot.in");
 
         myService.insert(postUrl, myEntry);

    }

    public static void main(String ar[]) throws ServiceException, IOException {


//creating Google service required to access and update Blogger post
        GoogleService myService = new GoogleService("blogger", "http://allindiatelevision.blogspot.in/");
        myService.setUserCredentials("gmail username", "password");

        createPost(myService );
    }

}






Method Overriding VS Method Hiding in java





                                  Method Overriding VS Method Hiding in java



If and instance  method in a subclass has same signature  and return type as instance method in the superclass ,it is called overriding.Method signature means name, and the number and the type of its parameters. Number ,type of the parameters should be same in method written in super class and sub class.

 overriding method can also return a subtype of the type returned by the super class method. This is called a co-variant return type.
Foe example

public class Animal {
  
    public void test() {
       
 }
}




public class Dog extends Animal {
   
    public void test() {
    }

}
test() method in Dog class is overridden .

If a  class method in subclass has the same signature as a class method in the superclass, This is called method hiding.

For example

public class Animal {
    public static void test() {
       ;
    }
  
}


public class Dog extends Animal {
    public static void test() {
      
    }
   }
here test() method in Dog class hides test() method of Animal superclass




In case of overriding method defined in subclass is invoked If object of subclass is created and assigned to a reference of superclass or subclass .i.e. if instance of Dog class is assigned to a reference of Dog class or animal class and test() method is invoked ,method written in Dog class will be invoked









 The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass. I.e. If reference is of Animal class animal class test() method would be invoked but if reference is of Dog class , Dog class test() method would be invoked.

Lets take another example :

public class Animal {
    public static void testClass() {
        System.out.println("Animal---> Class Method.");
    }
    public void testInstance() {
        System.out.println("Animal---> Instance method.");
    }
}



public class Dog extends Animal {
    public static void testClass() {
        System.out.println("Dog------> class metohd");
    }
    public void testInstance() {
        System.out.println("Dog---->Instance method.");
    }

    public static void main(String[] args) {
        Dog myCat = new Dog();
        Animal animal = lazyDog;
        Animal.testClassMethod();
        animal.testInstanceMethod();
    }
}

The Dog class overrides the instance method in Animal and hides the class method in Animal. The main method in this class creates an instance of Dog and calls testClass() on the class and testInstance() on the instance.

The output from this program is as follows:

Animal---> Class Method
Dog---->Instance method.






Conclusion
So class method invocation depends upon the reference type while instance method invocation depends upon the type of instance created. Method hiding happens for static methods and method overriding for instance methods


-->

Friday, September 20

Java concurrency interrupt method theory , wating thread and interruptException

             Java concurrency interrupt method theory ,  wating thread and interruptException


java Thread interrupt method

one important thing to know about this method  :

This method throws interruption exception only for waiting thread .

If thread is not in waiting status thread's interrupted status will be set to true but it won't stop it's execution there instead thread will continue running normally

For example

class MyThread extends Thread {


public void run(){

try{
Thread.currentThread().sleep(100);

for (int i=0;i<100 br="" i="">System.out.println(i);
}
}catch(InterruptedException exp){
    System.out.println(1);
exp.getMessage();
}
 }

}

public class Test{

public static void main(String args[]){

Thread t=new MyThread();

t.start();
t.interrupt();

}
}
In above example when t.interrupt() method is invoked Mythread thread is in sleeping i.e. waiting status . So it would be interrupted and for loop won't be executed but execution control flow will land up directly in catch block .








Now let's change this scenario a bit


class MyThread extends Thread {


public void run(){

try{
Thread.currentThread().sleep(101);

for (int i=0;i<100 br="" i="">System.out.println(i);
}
}catch(InterruptedException exp){
    System.out.println(1);
exp.getMessage();
}
 }

}

public class Test{

public static void main(String args[]){

Thread t=new MyThread();

t.start();
try {
    Thread.sleep(101);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
t.interrupt();

}


Here after starting the thread , t.interrupt() is not invoked immediately but after waiting for 101 ms . So , here when t.interrupt() in invoked MyThread thread has already come out of sleeping status. So it won't be interrupted and for loop will be executed normally without any exception.


Thus interrupt() method will cause interruptException only when invoked thread is in waiting status .

 

What are the different way , a thread can go in waiting status is listed here


-->




java.lang.Thread.interrupted() method oncoked twice ..

                            java.lang.Thread.interrupted() method oncoked twice ..




If java.lang.Thread.interrupted() method is invoked twice , In second execution it will always return true.

Why?

Thread.interrupted() method does two things

1. returns the current status
2. clears the interrupted flag . i.e. set the interrupted flag of thread to false.


So at the time of first execution what ever might be the status of interruption flag of thread , it will follow above tow steps

1. return current status (true or false i.e. interrupted or not-interrupted)
2. clears the interrupted flag . i.e. set the interrupted flag of thread to fa
lse.

And at the time of second execution , as status after first execution is already set to false So

1. return current status (false)
2. clears the interrupted flag . i.e. set the interrupted flag of thread to false.

Thus is case of two successive execution of Thread.interrupted() method second execution will always return false.


So What is the purpose of Thread.interrupted() method at all in java




Sole purpose is to know the current interruption status of thread and also clearing the interruption status.







methods that take a thread to waiting status

                                              methods that take a thread to waiting status

-->





Methods in java concurrency when threads come in waiting status


  1. Thread.sleep() 
  2. BlockingQueue.get()
  3. Semaphore.acquire();
  4. wait();
  5. join();


-->








-->