Wednesday, November 24, 2010

How to add all jars from a folder quickly

    jars=""
    for j in `ls $libs/*.jar`
    do
        jars=$jars:$j
    done


Use the above jars varible in the classpath.

Tuesday, November 23, 2010

How to set temporary directory for MySQL?

Source : http://maisonbisson.com/blog/post/11657/mysql-error-28-temp-tables-and-running-out-of-disk-space/

Error 28 is about disk space, usually the disk space for temp tables. The first thing to do is figure out what filesystem(s) the tables are on. SHOW VARIABLES LIKE “%dir%” will return a number of results, but the ones that matter are tmpdir and datadir.
SHOW VARIABLES LIKE “%dir%”;

basedir /
character_sets_dir /usr/share/mysql/charsets/
datadir /var/lib/mysql/
innodb_data_home_dir
innodb_log_arch_dir
innodb_log_group_home_dir ./
innodb_max_dirty_pages_pct 90
slave_load_tmpdir /tmp/
tmpdir /tmp/


As it turns out, df shows plenty of space available on all the filesystems:

# df -H

Filesystem Size Used Avail Use% Mounted on
/dev/sda3 195G 11G 175G 6% /
/dev/sda1 98M 16M 78M 17% /boot
none 1.1G 0 1.1G 0% /dev/shm
/usr/tmpDSK 508M 12M 471M 3% /tmp
/tmp 508M 12M 471M 3% /var/tmp

How to set temporary directory?

# export TMPDIR = /var/tmp/Yogesh

# /etc/init.d/mysqld restart

mysql restart is a must, because temporary directory does not gets picked up dynamically, but it is picked up during startup of mysql.

Saturday, November 20, 2010

Can singleton be broken?

Singleton pattern
The most common code for creating a singleton pattern is as follows:

Code 1:
public class SingletonSample {
   private static SingletonSample instance;
   private SingletonSample(){}
   public static SingletonSample getInstance() {
         if (instance == null)                     // 1
         {instance = new SingletonSample();}       // 2
         return instance;                          // 3
   }
}
In the above design, the constructor is declared as private and the getInstance() method ensures that only a single object is created. This program will work well for single threaded program. But however, for multithreaded programs this pattern will fail inconsistently. Let’s have a look at how and where this pattern will fail.

a) Thread 1 enters the getInstance() method and determines that instance variable is null at line // 1.
b) Thread 1 enters if block but is pre-empted by thread 2 before executing line // 2.
c) Thread 2 calls getInstance() method and determines instance variable is null at line //1.
d) Thread 2 enters the IF block and creates a new object which is assigned to instance.
e) Thread 2 then returns the Object reference at line // 3.
f) Thread 2 is pre-empted by thread 1.
g) Thread 1 starts off where it left and creates another object at line // 2 and returns the reference at // 3.

Thus two Objects of the class are created when it was required to generate only one. A “synchronized” keyword is used to make multithreaded application thread safe and hence so, if we want to use the singleton pattern in multithreaded application we must synchronized the getInstance() method as follows :

Code 2:
synchronized public static SingletonSample getInstance() {
         if (instance == null)                     // 1
         {instance = new SingletonSample();}       // 2
         return instance;                          // 3
   }
The above code works fine for multithreaded access to getInstance() method but on deeper analysis we find that the ‘synchronization’ is actually required only for the first invocation as only on the first invocation will the line // 2 be actually executed, which also happens to be the line which needs to be synchronized. All other invocation apart from the first will get a not-null value and return the reference. However, it also adds up to performance issue as the complete getInstance() method is synchronized and that any other invocation will have to wait for the lock to be released to execute the getInstance().

Hence, in order improve the performance we can wrap up the line // 2 in a synchronized block instead of the complete method as follows

Code 3:
public static SingletonSample getInstance() {
         if (instance == null)                                                        // 1
         synchronized(SingletonSample.class){instance = new SingletonSample();}       // 2
         return instance;                                                             // 3
   }
However, in the above code too, we face the same problem as we had in code 1.Two threads can enter the IF block after finding instance is null. Now, Thread 1 enters the ‘synchronized’ block and creates the Object. Thread 2 also enters the synchronized block after Thread 1 releases the lock and also creates one more object. Now, in this case the Thread 2 does not check for the instance==null and directly creates a second Object.
Double-checked locking
A way to fix the problem in Code 3 is to put one more check, a second check on instance variable as follows:

Code 4:
public static SingletonSample getInstance() {
         if (instance == null)                                                        
         synchronized(SingletonSample.class){                      // 1               
             if(instance==null)                                    // 2
                instance = new SingletonSample();                  // 3
         }       
         return instance;                                          
   }
Thus by implementing the second check on instance variable makes it impossible for creation of more than one SingletonSample objects.
Since, this design uses two checks it’s called as “Double-checked locking”. Consider the following scenario.

a) Thread 1 enters the getInstance() method and the synchronized block at line as instance is null.
b) Thread 1 is pre-empted by thread 2.
c) Thread 2 enters the getInstance() and tries to acquire lock at // 1 as instance is still null but fails because Thread 1 already has a lock on the Object.
d) Thread 2 is pre-empted by Thread 1.
e) Thread 1 then executes line //2 and line // 3 to create an Object as instance is still null at line // 2.
f) Thread 1 is pre-empted by thread 2 and acquires a lock at line // 1.
g) Thread 2 does a null check at line // 2 and after finding that it is not null exits the block without creating a new Object.

Now, finally we can conclude that we are able to generate a single instance at last. But can we really do that? Let us have a look at the code 4 line 3 again. The problem with this line is that instance can become non-null even before the constructor executes completely. Let’s look at the following flow.

a) Thread 1 enters the getInstance() and the synchronized block at line // 1 as instance is null.
b) Thread 1 executes the code at line // 3. Now after the fully creating the Object but BEFORE the initializing the object.
c) Thread 1 is pre-empted by thread 2.
d) Thread 2 checks to see if instance is null. Because it is not, thread 2 returns a reference to a fully constructed but partially initialized Object.
e) Thread 2 is pre-empted by thread 1.
f) Thread 1 completes the constructor and initializes the object and returning the reference.

Solution
This problem is because of the Java memory problem and hence the solution to the above problem is to use synchronized getInstance() as in Code // 2 or static as follows :

public static Singleton getInstance()
  {
    return instance;
  } 

Friday, November 12, 2010

Reading files made easy in Java

Courtesy : http://code.hammerpig.com/how-to-read-really-large-files-in-java.html

import java.util.*;
import java.io.*;
 
public class BigFile implements Iterable<String>
{
    private BufferedReader _reader;
 
    public BigFile(String filePath) throws Exception
    {
        _reader = new BufferedReader(new FileReader(filePath));
    }
 
    public void Close()
    {
        try
        {
            _reader.close();
        }
        catch (Exception ex) {}
    }
 
    public Iterator<String> iterator()
    {
        return new FileIterator();
    }
 
    private class FileIterator implements Iterator<String>
    {
        private String _currentLine;
 
        public boolean hasNext()
        {
            try
            {
                _currentLine = _reader.readLine();
            }
            catch (Exception ex)
            {
                _currentLine = null;
                ex.printStackTrace();
            }
 
            return _currentLine != null;
        }
 
        public String next()
        {
            return _currentLine;
        }
 
        public void remove()
        {
        }
    }
}

Here is how you might use it:

BigFile file = new BigFile("C:\\Temp\\BigFile.txt");
 
for (String line : file)
    System.out.println(line);

Thursday, November 11, 2010

How to read a file in Java

Usually such kind of function is not recommended when reading huge files. Because it is not possible for java to allocate so much contigous memory.

As far a possible, avoid using this function.

public static String getFile(String filepath) 
{
        StringBuilder output = new StringBuilder("");
        try 
        {       
            File file = new File(filepath);
            FileReader fileReader = new FileReader(file);
            BufferedReader bfr = new BufferedReader(fileReader);
            String line ;
            while((line = bfr.readLine()) != null)
            {
                output.append(line + "\n");
            } 
            bfr.close();
            fileReader.close();
        }
        catch (FileNotFoundException e) 
        {
              e.printStackTrace();
        } 
        catch (IOException e) 
        {
              e.printStackTrace();
        }   
        finally
        {
            
        }
        return output.toString();
}

Wednesday, November 10, 2010

Code that creates the code to create text

Many times it was required that I need to write a code that writes a particular text.
When such a requirement was being faced repetitively, i decided to write a small snippet of code for it.

@Action
    public void createCode() {
       
        String txt = txtarea.getText();
        String[] s2=txt.split("\\n");
        StringBuilder sb = new StringBuilder("");
        sb.append("public static String createCode()\n");
        sb.append("{\n");
        sb.append("\tStringBuilder sb=new StringBuilder(\"\");\n");
        for(String s:s2)
        {
            sb.append("\tsb.append(\"");
            sb.append(s);
            sb.append("\\n\");\n");
        }
        sb.append("\treturn sb.toString();");
        sb.append("\n}");
        code.setText(sb.toString());
    }

I have a complete application for it as well. But I do not have web-space where I can host it.
And attachments are not allowed in blog. :(