Aspect Oriented Programming

In object oriented programming, each object or class is made up of some properties and behavior. For example an Employee object might have properties like employee id, name etc and behavior like works.

so a simple Employee class would look like

public class Employee {
int id;
String name;

public Employee(int id, String name)
{
this.id=id;
this.name=name;
}

public void work()
{
System.out.println(“Employee:”+ name+” is working”);
}
}

But in real world a lot of things are happening around objects, for example lets say a bookkeeper needs to take a note of everytime an employee starts working and finishes working.

public class BookKeeper {

java.util.Date date= new java.util.Date();
/**
* This method records when employee has started working
*/
public void employeeStartsWorking()
{
System.out.println(“Employee has started working”+new Timestamp(date.getTime()));
}

/**
* This method indicates when employee has stopped working
*/
public void employeeEndsWorking()
{
System.out.println(“Employee has ended working”+new Timestamp(date.getTime()));
}

}

And my Employee class should be modified to

public class Employee {
int id;
String name;
BookKeeper bookKeeper=new BookKeeper();

public Employee(int id, String name)
{
this.id=id;
this.name=name;
}

public void work()
{
bookKeeper.employeeStartsWorking();
System.out.println(“Employee:”+ name+” is working”);
bookKeeper.employeeEndsWorking();
}
}

The challenge here is that my Employee should now be aware of BookKeeper and maintain its calls. Also it would need to actually handle error conditions (what if bookKeeper is null).

AOP or Aspect oriented programming comes to rescue here. Wikipedia states “In computing, aspect-oriented programming (AOP) is a patented[1] programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.”

Cross cutting concerns are the functionalities which are distributed accross the application but are not part of any core module. So for example, we have employee class, manager class, consultant class all implemeting work and book keeper keeoing record. So Book keeper needs to be available to all these objects, though it is not part of their core functionality. AOP recommends to move these functionality out of class and provides methods and tools for this.

Some frequently occuring cases for AOP are logging (ever class has need for logging, but it is not part of core functionality), transaction management, authentication and authrization etc.

I will talk about implementation of AOP part in next post.

Postgres error: Relation Does not exist

Faced an interesting error while accessing postgres table with a simple java code.

"select * from User"

A little googling revealed that if you are using multicase table name (U is caps), you will need to give the table name in quotes.

http://stackoverflow.com/questions/695289/cannot-simply-use-postgresql-table-name-relation-does-not-exist

A better fix would be to always use lower case characters when declaring tables in postgres.

Understanding Maven

Official definition of Maven states “Apache Maven is a software project management and comprehension tool.” For me, Maven is a build tool, that helps me manage dependencies as well. The advantage I get over Ant tool, is that it not only manages dependencies, but also downloads them automatically. Also I need not worry about transitive depeendies, that is if a jar I need for my project is inturn dependent on other jars, maven will take care of it for me.

Getting started: Using maven with Eclipse is simple. Eclipse comes with embedded maven so you need not worry about downloading or installing.

In New->Project, you can select Maven Project. It will ask you to select Archtype. An Archtype is predefine project templates. For example you can use Spring project, you can choose spring archtype. This will help you getting started by providing a sample template project, on which you can build further.

Once you create a project, you will find a pom.xml. POM stands for Project Object Model. It is fundamental Unit of Work in Maven.

A simple POM file might look like

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>TEST</groupId>
<artifactId>TEST</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Permission</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Note the dependencies part. This is telling the maven which version of the required dependency is needed by application.

ExecutorService with Callable and Future

In last post I talked about ExecutorService basics.

Let us take it forward to understand usage of Future keyword. So far we have seen Executor service usage to start executing a thread in controlled way. What if we need to get a returned object from the thread being executed. Callable interface comes to help. Instead of implmenting Runnable, we can implement Callable to create thread. In this case we will override call() method method instead of run().

From Javadocs: The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

The result of callable class is retrived in Future instance. Instead of executor.execute, we will call submit method which forces ExecutorService to return the Future instance. execute can also be used with Runnable instance, but in that case it will return null on completion as run() method cannot return an object.

Further Reading

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html

package com.kamalmeet;

import java.util.concurrent.*;

public class CallableTest {
	int num = 1;

	public static void main(String s[]) {
		ExecutorService executor = Executors.newFixedThreadPool(5);
		for (int i = 0; i < 10; i++) {
			Future<CallableTest> ft1 = executor.submit(new RunTest1());
			try {
				CallableTest ct1 = (CallableTest) ft1.get();
				System.out.println("ct1 val:" + ct1.num);
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}		}
		executor.shutdown();
	}
}

class RunTest1 implements Callable<CallableTest> {
	@Override
	public CallableTest call() throws Exception {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		CallableTest ct = new CallableTest();
		ct.num = 10;
		return ct;
	}
}

ExecutorService

Here is refresher of multithreading in java.

Java (since 1.5) has come up with more controlled and cleaner way to handle threads, i.e. ExecutorService.

ExecutorService is a way in which we can create thread pools and execute threads in controlled manner, i.e. can define number of threads to be allowed at the same time.

ExecutorService ex = Executors.newSingleThreadExecutor();
ExecutorService ex = Executors.newFixedThreadPool(5);
ExecutorService ex = Executors.newScheduledThreadPool(10);

In first case we are creating a singlethreaded pool, whereas in second case we are defining a pool of fixed length. In third case we are creating a pool of threads which can perform activities at a defined time.

Read more
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
http://tutorials.jenkov.com/java-util-concurrent/executorservice.html

A simple code example

Play around with thread pool size to understand the usage.

package com.kamalmeet;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTestExecutor {
	public static void main(String s[]) {
		ExecutorService executor = Executors.newFixedThreadPool(5);
		for (int i = 0; i < 10; i++) {
			executor.execute(new RunTest());
			executor.execute(new RunTest2());
		}
		executor.shutdown();
	}
}

class RunTest implements Runnable {
	@Override
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("run" + Thread.currentThread().getName());
	}
}

class RunTest2 implements Runnable {
	@Override
	public void run() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("run2" + Thread.currentThread().getName());
	}
}

At this point let us dig a bit into what happens behind the scenes when we initialize an ExecutorService Instance. In Executor class we have this implementation for the newFixedThreadPool method

public static ExecutorService newFixedThreadPool(int nThreads) {
   return new ThreadPoolExecutor(nThreads, nThreads, 0L,  TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}

Similar to the newFixedThreadPool, we have implementation for SingleThreadExector and CachedThreadPool

public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));
}

public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}

Let’s take a look at ThreadPoolExecutor as well

/**
     * Creates a new {@code ThreadPoolExecutor} with the given initial
     * parameters, the
     * {@linkplain Executors#defaultThreadFactory default thread factory}
     * and the {@linkplain ThreadPoolExecutor.AbortPolicy
     * default rejected execution handler}.
     *
     * <p>It may be more convenient to use one of the {@link Executors}
     * factory methods instead of this general purpose constructor.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @param maximumPoolSize the maximum number of threads to allow in the
     *        pool
     * @param keepAliveTime when the number of threads is greater than
     *        the core, this is the maximum time that excess idle threads
     *        will wait for new tasks before terminating.
     * @param unit the time unit for the {@code keepAliveTime} argument
     * @param workQueue the queue to use for holding tasks before they are
     *        executed.  This queue will hold only the {@code Runnable}
     *        tasks submitted by the {@code execute} method.
     * @throws IllegalArgumentException if one of the following holds:<br>
     *         {@code corePoolSize < 0}<br>
     *         {@code keepAliveTime < 0}<br>
     *         {@code maximumPoolSize <= 0}<br>
     *         {@code maximumPoolSize < corePoolSize}
     * @throws NullPointerException if {@code workQueue} is null
     */
    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue) {
        this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
             Executors.defaultThreadFactory(), defaultHandler);
    }

If you need additional control on the type of threads being created, you can send ThreadFactory as an additional parameter to ThreadPoolExecutor.

Before closing on the topic, it is worth looking at Atomic Variables. Atomic variables provide a way to maintain a thread-safe state for variables. This implements CAS (Compare and Swap) approach, which is faster than implementing synchronization as that needs locking and unlocking mechanism. The most commonly used atomic variable classes in Java are AtomicIntegerAtomicLongAtomicBoolean, and AtomicReference.

Kill a process in linux

There can be times that a process becomes unresposive and you want to kill it forcefully.

In linux, we will need to first find out the process id

ps command- it gives a list of all processes running.

ps aux- gives all process details with, a = show processes for all users, u = display the process’s user/owner, x = also show processes not attached to a terminal

to fetch only required process- use grep, ps aux| grep ‘name’

Then kill it forcefully- kill -9 PID

Putting it all together.

Lets say I want to kill eclipse

kamalmeet@System:~$ ps aux | grep eclipse

1101 6070 1.3 1.8 46584 5500 ? Sl 12:34 1:38 /usr/bin/java -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m -jar /home/kamalmeet/eclipse//plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar

kamalmeet@System:~$ kill -9 6070

Understanding wait, notify and notifyAll using producer-consumer problem

In last post I mentioned about some of the Java Thread’s utility methods. Here I will try to explain wait and notify concept in Threads using classic producer – consumer problem.

Wait: It tells current thread to wait till some notifies it to resume execution, myThread.wait(). There is another version of wait which takes milliseconds as input, wait(2000), it basically tells the thread to wait till someone notifies or a max of 2 seconds.

Notify: Notify statement wakes up one of the threads (randomly chosen in case multiple threads are waiting) waiting on object, to resume execution.

Notifyall: The only difference between notify and notifyAll is that later one notifies all the threads waiting on the object.

Producer- Consumer problem: This is a classic problem where producer and consumer are 2 independent entities. As the name suggests, producer is producing something which is consumed by consumer. The challenge is that one of the entities can be faster than other, for example consumer is consuming faster than producer is producing, in this case the error condition will occur when consumer has nothing to consume (as producer is slow). Vice-versa can be that producer is producing too fast for consumer to consume, so that the storage area is overflowing (as consumer is not able to consume at same speed).

To solve the problem, wait and notify operations come to rescue. So if one entity reaches error state, say consumer has run out of objects to consume, it will wait till a new object is available.

Here is an example of case two where producer is producing faster, so evetually will need to wait at a stage where queue is full till consumer has consumed objects.

The second case (consumer is faster than producer) can be simulated by reversing the sleep time.

package com.kamalmeet;

public class ProducerConsumer {
	public static void main(String s[]) {
		Queue q = new Queue();
		Producer p = new Producer(q);
		Consumer c = new Consumer(q);
		Thread t1 = new Thread(p);
		Thread t2 = new Thread(c);
		t1.start();
		t2.start();
		System.out.println("Exiting main thread now");
	}
}

class Queue {
	int arr[] = new int[10];
	int index = 0;
	final int MAX = 9;

	public synchronized int remove() {
		if (index == 0) {
			try {
				System.out.println("waiting to remove");
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		index--;
		int num = arr[index];
		notify();
		return num;
	}

	public synchronized void add(int num) {
		if (index == MAX) {
			try {
				System.out.println("waiting to add");
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		notify();
		arr[index] = num;
		index++;
	}
}

class Producer implements Runnable {
	Queue q;
	int num = 0;

	Producer(Queue q) {
		this.q = q;
	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("adding:" + num);
			q.add(num++);
		}
	}
}

class Consumer implements Runnable {
	Queue q;
	int num = 0;

	Consumer(Queue q) {
		this.q = q;
	}

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("removing:" + q.remove());
		}
	}
}

Thread Basics: Sleep, Join and Yield

When dealing with Java threads, some basic concepts can come handy in order to manage control among threads.

Sleep: A simple command which will tell the thread to go to sleep for given milliseconds. e.g. Thread.sleep(1000), tells current thread to wait for one second.

Join: This command tells program to wait till my current thread is done (killed). It has two versions. t1.join(), tells current thread to wait till thread T1 is killed before moving to next statement. A second verion takes milliseconds as input, t1.join(2000) tells current thread to wait till t1 is killed or 2 seconds which ever occurs first.

Yield: Yield is used by a thread to voluntarily give up control in favor of other threads. Though this is not guranteed- read more – http://www.javamex.com/tutorials/threads/yield.shtml

An example depicting sleep and join operations.

package com.kamalmeet;

public class TestThread {
	public static void main(String s[]) {
		RunnableTest r = new RunnableTest();
		Thread t1 = new Thread(r);
		t1.setName("t1");
		t1.start();

		Thread t2 = new Thread(r);
		t2.setName("t2");
		t2.start();

		try {
			t1.join();
			t2.join(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Exiting main thread now");
	}
}

class RunnableTest implements Runnable {
	int count = 0;
	public synchronized void test() {
		try {
			count++;
			Thread.sleep(4000); // This will just introduce some delay
			System.out.println(Thread.currentThread().getName() + " says Hello from Runnable:" + count);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public void run() {
		test();
	}
}

Understanding Thread Priority

Here is a little refresher on concept of threads in Java

http://kamalmeet.com/java/multithreading-runnable-vs-thread/
http://kamalmeet.com/java/handling-multithreading-with-synchronization/
http://kamalmeet.com/java/synchronization-object-vs-class-level/

There can be times where you might want to give priority to one thread over another. Java provides you a way to do that by setting priority of threads.

Thread.setPriority

An Example

package com.kamalmeet;
public class TestThread {
	public static void main(String s[]) {
		RunnableTest r = new RunnableTest();
		for (int i = 0; i < 5; i++) {
			Thread t = new Thread(r);
			t.setName("t:" + i);
			t.setPriority(Thread.NORM_PRIORITY + i);
			t.start();
		}
	}
}

class RunnableTest implements Runnable {
	int count = 0;
	public synchronized void test() {
		try {
			count++;
			System.out.println(Thread.currentThread().getPriority());
			Thread.sleep(1000);
			//This will just introduce some delay 
			System.out.println(Thread.currentThread().getName() + " says Hello from Runnable:" + count);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		test();
	}
}

But there is a catch. Threads are handled at the operating system level, so Java is depending on OS to make sure thread priority is taken care of. Here is a good explanation of thread priority in Java being handled in different OS – http://www.javamex.com/tutorials/threads/priority_what.shtml