Wednesday, October 10, 2012

Java Concurrency / Multithreading: Part 3

In Previous post (Java Concurrency / Multithreading :Part 2) learn how to initialize and use ExecutorService.

There are numerous problem domains where while designing the application you will need to implement the thread pool and each thread after completing the task, based on inputs should return a value. Futures and Callables exist exactly to solve this area.

Till now we have implemented the thread pool (in previous post) using the Runnables which do not return any value. In case a thread should return a value based on computation or work done as part of task you can use java.util.concurremt.Callable. Callable return value after execution. Callable returns an object of java.util.concurrent.Future. Future is used to check the status of task and return some value. Below is the example to use Callable and return a value

public class MyRunnable implements Callable<Long> {  
     @Override  
     public Long call() throw Exception {  
       return 5L;  
     }  
   } 

Using the Executor Framework

public class Example {

    public static void main(String[] args) {



        ExecutorService executor = Executors.newFixedThreadPool(5);

        List<Future<Long>> fList = new ArrayList<Future<Long>>();

        for (int i = 0; i < 999; i++) {

            Callable<Long> worker = new MyRunnable();

            Future<Long> submit = executor.submit(worker);

            fList.add(submit);

        }

        long sum = 0;



        for (Future<Long> future : fList) {

            try {

            sum += future.get();

            } catch (InterruptedException e) {

            e.printStackTrace();

            } catch (ExecutionException e) {

            e.printStackTrace();

            }

        }

        System.out.println("Final Sum is : " + sum);

        executor.shutdown();

    }

} 

Java Concurrency / Multithreading: Part 2

See my earlier post (Java Concurrency / Multithreading :Part 1) related to basics about java threads and concurrency.

Executor Framework

ThreadPools is the way to manage the pool of working threads and limit the creation of thread as creating a thread have resource overhead associates with it. Thus thread pools contains a work queue waiting for their turn to get executed.

In thread pool, the thread are constantly running and checking the queue for new work. As soon as the pool have a idle thread the work is assigned to one of the threads.

With Java 5 came Executor Framework under java.util.concurrent package, so that you do not need to create and manage your own pool e.g java.util.concurrent.Executor can be initialized as Executors.newfixedThreadPool(n) to create n worker threads. 

Like wise Executors.newSingleThreadExecutor(); will create a single thread pool with only one worker thread.

Example:
Creating a Runnable class
public class MyRunnable implements Runnable {
    private final long count;
    MyRunnable(long count) {
        this.count = count;
    }
    @Override
    public void run() {
        long mySum = 0;
        for (long i = 1; i < count; i++) {
            mySum = mySum + i;
        }
        System.out.println(mySum);
    }
}

Using the Executor Framework
public class Example {

    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(5);

        for (int i = 0; i < 100; i++) {

            Runnable myworker = new MyRunnable(99999999L + i);

            executor.execute(myworker);

        }

        executor.shutdown();

        while (!executor.isTerminated()) {



        }

        System.out.println("Completed processing the work queue");

    }

} 


ExecutorService executor = Executors.newFixedThreadPool(5); creates a new pool of 5 worker threads.

executor.execute(myworker); adds new work in the queue for thread pool to complete.

executor.shutdown(); waits till all the work in the queue is completed. After calling this method the ExecutorService will not take any new task but once all the current tasks in the queue are complete the ExecutorService will shutdown.

To stop the ExecutorService immediately call shutdownNow() method.This will attempt to stop all executing tasks right away, and skips all submitted but non-processed tasks. There are no guarantees given about the executing tasks. Perhaps they stop, perhaps the execute until the end. It is a best effort attempt.

 executor.isTerminated() checks if the service is terminated or not.

Till now we have submitted the tasks to ExecutorService and never waited to get a return value after . Thus in case the threads should return some value then you should use java.util.concurrent.Callable. See my next post (Java Concurrency / Multithreading: Part 3) summarizing the above concept.

Tuesday, October 09, 2012

Java Concurrency / Multithreading :Part 1

Goal:

How to execute the tasks in background using Java concurrent programming. It also covers the concepts of threads, parallel programming, java.util.concurrent.Executor, java.util.concurrent.Future, java.util.concurrent.Callable framework.

Details:

What is Concurrency?

In computer world, concurrency means executing several programs simultaneously or in parallel and these programs may or may not interact with each other. Running several programs in parallel or asynchronously can add to over all performance of the task.

Process vs Threads

Both thread and process are methods of parallelizing the application. Below are the key points highlighting the differences between Process and Thread.

Process: are independent execution units. Applications are typically divided into various processes during the design phase. It cannot directly access shared data in other processes.However this is done by inter-process mechanism managed by operating system. A process might contain multiple threads.

Thread: are light weight processes and is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread. . Every thread have its own memory cache.

Why Concurrency

  •  Efficient utilization of resources
  • Increased application throughput
  • High responsiveness
  •  Programs becomes simpler: as some of the problem domains are well suited to be represented as concurrent tasks

Disadvantages

  • With increased complexity the program design becomes more complex
  • Context Switching between threads, too many open threads will lead to context switching overheads causing the performance degrade

 Threads in Action

Threads are the core of all concurrent or parallel programing. There are 2 ways to implement thread in java.
The First one is extending java.lang.Thread class. Below is the way to create a thread:
Thread myThread =  new Thread();
Now after initialization , to start thread
myThread.start();

Create a subclass of Thread
public class CustomThread extends Thread{

 public void run(){
 System.out.println("Hello Thread");
}
}

Using the above class
CustomThread thread1 = new CustomThread();

thread1.start()
The second way is to implement the java.lang.Runnable interface.
public class MyRunnable implements Runnablee{
public void run (){

System.out.println("Hello Runnable"); 

} 

}
Using the above class
Thread thread1 =  new Thread(new MyRunnable);

thread1.start();
Next>>> Java Concurrency / Multithreading: Part 2


Linux/Unix: Handy Commands

To change the timezone on linux server use the below command


ln -sf /usr/share/zoneinfo/America/New_York  /etc/localtime

 To create an archive

tar -czf my_archive.tar.gz /usr/local/jeff/docs

To change or update the date

date -s "09 OCT 2012 12:19:00"

MySQL: Backup and Restore

In the past I have always tried to take database backup and followed by restoration process using the various client tools like SQLYog or HeidiSQL. As I have worked on community version of these tools thus always faced restrictions or got errors while working with larger database.

With experience I switched to MYSQL in built library function which came to my rescue and never let me down: Below are the details for the same:

For Windows:

  • Open a command prompt DOS window by typing in "cmd" in Start>>Run Menu
  • Now navigate to the bin directory of the MySQL installation say
    cd c:/Program Files/MySQL/MySQL Server 5.0/bin
  • For backup
    mysqldump.exe -u root -pabc123 se_alfresco > d:\backup.sql
  • For restore
    mysql.exe -u root -pabc123 se_alfresco < d:\backup.sql

For Linux/Unix:

  • Go to linux shell prompt
  • Use the below command for back up and restoration
  • For backup
    mysqldump -u root -pabc123 se_alfresco > d:\backup.sql
  • For restore
    mysql -u root -pabc123 se_alfresco < d:\backup.sql

MySQL: Grant permission to a user

Create a new user

 CREATE USER 'c'@'localhost' IDENTIFIED BY 'mypass';

Grant all permission on all database

grant all privileges on *.* to 'v'@'localhost' identified by 'mypass' with grant option;

Grant all permission on a database


grant all privileges on appDB.* to 'v'@'localhost' identified by 'mypass' with grant option;

References:

For complete details see MySQL Manual.


Wednesday, October 03, 2012

Why C3P0 over Commons DBCP?

What is Connection pool?
Connection pool is good for performance, as it prevents Java application create a connection each time when interact with database and minimizes the cost of opening and closing connections. The purpose is to reuse the connections and manage the process of opening, closing efficiently.

The shortcomings/disadvantages of Commons DBCP are very clearly documented over Tomcat 7.0 JDBC connection pool  documentation.

  1. commons-dbcp is single threaded, in order to be thread safe commons-dbcp locks the entire pool, even during query validation.
  2. commons-dbcp is slow - as the number of logical CPUs grow, the performance suffers, the above point shows that there is not support for high concurrency Even with the enormous optimizations of the synchronized statement in Java 6, commons-dbcp still suffers in speed and concurrency.
  3. commons-dbcp is complex, over 60 classes. tomcat-jdbc-pool, core is 8 classes, hence modifications for future requirement will require much less changes. This is all you need to run the connection pool itself, the rest is gravy.
  4. commons-dbcp uses static interfaces. This means you can't compile it with JDK 1.6, or if you run on JDK 1.6/1.7 you will get NoSuchMethodException for all the methods not implemented, even if the driver supports it.
  5. The commons-dbcp has become fairly stagnant. Sparse updates, releases, and new feature support.
  6. It's not worth rewriting over 60 classes, when something as a connection pool can be accomplished with as a much simpler implementation.
  7. Tomcat jdbc pool implements a fairness option not available in commons-dbcp and still performs faster than commons-dbcp
  8. Tomcat jdbc pool implements the ability retrieve a connection asynchronously, without adding additional threads to the library itself
  9. Tomcat jdbc pool is a Tomcat module, it depends on Tomcat JULI, a simplified logging framework used in Tomcat.
  10. Retrieve the underlying connection using the javax.sql.PooledConnection interface.
  11. Starvation proof. If a pool is empty, and threads are waiting for a connection, when a connection is returned, the pool will awake the correct thread waiting. Most pools will simply starve.


See my other post (Tomcat 7: C3P0 Datasource Pool) summarizing how to Configure C3P0 in Tomcat 7.

Tomcat 7: C3P0 Datasource Pool

Goal:

To configure C3P0 datasource pool in tomcat 7

Detail: 

Till now I was using DBCP JDBC connection pooling for my applications. Considering that the library seems to be out of date now and is not meant for production grade system with heavy load on them, I looked towards the other free open source connection pooling library C3P0.

C3P0 is an easy-to-use library for making traditional JDBC drivers "enterprise-ready" by augmenting them with functionality defined by the jdbc3 spec and the optional extensions to jdbc2.
Below are the steps to configure C3P0 pool in tomcat. 
  1. Download the latest c3p0-{version}.jar from http://sourceforge.net/projects/c3p0/.
  2. Copy the c3p0-{version}.jar to tomcat/lib directory.
  3. Open the tomcat/conf/context.xml in edit mode and add the below lines inside <Context> element.
    <Resource auth="Container" description="DB Connection" driverClass="com.mysql.jdbc.Driver" maxPoolSize="50" minPoolSize="10" acquireIncrement="10" name="jdbc/MyDBPool" user="root" password="abc123" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:mysql://localhost:3306/lportal?autoReconnect=true" />
     
  4.  For  more on configuring various attributes of the connection pool, see http://www.mchange.com/projects/c3p0/#configuration

Environment:

Java 6
Tomcat 7
MySQL 5

Reference:

See my other post (Why C3P0 over Commons DBCP?) for more on DBCP vs C3P0 

Monday, October 01, 2012

Liferay 6.1: Documents And Media CMIS: Install & Configure Xuggler

Goal: 


Installation and Setup:

  • Install Xuggler 3.4 from http://www.xuggle.com/xuggler/downloads/
  • Configure Xuggler environment variables in setenv.sh
    export XUGGLE_HOME=/usr/local/xuggler
    export LD_LIBRARY_PATH=$XUGGLE_HOME/lib:$LD_LIBRARY_PATH
    export PATH=$XUGGLE_HOME/bin:$PATH
  • Start the tomcat
  • Go to Control Panel -> Server Administration->External Services. Select the version of Xuggler binary library compatible with your environment and follow the instructions.
  • You will need to restart the tomcat after above step as per the installation process.
  • To Enable Xuggler in portal
    Control Panel -> Server Administration->External Services -> Enable Xuggler
  • Now you are ready to play audio/video files uploaded in Documents And Media portlet.

Environment:

  • Linux 64 bit (libc version 6)
  • Liferay 6.1 GA2
  •  Tomcat 7
  • Java 1.6.0.20

Reference :