Category Archives: Core Java

How to improve java performance

Hi All,

When ever we dealing with huge amount of data,we always find difficult achieving good performance.I have faced similar issue. To solve i followed simple below techniques which helped me to see good improvement in java performance

1) First and most important is writing efficient code.

 What is efficient code means ?

Avoid unnecessary iterations which can be avoided by writing conditional blocks.Make sure it is really needed to execute that loop all the time or only for special cases.

Use break; statement when it is necessary.When ever in loop if you are looking for particular element then break the loop once you found the element.

Do not extract same element twice.For example you are looking for particular element in loop and you found it then try to pass it all the method where ever it is needed,do not extract it again.

2) Use frameworks, third party jars.

Example:Spring frame works take care life cycle of beans,it will load beans when container loads which will make application light weight.

Proved third party jars will already have well tested and efficient code which will save your time as well as it improve performance,but make sure using third party library is really needed.

3) Avoid unnecessary logging which means writing log statement in loop,duplicate logs

4) Always measure response time,which helps you to write efficient code.

5) Do not hit data base several times for single action,Which can avoided by writing                    DB Stored procedure,functions.

Example:If user action has end up having insert/update and data retrieval then pass all data as input parameters to DB procedure perform insert/update and execute select query pass back result set as out parameter from DB procedure.

6) Always tune DB queries,which means use joins carefully when ever data is high.Use DB        indexes and use indexed columns for joins.Avoid outer joins.

7) Mostly importantly for Data retrieval if you are using JDBC then set fetch size.

By default, when Oracle JDBC executes a query, it receives the result set 10 rows at a time from the database cursor. This is the default Oracle row-prefetch value.  You can change the number of rows retrieved with each trip to the database cursor by changing the row-prefetch value.

JDBC Example:

Connection connection = DriverManager.getConnection("");
Statement statement = connection.createStatement();
statement.setFetchSize(1000); // configure the fetch size
ResultSet resultSet = statement.executeQuery("");
if you are using Spring JDBC then
simpleJdbcTemplate.setFetchSize(1000); 

Hope these points helps making difference:)

Java Custom Annotation Example

Hi Readers,

in this post i will explain how custom annotation works.

  1. Annotation works like metadata.
  2. @ means compiler treats it as annotation.
  3. @interface means compiler treats as custom annotation.
  4. @Retention:Retention is used specify annotation if it should be available at runtime.
  5. @Target : We can specify which Java elements your custom annotation can be used to annotate.  In the below example MyFirstAnnotation targeted to use at method level.
package com.myjavarecipes.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //can use in method only.
public @interface MyFirstAnnotation {
String value();

}

Example to show how to use it.

package com.myjavarecipes.examples;

import java.lang.reflect.Method;

import com.myjavarecipes.annotations.MyFirstAnnotation;

public class AnnotationExample{

@MyFirstAnnotation(value="DOACTION")
public void doBatting(){
System.out.println("Batting performed");
}
public void doBowling(){
System.out.println("Bowling performed");
}
public static void main(String[] args) throws Exception {
{
AnnotationExample anExample=new AnnotationExample();
Method[] methodsArray=anExample.getClass().getMethods();
for(int i=0;i<methodsArray.length;i++){
Method method=methodsArray[i];
if(method.isAnnotationPresent(MyFirstAnnotation.class)){
MyFirstAnnotation myAnnObject=(method.getAnnotation(MyFirstAnnotation.class));
System.out.println("My annotation value:"+myAnnObject.value());
method.invoke(anExample, null);
}
}
}
}
}

OutPut:
My annotation value:DOACTION
Batting performed