Archive

Posts Tagged ‘how’

Deadlock detection through YourKit Java Profiler

March 15th, 2012 39 views No comments

Some days ago I wrote an article How Nested Monitor can cause DeadLock and made a tracer to know what i going on in my program.

This was a simple lite-weighted tracer who monitors deadlock and exit from the program if any deadlock detected. It also prints various states of a thread. But since it is again a thread so it prints states whenever it gets proceed by CPU. Hence you miss all states of thread. YourKit Jprofiler solved this problem.

I commented deadlock detection code from tracer and run program through YourKit Profiler. Now see how this tool informed me about dead lock and various states of all running threads & CPU.

YourKit thread states

As you can see in the last, both threads A & B are blocked. YourKit popped up a deadlock message and gave me following information about deadlock.

YourKit deadlock popup

YourKit deadlock popup

It is clearly mentioned in above screen shot that which thread waiting for a lock locked by another thread. It’ll help you to debug your code. Above 2 lines explains, that thread A is taking a lock on some Integer object while thread B is taking lock on DeadLockDemo object. I’ll suggest you to override toString() of class you are locking on. It’l help you to find out exact position where a thread is locking on.

Note:

  • If you are running your program through YourKit, it takes more CPU.
  • YourKit detects deadlock after a while than Tracer. If tracer detects deadlock and exit from the program, yourkit will never know whether deadlock was occurred.


YourKit CPU time & thread overview

If you go through CPU stats, you’ll know that Thread.activeCount() & Thread.getState() takes most CPU. These method are called by tracer to know current state of thread and print it.

CPU stats

Second cycle

I dint call tracer this time and observed that CPU consumption clashed down. Dadlock was still there in the program.

CPU time and thread overview chart

Now you will not notice any Hot Spot. See how CPU consumption slashed down.

CPU time in second cycle

And here how many threads are running after skipping tracer.

Threads runninng in second cycle

Threads runninng in second cycle

A good condition can let other threads go ahead

March 11th, 2012 13 views No comments

Java 5 provides Lock over synchronized and handed over locking to programmer hands. But it is incomplete without Condition.

Condition

condition (since Java 5) is nothing but labeled wait(), notify() & notifyAll(). Let’s understand it with an example;

There are 2 containers: ChocoPowederContainer, WheatPowderContainer. CookieMaker takes some amount of powder from both containers to make a ChocoWheatCookie. There is a Filler who checks container with regular interval. If he finds any container empty, he fills it.

Condition:

  1. CookieMaker starts N threads say maker1, maker2,.. makerN  to produce cookies.
  2. maker5, maker6, and maker9 found ChocoPowederContainer empty. They took powder from WheatPowderContainer.
  3. maker13, maker10, and maker7 found WheatPowderContainer empty. They took powder from ChocoPowederContainer.
  4. Filler finds ChocoPowederContainer empty. So he filled it and notifyAll() makers. After sometime Filler finds another container empty and fills it and notifyAll() again.

But maker13, maker10, and maker7 want to be notified only when WheatPowderContainer get filled. And maker5, maker6, and maker9 want to be notified only when ChocoPowederContainer get filled.

For the solution, Filler can make 2 Condition objects, say ChocoPowederContainerEmpty and  WheatPowderContainerEmpty. maker5, maker6, and maker9 will call ChocoPowederContainerEmpty.await() and maker13, maker10, and maker7 will call WheatPowderContainer.await().

Filler will call WheatPowderContainerEmpty.signalAll() once WheatPowderContainer is filled. And so on.

I have made a sample program to understand the above example practically. So keep reading…

Java multithreading – when to use what, why and how

March 11th, 2012 42 views No comments

I code this program to explain Condition in java. Further I extended it to explain the use of

  1. Executor & ExecutorServices to make ThreadPool.
  2. Use of Future Object & Callable interface to enquiry/communicate with running thread.
  3. Use of Semaphore to control number of threads.
  4. Lock over synchronized block
  5. Threads inside threads
  6. Enum & EnumMap to store well known values.
  7. Condition (labeled wait & notify) to notify thread waiting for specific condition.

Classes


Cookie class: First I developed this code with this class then I omitted it. Because this class does nothing but getting/setting an EnumMap. So I directly moved this EnumMap into CookieMaker class

import java.util.EnumMap;

public class Cookie {
	EnumMap<Ingredient, Integer> ingredients;

	Cookie(){
		ingredients = new EnumMap<Ingredient, Integer>(Ingredient.class);
	}

	public void setIngredient(Ingredient i,int quantity){
		ingredients.put(i, quantity);
	}

	public EnumMap<Ingredient, Integer> getIngredients(){
		return ingredients;
	}
}

IngredientContainer Class – represents a container with some method to fill & consume container ingredient. As per the problem, a maker thread should wait if container is empty. If thread A waits for container 1 and B waits for container 2 and Filler fills container 1 then only thread A should be notified. So I have attached Condition object with container class itself.

code

/*
 *
 * (C) Copyright 2012-2013, by Amit Gupta (http://article-stack.com/).
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA. OR see <http://www.gnu.org/licenses/>.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * version 1.0
 *
 * ---------------------
 * IngredientContainer.java
 * ---------------------
 *
 *
 */
public class IngredientContainer {
	private int capacity;//How much quantity of an ingredient a container can have
	private int quantityHeld;
	Condition empty;

	IngredientContainer(int c){
		capacity = c;
	}

	public int getQuantityHeld(){
		return quantityHeld;
	}

	public int getCapacity(){
		return capacity;
	}

	public int getQuantityRequired(){
		return capacity - quantityHeld;
	}

	public void fill(int n) throws Exception{
		if((n + quantityHeld) > capacity){
			throw new Exception("Overfilled");
		}
		quantityHeld += n;
	}

	public boolean isEmpty(){
		return quantityHeld == 0;
	}
	/**
	 *
	 * @param n filled units
	 * @return
	 */
	public int fillSafe(int n){
		int require = capacity  - quantityHeld;
		int toBeFilled = Math.min(require, n);
		quantityHeld +=toBeFilled ;
		return toBeFilled ;
	}

	public void setEmptyCondition(Condition c){
		this.empty =c;
	}
	public boolean getIngredient(int n) throws Exception{
		if(n > capacity){
			throw new Exception("Accessing quantity more than capacity");
		}
		if(quantityHeld >= n){
			TimeUnit.SECONDS.sleep(1);
			quantityHeld -= n;
			return true;
		}

		System.out.println("Less Quantity Held");
		return false;
	}
}

Filler class – One filler is associated with one maker in our program who keeps watch on containers of associated maker only. And fills them.

code

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;

/*
 * Filler checks a Container with regular interval. Either it fills Container
 * safely or check it before filling it.
 *
 * (C) Copyright 2012-2013, by Amit Gupta (http://article-stack.com/).
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA. OR see <http://www.gnu.org/licenses/>.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * version 1.0
 *
 * ---------------------
 * Filler.java
 * ---------------------
 *
 *
 */
public class Filler {
	private ArrayList<IngredientContainer> ingredientContainers;
	int capacity = 6;
	private int checkInterval = 3;
	private int fillingQuantity = 2;
	private boolean isInterrupted = false;
	private Lock containerLock;

	public Filler(int c, Lock l) {
		ingredientContainers = new ArrayList<IngredientContainer>();
		capacity = c;
		containerLock = l;
	}

	public void addContainer(IngredientContainer c) throws Exception{
		if(ingredientContainers.size() == capacity)
			throw new Exception("Filler is overloaded");
		ingredientContainers.add(c);
	}
	/**
	 * Filler checks container with regular interval and fills if it is empty.
	 * @author Amit Gupta
	 */
	public void startFilling(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("Filler has started working");
				while(!isInterrupted){
					try {
						TimeUnit.SECONDS.sleep(checkInterval);
						System.out.println("Filler starts checking containers");
						containerLock.lock();
							Iterator<IngredientContainer> itr = ingredientContainers.iterator();
							while(itr.hasNext()){

								IngredientContainer ingredientContainer = itr.next();
								System.out.println("Require : "+ingredientContainer.getQuantityRequired());
								System.out.println("Capacity : "+ingredientContainer.getCapacity());
								System.out.println("Filling : "+ fillingQuantity);

									int filledQ = ingredientContainer.fillSafe(fillingQuantity);//Try to fill required quantity only.
									System.out.println("Filled " + filledQ );
									ingredientContainer.empty.signalAll();//This condition must be instantiate from CookieMaker

							}
						containerLock.unlock();
					} catch (Exception e) {
						System.out.println(e.getMessage());
					}
				}
			}
		}).start();
	}

	public void stopFilling(){
		isInterrupted = true;
	}
	/**
	 * How long Filler should wait checking a container.
	 * @param checkInterval Seconds. default is 3 seconds.
	 */
	public void setCheckInterval(int checkInterval) {
		this.checkInterval = checkInterval;
	}

	/**
	 * How much quantity should be filled in a container, if it is empty.
	 * @param fillingQuantity default 10.
	 */
	public void setFillingQuantity(int fillingQuantity) {
		this.fillingQuantity = fillingQuantity;
	}
}

CookieMaker class – This is heart of the program. All other classes are created to support this class. It seems little bit complex. So we’ll understand it into parts.

code

package cookie;

import java.util.EnumMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*
 * CookieMaker bakes N cookies at a time. He can bake 1 type of cookie only
 * Required containers must be installed in prior to bake a cookie.
 *
 * (C) Copyright 2012-2013, by Amit Gupta (http://article-stack.com/).
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA. OR see <http://www.gnu.org/licenses/>.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 * version 1.0
 *
 * ---------------------
 * CookieMaker.java
 * ---------------------
 *
 *
 */

public class CookieMaker implements Runnable{
	EnumMap<Ingredient,IngredientContainer> containers;
	int containerCapacity = 0;//How many containers a maker can have
	Semaphore bakingCapacity;//How many cookies a maker can bake
	EnumMap<Ingredient, Integer> cookie;
	private Lock bakingLock = new ReentrantLock();
	private Lock containerLock = new ReentrantLock();
	Filler fillingWorker = new Filler(6, containerLock);

	public void addContainer(Ingredient i,IngredientContainer c) throws Exception{
		//System.out.println("Adding " + i.toString() + " container.");
		if(containers.size() == containerCapacity){
			throw new Exception("Containers over loaded");
		}
		c.setEmptyCondition(containerLock.newCondition());
		//System.out.println("Condition is added to Container");
		fillingWorker.addContainer(c);
		//System.out.println("Container is added to Filler");
		containers.put(i,c);
		System.out.println(i.toString() + "container added");
	}	

	public void StartBaking(){
		try{
			System.out.println(bakingCapacity.getQueueLength() + " are waiting");
			bakingCapacity.acquire();
			System.out.println(bakingCapacity.availablePermits() + " more threads can enter");
			bakingLock.lock();
				System.out.println(Thread.currentThread().getName() + " :: Entered.");
				Set<Ingredient> ingredients = cookie.keySet();
				Iterator<Ingredient> itr = ingredients.iterator();
				ExecutorService pool;
				Set<Future<Boolean>> tasks = new HashSet<Future<Boolean>>();

				try{
					pool = Executors.newFixedThreadPool(containers.size());
					while(itr.hasNext()){
						System.out.println();
						final Ingredient ingredient = itr.next();
						tasks.add(pool.submit(new Callable<Boolean>() {
							@Override
							public Boolean call() throws Exception {
								return getIngredient(ingredient);
							}
						}));
					}
					Iterator<Future<Boolean>> fuItr = tasks.iterator();
					boolean tasksCompleted = true;

					while(fuItr.hasNext()){
						Future<Boolean> f = fuItr.next();
						tasksCompleted &= f.get();
					}
					if(tasksCompleted){
						System.out.println("Mixture is ready. Backing cookie");
						pool.shutdown();
					}
				}catch(Exception e){
					e.printStackTrace();
				}

				System.out.println("Thread " + Thread.currentThread().getName() + " :: completed");
			bakingLock.unlock();
			bakingCapacity.release();//let other threads start baking
			fillingWorker.stopFilling();
		}catch(InterruptedException ie){
			ie.printStackTrace();
		}
	}

	/**
	 * Take ingredients from a container. Wait if container is empty.
	 * @param ingredient
	 * @return
	 * @throws Exception
	 * @throws InterruptedException
	 */
	private boolean getIngredient(final Ingredient ingredient) throws InterruptedException, Exception{
		containerLock.lock();
			System.out.println("sub thread for " + ingredient.toString());
			IngredientContainer container = containers.get(ingredient);
			System.out.println(ingredient.toString() + " Container has " + container.getQuantityHeld());
			int quantity = cookie.get(ingredient);
			System.out.println("sub thread : Quantity require:" + quantity);
			while(!container.getIngredient(quantity)){
				container.empty.await();
			}
			//In real world I believe, this method will take some time to take ingredients from a container.
			TimeUnit.SECONDS.sleep(1);
			System.out.println("subThread " + ingredient.toString() + " has completed");
		containerLock.unlock();
		return true;

	}

	public static void main(String[] args) {
		IngredientContainer chocoPowderContainer = new IngredientContainer(12);
		IngredientContainer wheatPowderContainer = new IngredientContainer(15);

		CookieMaker cm = new CookieMaker(4,5);
		EnumMap<Ingredient, Integer> chocoWheatBar = new EnumMap<Ingredient, Integer>(Ingredient.class);

		chocoWheatBar.put(Ingredient.ChokoPowder, 3);
		chocoWheatBar.put(Ingredient.WheatPowder, 1);

		try{
			cm.addContainer(Ingredient.ChokoPowder, chocoPowderContainer);
			cm.addContainer(Ingredient.WheatPowder, wheatPowderContainer);
			cm.setCookie(chocoWheatBar);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}

		new Thread(cm,"maker1").start();
		new Thread(cm,"maker2").start();
		new Thread(cm,"maker3").start();
		new Thread(cm,"maker4").start();
		new Thread(cm,"maker5").start();
		new Thread(cm,"maker6").start();

		cm.fillingWorker.startFilling();

	}

	@Override
	public void run() {
		if(checkSetup()){
			StartBaking();
		}else{
			System.out.println("Initial setup is required");
		}
	}

	public CookieMaker() {
		containers = new EnumMap<Ingredient,IngredientContainer>(Ingredient.class);
	}

	public CookieMaker(int bc) {
		bakingCapacity = new Semaphore(bc);
		containers = new EnumMap<Ingredient,IngredientContainer>(Ingredient.class);
	}

	public CookieMaker(int bc, int cc) {
		bakingCapacity = new Semaphore(bc);
		this.containerCapacity = cc;
		containers = new EnumMap<Ingredient,IngredientContainer>(Ingredient.class);
	}

	public void setCookie(EnumMap<Ingredient, Integer> cookie) {
		this.cookie = cookie;
	}

	public void setBakingCapacity(int bakingCapacity) {
		this.bakingCapacity = new Semaphore(bakingCapacity);
	}

	public void setContainerCapacity(int containerCapacity) {
		this.containerCapacity = containerCapacity;
	}

	/**
	 * Checks Maker's initial set up.
	 * @throws Exception
	 */
	public boolean checkSetup(){
		boolean signal = false;
		/*if(bakingCapacity. < 1){
			System.out.println("Maker can not bake cookies");
		}
		else*/ if(containerCapacity < 1){
			System.out.println("Container capacity is 0");
		}
		else if(containers.size() < 1){
			System.out.println("No container is installed.");
		}else{
			signal = true;
		}
		return signal;
	}
}


I am not attaching output of above classes here since it is too long. I am chunking it and explaining you important parts.

bakingCapacity – Maker has the capacity of baking 4 cookies at a time. bakingCapacity semaphore keeps counting on running thread. If 4 threads are running and 1 completes its working, bakingCapacity allows 1 more thread. Review the output below;

0 are waiting
3 more threads can enter
maker1 :: Entered.
0 are waiting
2 more threads can enter
0 are waiting
1 more threads can enter
0 are waiting
0 more threads can enter
Thread maker1 :: completed
maker3 :: Entered.
0 more threads can enter
:

In above output, bakingCapacity allows 1st 4 threads to get entered. marker1 enters into its critical session. Total 4 threads are inside semaphore this time. Out of them 1 is running and other 3 are waiting. marker1 completes its work and get out from semaphore. marker3 enters into monitor. Now 2 threads are waiting and 1 is running. Total 3 threads are in semaphore. So bakingCapacity allows 1 more thread to get enter. And so on.


bakingLock: This lock ensures that only 1 thread can enter in its monitor. If you see in output you’ll notice following sequence;

maker1 :: Entered.
Thread maker1 :: completed
maker3 :: Entered.
Thread maker3 :: completed
maker5 :: Entered.
Thread maker5 :: completed
maker2 :: Entered.
Thread maker2 :: completed
maker4 :: Entered.
Thread maker4 :: completed
maker6 :: Entered.
Thread maker6 :: completed

Here the question arises if all threads are running in sequence then what is the need of multi-threading?

MultiThreadig is required when multiple tasks run parallel. And Locking is required when you fear that some other thread can change value of a variable required by current thread.

Let’s traverse each statement of StartBaking().

  1. What type of ingredients a cookie has?
  2. For each ingredient take required quantity from relevant container.
  3. If 1 container is empty then take ingredient from another container and for 1st container to get filled.
  4. Once ingredients are taken from all containers, bake cookie.

Ingredients are always fixed for a cookie type. It could be critical if a maker can produce multiple types of cookies. Moreover, local variables are always thread safe. So we need not to worry for point a).

For point b) & c), we will have to take ingredients from containers parallel. So for each container we’ll have to create one sub-thread. Containers are being filled by Filler. When a sub-thread takes ingredients from container, container’s quantity get reduced. So there is a critical condition when 2 threads check container quantity at the same time and both reduce it.

public boolean getIngredient(int n) throws Exception{
		:
		if(quantityHeld >= n){
			quantityHeld -= n;
			return true;
		}
		:
}

This section requires lock. Since we already acquiring lock in getIngredient() of Maker class. So there is no need of locking in StartBaking().

Once ingredients are taken, we don’t have to check any value which can be modified by any other thread.


containerLock – Condition discussed in above points proves the need of this lock. But we’ll analyze it.

java multithreading CookieMaker containers

Suppose marker1 creates 2 sub-threads A, B. marker2 creates C,D. All sub-threads call getIngredient() of Maker class. ChocoPowder Container has 2 units of powder while other container has 5 units. As per cookie recipe, A & C require 3 units while B & D require 1 unit.

  1. A : enter
  2. C: waiting
  3. A: finds insufficient quantity in container. waits and releases lock.
  4. C: enters
  5. Filler : fills 2 units. So total units in ChocoPowder container are 4.
  6. A & C both access getIngredient() of container class at the same time.
  7. A & C both finds sufficient quantity. So both reduce quantity and come out.
  8. Current quantity in ChocoPowder container is -2 units.

Where we require lock?

containerLock can be moved down since starting statements are creating local variables which are thread safe. Note that container is referencing field member. But its value is fixed so the next version of method could be as follows;

	private boolean getIngredient(final Ingredient ingredient) throws Exception{
		IngredientContainer container = containers.get(ingredient);
		int quantity = cookie.get(ingredient);
		containerLock.lock();
			while(!container.getIngredient(quantity)){
				container.empty.await();
			}
		containerLock.unlock();
		return true;
	}

Even this move doesn’t solve the problem. Our need says that getIngredient() of container class should be accessed by only one thread at a time. So I must make it synchronized. I don’t require to synchronize fill() in our example.

This output will show that everything is fine after above changes.

output


A: sub thread for ChokoPowder
A: ChokoPowder Container has 0
A: sub thread : Quantity require:3
A: Less Quantity Held
B: sub thread for WheatPowder
B: WheatPowder Container has 0
B: sub thread : Quantity require:1
B: Less Quantity Held
C: sub thread for ChokoPowder
C: ChokoPowder Container has 0
C: sub thread : Quantity require:3
C: Less Quantity Held
D: sub thread for WheatPowder
D: WheatPowder Container has 0
D: sub thread : Quantity require:1
D: Less Quantity Held
Filler: Filler starts checking containers
Filler(choco): Require : 12
Filler(choco): Capacity : 12
Filler(choco): Filling : 2
Filler(choco): Filled 2
Filler(wheat): Require : 15
Filler(wheat): Capacity : 15
Filler(wheat): Filling : 2
Filler(wheat): Filled 2
A: Less Quantity Held
C: Less Quantity Held
B: subThread WheatPowder has completed
D: subThread WheatPowder has completed
Filler: Filler starts checking containers
Filler(choco): Require : 10
Filler(choco): Capacity : 12
Filler(choco): Filling : 2
Filler(choco): Filled 2
Filler(wheat): Require : 15
Filler(wheat): Capacity : 15
Filler(wheat): Filling : 2
Filler(wheat): Filled 2
A: subThread ChokoPowder has completed
C: Less Quantity Held

Mixture is ready. Backing cookie
Thread maker1 :: completed
Filler: Filler starts checking containers
Filler(choco): Require : 11
Filler(choco): Capacity : 12
Filler(choco): Filling : 2
Filler(choco): Filled 2
Filler(wheat): Require : 13
Filler(wheat): Capacity : 15
Filler(wheat): Filling : 2
Filler(wheat): Filled 2
subThread ChokoPowder has completed
Mixture is ready. Backing cookie
Thread maker2 :: completed


containerLock in Filler class : since we are creating only one thread of Filler class so there will not be another thread calling fillSafe() at the same time. Although writing it under lock is good practice. But some statements can be written out of lock as follows;

containerLock.lock();
	int filledQ = ingredientContainer.fillSafe(fillingQuantity);			ingredientContainer.empty.signalAll();
containerLock.unlock();

I hope this article can explain you more about multithreading. Keep reading …

External Locking vs synchronized

March 10th, 2012 27 views No comments

You must be aware with the need of synchronization after reading Bad synchronization leads program’s death and Synchronization – solve the mystery.

Well! synchronized has some limitations. Lets understand with this example.

public class DummyClass {
	private Integer age = 0;
	public  void display(){
		System.out.println(Thread.currentThread().getName() + ": " + age);
	}
	public  void edit(int n){
		age = n;
	}
}

public class SynchronizedLimitDemo {

	public static void main(String[] args) {
		final DummyClass dcObj = new DummyClass();
		Thread amit = new Thread(new Runnable(){
			@Override
			public void run() {
				try{
					dcObj.edit(27);
					Thread.sleep(1000);
					dcObj.display();
				}catch(InterruptedException ixp){
					System.out.println(ixp.getMessage());
				}
			}
		},"Amit Thread");

		Thread arti = new Thread(new Runnable(){
			@Override
			public void run() {
				try{
					dcObj.edit(29);
					Thread.sleep(1000);
					dcObj.display();
				}catch(InterruptedException ixp){
					System.out.println(ixp.getMessage());
				}
			}
		},"Arti Thread");

		amit.start();
		arti.start();
	}
}

In above stupid example, even if you synchronized display() & edit(), you can’t get proper output. Now see how external locking (since Java 5) can solve it.

public class DummyClass {
	private Integer age = 0;
	private Lock dataLock = new ReentrantLock();
	public  void display(){
		System.out.println(Thread.currentThread().getName() + ": " + age);
		dataLock.unlock();
	}
	public  void edit(int n){
		dataLock.lock();
		age = n;
	}
}

Output:
Arti Thread: 29
Amit Thread: 27

Warning: If a thread tries calling display() directly or before lock was acquired, it’ll generate run time exception. Similarly a thread can call edit() multiple times but can forget calling display(). It’ll lock the object forever.
This example is just to elaborate an advantage of Lock over synchronized.

Problem with synchronized keyword

  1. A synchronized block makes no guarantees about the sequence in which threads waiting to entering it are granted access.
  2. You cannot pass any parameters to the entry of a synchronized block. Thus, having a timeout trying to get access to a synchronized block is not possible.
  3. The synchronized block must be fully contained within a single method. It’s not possible that synchronization starts in one method and ends in another.
  4. If a thread meets synchronized block, where another thread already had entered into it, it is supposed to wait until another thread release the lock.

Java 5 provides another locking mechanism. Where

  1. A thread can request to acquire a lock with timeout period. If lock is not acquired then thread can do something else or can request again.
  2. A Lock can be started in one method and can end in another.
How to

synchronized

	method(
		synchronized(this){
			//critical section
		}
	}

Lock

	Lock lock = new ReentrantLock();

	method(
		lock.lock();
			//critical section
		lock.unlock();
	}

* It is good to call unlock() in finally block.

Methods

The Lock interface has the following primary methods:

  • lock()
  • lockInterruptibly()
  • tryLock()
  • tryLock(long timeout, TimeUnit timeUnit)
  • unlock()

The lock() method locks the Lock instance if possible. If the Lock instance is already locked, the thread calling lock() is blocked until the Lock is unlocked.

The lockInterruptibly() method locks the Lock unless the thread calling the method has been interrupted. Additionally, if a thread is blocked waiting to lock the Lock via this method, and it is interrupted, it exits this method calls.

The tryLock() method attempts to lock the Lock instance immediately. It returns true if the locking succeeds, false if Lock is already locked. This method never blocks.

The tryLock(long timeout, TimeUnit timeUnit) works like the tryLock() method, except it waits up the given timeout before giving up trying to lock the Lock.

The unlock() method unlocks the Lock instance. Typically, a Lock implementation will only allow the thread that has locked the Lock to call this method. Other threads calling this method may result in an unchecked exception (RuntimeException).

Note:
If you have locked an object N times you will have to call unlock() N times.

Bad synchronization leads program’s death

March 8th, 2012 29 views No comments

amty husband of arti

Amit, father of 6 children, whose wife Arti has gone to her maternal house for a week, has very less knowledge of housekeeping, cooking, gardening etc. 6 children will definitely prove need of a woman in man’s life.

The same situation occurs in a programmer life when he births creates many threads and doesn’t tell CPU how to handle them. CPU starts handling them in his own way and things get mixed up. Real life situations can be solved somehow. But in computer it ends with abnormal DEATH of a program.


If you don’t want CPU as helpless as Amit is, use proper synchronization.

First identify the problem: threads must not disturb each other even if they are working for same work under one roof.

synchronized

Java provides synchronized keyword to fight such situations. When a thread starts working on some critical work he locks the room (say monitor) using synchronized keyword. So no other thread can enter into the room until the current thread finish his work.

In java, see how a thread locks lines of code.

1. Synchronize an object

public void run() {
	synchronized(target) { // synchronized object
		:
	}
}

2. Synchronize a method

public synchronized void myMethod() {
	:
}
or
public void myMethod() {
	Synchronized(this){
		:
	}
}

Note: It is not necessary that you use synchronized in run() only. It’ll be cleared to you when you’ll see sample program below.

public class DummyClass {
	private Integer age = 0;

	public  void display(){
		System.out.println(Thread.currentThread().getName() + ": " + age);
	}
	public  void edit(int n){
		age = n;
		display();
	}
}

public class Tester {

	public static void main(String[] args) {
		final DummyClass dcObj = new DummyClass();
		Thread amit = new Thread(new Runnable(){
			@Override
			public void run() {
				try{
					Thread.sleep(1000);
					dcObj.edit(27);
				}catch(InterruptedException ixp){
					System.out.println(ixp.getMessage());
				}
			}
		},"Amit Thread");

		Thread arti = new Thread(new Runnable(){
			@Override
			public void run() {
				try{
					Thread.sleep(1000);
					dcObj.edit(29);
				}catch(InterruptedException ixp){
					System.out.println(ixp.getMessage());
				}
			}
		},"Arti Thread");

		amit.start();
		arti.start();
	}

}

Output:
Amit Thread: 29
Arti Thread: 29

[If you feel that the syntax I mentioned above are not matching with code I wrote in sample program, please read basic threading.]

Opps!! what I did. This is like Amit’s younger son asked for lollipop and got cigarette. NOT ACCEPTABLE.

put synchronization;

public class DummyClass {
	private Integer age = 0;

	public  void display(){
		synchronized(age){
			System.out.println(Thread.currentThread().getName() + ": " + age);
		}
	}
	public  void edit(int n){
		synchronized(age){
			age = n;
			display();
		}
	}
}

Output:
Amit Thread: 27
Arti Thread: 29

I did it !!!!!!!!!!!!!!!!!!!!

 

Know more about synchronized:

  1. If you are synchronizing a static method then it’ll take lock over complete class.
  2. If you place synchronized block over the same object on multiple places and one of the thread had already entered into one of them, no other thread can enter into any of the synchronized block until first thread comes out.
  3. blah blah

Well! I am not inking any extra point here which can make you fear from synchronized. I’ll cover all rest points in separate article with spicy examples.

Happy coding :)

Write a program for FTP, TelNet, Ping or any other service in a minute

January 23rd, 2012 59 views No comments

I wasted to collect sample code for communicating with FTP then Ping. When I compared both program, I found there is no difference between both program since they are just for connecting to specific service and were not doing anything extra. Only the single difference what i found is their port number.

See your self;

Write a program to communicate with FTP server

:
Socket t = new Socket(hostname, 21);
:

Write a program to ping a server

:
Socket t = new Socket(hostname, 7);
:

Good trick… isn’t it? So there is a list of well known ports which can help you to connect particular service on the server.

Port Number Description
1 TCP Port Service Multiplexer (TCPMUX)
5 Remote Job Entry (RJE)
7 ECHO
18 Message Send Protocol (MSP)
20 FTP — Data
21 FTP — Control
22 SSH Remote Login Protocol
23 Telnet
25 Simple Mail Transfer Protocol (SMTP)
29 MSG ICP
37 Time
42 Host Name Server (Nameserv)
43 WhoIs
49 Login Host Protocol (Login)
53 Domain Name System (DNS)
69 Trivial File Transfer Protocol (TFTP)
70 Gopher Services
79 Finger
80 HTTP
103 X.400 Standard
108 SNA Gateway Access Server
109 POP2
110 POP3
115 Simple File Transfer Protocol (SFTP)
118 SQL Services
119 Newsgroup (NNTP)
137 NetBIOS Name Service
139 NetBIOS Datagram Service
143 Interim Mail Access Protocol (IMAP)
150 NetBIOS Session Service
156 SQL Server
161 SNMP
179 Border Gateway Protocol (BGP)
190 Gateway Access Control Protocol (GACP)
194 Internet Relay Chat (IRC)
197 Directory Location Service (DLS)
389 Lightweight Directory Access Protocol (LDAP)
396 Novell Netware over IP
443 HTTPS
444 Simple Network Paging Protocol (SNPP)
445 Microsoft-DS
458 Apple QuickTime
546 DHCP Client
547 DHCP Server
563 SNEWS
569 MSN
1080 Socks

Besides this list, there is a list of some software which connects to specific port. If the port is not free or busy with another service then either that application will search for alternate port or will stop working.

You can refer the complete list on wikipedia.

Serialization – a Sci Fi comic book story

December 18th, 2011 1061 views No comments

Short story about serialization

Serialization an interesting story of mars and earth scientistAfter hard work of many years, Earth’s scientist developed a robot who can help them in daily work. But this robot was less featured than the robots which were developed by the scientist of Mars planet.

After a meeting between both planet’s scientist, it is decided that mars will send their robots to earth. But a problem occurred. The cost of sending 100 robots to earth was $100 millions. And it takes around 60 days for traveling.

Finally, Mar’s scientist decided to share their secret with Earth’s scientists. This secret was about the structure of class/robot. Earth’s scientists developed the same structure on earth itself. Mar’s scientists serialized the data of each robot and send it to earth. Earth’s scientists deserialized the data and fed it into each robot accordingly.

This process saved the time in communicating mass amount of data.

Some of the robots were being used in some defensive work on Mars. So their scientists marked some crucial properties of those robots as transient before sending their data to Earth. Note that transient property is set to null(in case of reference) or to default value(in case of primitive type) when the object gets deserialized.

One more point which was noticed by Earth’s scientists is that Mars’s scientists ask them to create some static variables to keep environmental detail. This detail is used by some robots. But Mars’s scientists dint share this detail since the environment on earth was different than Mars environment.

Even though knowing about the robot class structure and having serialized data Earth’s scientist were not able to deserialize the data which can make robots working.

Exception in thread "main" java.io.InvalidClassException:
SerializeMe; local class incompatible: stream classdesc
:

Mars’s scientists were waiting for the complete payment. Once the payment was done Mars’s scientists shared the serialVersionUID with Earth’s scientists. Earth’s scientist set it to robot class and everything became fine.

One of the Mar’s Scientist: Earth’s scientists are very slow to deserialize some of our best Robots. They must be using old version of java. They should either switch to new java version or to implement Externalizable.

Terms highlighted in above example are:
a. Serialization
b. Deserialization
c. Transient variables
d. Static variables
e. Externalizable

Technically

Serialization is nothing but storing current state of an object to physical file. So the data can be used later by deserializing it.

When it is required:

a. When a system gets crashed.
b. When a player want to save all played game data so he can continue playing later.
c. When a process need to be stopped by some reason. But it must continue working the next time with the same data.

How to

a. Implements Serializable or Externalizable
b. Serialize:

	ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("fileName"));
	out.writeObject(obj);

c. Deserialize:

	ObjectInputStream in = new ObjectInputStream(new FileInputStream("fileName"));
	in.readObject();

Java serialization all important points through image Image in words

1.       Object of class A can’t be deserialized to the object of class B.

2.       Object of class A can’t be deserialized to the object of class A itself if any of the instance variable changes its nature. For example

  1. Name
  2. Data type
  3. Removed from the class
  4. Make it transient or static

But if serialVersionUID is added to the class of serialized and deserialized object with same value then deserialization will not generate any error (excluding point b). So if variable is not found then its value is not set.

In short, deserialization finds instance variable with the same name they were used to serialize. If variable name is changed or they are removed from the class then it throws error. But if serialVersionUID is same then it skips initialization for not found variables.

In addition, if a variable is found with the same name but its datatype is changed then it generate error of casting even if serialVersionUID is same.

Changing their access level doesn’t impact.

3.       If serialVersionUID is not same for the class of serialized and deserialized object then deserialization throws error even if class structure are exact same.

4.       Methods, transient variables and static variables are not transient.

5.       An object can be serialized only if its class implements either Serializable or Externalizable.

6.       If extra variables found than the variables which are serialized then they are set to their default value at the time deserialization.

7.       Making a class part of inheritance tree is allowed. But moving a class in existing inheritance tree up/down is not allowed since it can affect saved/serialized object tree.


Why Externalizable

  1. It provides control of reading/writing object to/from an output/input stream.
  2. It was very useful till Java 1.2 since reflection was slow
  3. You can decide which object has to left from object tree or what default value has to be set to transient variables while deserialization.

Methods of Externalizable

public void writeExternal(ObjectOutput out) throws IOException {
	out.writeFloat(x);
	out.writeFloat(y);
	out.writeFloat(z);
	out.writeFloat(w);
}

public void readExternal(ObjectInput in) throws IOException {
	x = in.readFloat();
	y = in.readFloat();
	z = in.readFloat();
	w = in.readFloat();
}

Sample Code

I am attaching source code, I practiced on, for serialization and externalization.

This area is protected to registered users only.

Java Collection Framework Doodle Chart – what to use when and why

November 21st, 2011 362 views 3 comments

I saw most of the people, including me, using ArrayList and HashMap only in their project. I was surprised why the Java provided us so many varieties of collection classes if we are done with only these two.

Since I was not interested to read a whole book for this so I made a doodle chart (cheat sheet, decision chart and short notes) that you can hang to your desk wall or somewhere on your notice board. I have tried to pin up all small aspects related to collection framework. It can help you to decide which class or data structure you should use in your project and why, to maintain high performance.

This is first version of the chart excluding the practical use of Queue interfaces and its classes. I’ll definitely cover them in next article or in next version of this chart.

java,cheat sheet,short notes,class diagram,collection framework,doodle chart

properties,hashtable,hashmap,set,map,sortedset,treemap,treeset,linkedlist,linkedhashmap,linkedhashset,hashset,IdentityHashMap,WeakHashMap,Dictionary,NavigableMap,NavigableSet

java,cheat sheet,short notes,class diagram,flowchart,collection framework,arraylist,hashtable,hashmap,set,map,sortedset,treemap,treeset,linkedlist,linkedhashmap,linkedhashset,hashset,IdentityHashMap,WeakHashMap,NavigableMap,NavigableSet

Above images are not suitable to reference. So you can download PDF version from below link.

This area is protected to registered users only.

*Above contents are copyrighted. You are free to redistribute the PDF or images but the credit to the author or author’s site must not be removed.

Interface vs abstract class – when to use what and why

November 13th, 2011 295 views No comments

After writing an article about practical difference between abstract class and interface, I found myself less practical in that article. So I am writing this revised article with some theoretical difference for your interview and practical differences so you can decide what to use when and why.

As we are aware with the concept of inheritance, child always inherits something from his parent. Sometimes it could be some assets, a child can use. And sometimes they can be some duties, a child is responsible for.

Real time scenario

Mr Gupta was very rich business man. He left 3 factories, 2 houses, 4 cars and a big bank balance to his only son Amit. But as per Gupta bequest, Amit was supposed to be agreed paying 17% of total profit to an organization as charity. He is not liable to change the procedure of charity. He was also supposed to renew some business licenses, insurance policies etc time to time. But he was free to do such payments in his own way.

Lets draw this situation…

abstract class vs interface when to use what example

Above class diagram depicts this real time scenario programmatically. Base class “Mr Gupta” is giving his assets to Amit. donate() is one of the method Amit received in inheritance. Amit will use donate() as it is, as Mr Gupta wanted. So the actual code for donate() will go in base class Mr Gupta. But there are some bequest conditions which must be followed by Amit but his own way. So the above diagram looks perfect for this situation. But the same can be drawn in following manner too.

abstract class vs interface when to use what example

When to use What?

Well it depends completely on your need. Basically, an abstract class is combined form of base class and interface.

Abstract class = Base class + Interface

But what is the base class then? In summary, a base class is a class which has something to give to his child. So the child can use it as it is without putting his own effort. However the child is free to decide his own way.

  • If a base class has no asset to give to his child then there is no sense to make it.
  • Similarly, if a base class has nothing to give but wants his child to implement some procedures then you must go for Interface not a class.
  • And if base class has something to give and something he wants from his child to do mandatory then you must go for an abstract class.

Mathematically


Abstract class = Base class + Interface
Interface = Abstract class – Base class
Base class = Abstract class – Interface

Now there are some thumb rules which can help you to design time confusion. Just follow them.

Rules:

  1. If there are more than one inheritance trees in your application and you want some common functionalities in both tree then use Interface. If these functionalities are closely related to one inheritance tree then go for abstract class.
  2. Make three set of functionalities which must be followed by classes under a single inheritance tree:
    1. Common for all classes
    2. Common for some classes but not all
    3. Separate for each class

    for (a), create abstract class. For (b), create Interface. And for (c), include the functionality in class itself.

  3. Use interface to define the type of any class [Example: Serializable interface, Comparable interface].
  4. Use interface when you wants methods with same name among many classes. It helps you to call such methods without knowing class type using either reflection or a reference object of interface. [Example: Java Comparable, Java Collection, How to force a programmer to implement an interface]
  5. Dynamic proxies. The invaluable java.lang.reflect.Proxy class allows you to make an implementation of any interface at runtime, where calling any of the interface’s methods results in a call to a single invoke method. Read about Proxy Pattern before knowing more about Dynamic proxies.

What to care about

There are points you must keep in your mind before designing your class diagram.

  1. There should be as minimum as possible abstract methods in an abstract class or an interface. Because inclusion or exclusion of any method in the future require the change in all the classes of related inheritance tree.
  2. Since an abstract class is a base class so you can add an implemented method to this.
  3. Since an abstract class is a base class so it must have some implemented methods being used by child classes. So removing an abstract class from an inheritance tree requires changes in all classes beneath the abstract class. But converting it to base class would be an easier deal.
  4. Removing an interface from an inheritance trees is really an easier approach until that interface is not being used as a type of a class. [See this example: player]
  5. Retrofitting an interface to a class is much easier than a class since a class can implement any number of interface but can extent only single class at a time.
  6. Interfaces are designed with the concept of implementing its all methods. Some of them might not be suitable for all classes. But programmers are supposed to implement those useless methods in their class. It results ignoring interface.

*This article is copyrighted under creative common license. You are free to redistribute this article but with credit to original article

How to force a programmer implementing an interface

November 13th, 2011 64 views No comments

Most of the times we want programmer to follow common naming conventions for standard methods through out the all classes. It helps when we need to call a method using reflection or may be with some other purpose. But other developers generally are not aware with these rules. So they starts defining their own methods similar to standard method but not exact same in name.

There is a way to force them to use same naming convention using interface. Implementation of this way could be different as per your project requirement.

Interface

interface Player{
    public void play();
    public void pause();
    public void stop();
}

Classes which are implementing Player Interface;

class AudioPlayer implements Player{...}
class VideoPlayer implements Player{...}

Some extra class. Meaningless but seems relevant

Class PlayList{}

An enum who plays important role;

enum MEDIAPLAYER {
    AUDIO,VIDEO;

    public Player getPlayer() {
        switch (this) {
            case AUDIO:
                return new AudioPlayer();
            case VIDEO:
                return new VideoPlayer();
            default:
                return new AudioPlayer();
        }
    }
}

Using in code

Player testPlayer = MEDIAPLAYER.getPlayer();
:
testPlayer.play();

Now if any new player is added, its entry will go to MEDIAPLAYER which is returning Player type object. So everyone would have to implement Player

Another approach

You can create an abstract class, say MasterPlayer, who is having all the abstract methods Player interface has. In addition, MasterPlayer will have some additional methods like managing PlayList

I hope this example will help you to understand when to use what in interface and abstract class.

End of Java Generic

September 24th, 2011 77 views No comments

No no no no no no no!! don’t mean it wrong. I am just writing last article on Java Generics. I Hope you already had read;

  1. Java Generics – Boys are not allowed in Girls community
  2. Java Generics continue

I’ll continue writing practical problems related to generics, if you guys face any problem or I experience.
After reading above articles you must be clear with what are generics, how to use them, what basic mistakes we generally commit, generics type (remember A,B..E…T…Z) etc etc

You know what, wild card makes me wild everytime. See the below code

public class GenericClass<T> {
	  public void doSomething(Set<T> set) {
	    Set<?> copy = new HashSet<?>(set); //Err
	  }

	  public static void main(String args[]){
		  Object obj = new GenericClass();
		  Object obj2 = new GenericClass<String>();
		  Object obj3 = new GenericClass<?>();//Err:Cannot instantiate
		  GenericClass obj4 = new GenericClass<String>();
		  GenericClass<Number> obj5 = new GenericClass<String>();//Err: Type mismatch: cannot convert
		  GenericClass<String> obj6 = new GenericClass<String>();

		  //Working code
		  //Set<String> set;
		  //Set<String> copy = new HashSet<String>(set);
		  //obj6.doSomething(copy);

		  Set<String> set;
		  Set<?> copy = new HashSet<String>(set);
		  obj6.doSomething(copy);//Err: reason - Set<?> copy can accept any type of Set. But doSomething() can accept only String type of Set.
	  }
}

Above code, problems and reason are self explanatory. And I guess, I had covered most of them in very beginning already. Remember that, ‘T’ doesn’t like ‘?’ and ‘?’ doesn’t like ‘T’ too. So the object og GenericClass will not accept new GenericClass(). Same with the method or with any statement, refer doSomething() in above code.

Remember; Try to avoid wild card as more as possible. Refer below codes for better practice;

interface Collection<E> { public boolean containsAll(Collection<?> c);
	public boolean addAll(Collection<? extends E> c);
}
//Above code is correct but can be rewritten as

interface Collection<E> { public <T> boolean containsAll(Collection<T> c);
	public <T extends E> boolean addAll(Collection<T> c);
}

One more dice..

class Collections {
	public static <T> void copy(List<T> dest, List<? extends T> src){...}
}

//Above code is correct but can be rewritten as (without using wildcard)
class Collections {
	public static <T, S extends T> void copy(List<T> dest, List<S> src){...}
}

Refer below complex Generic method signature just for your reference.

public static <T extends Object & Collection<Integer>> void addToSet(Set<T> s) {...}

Leave your queries, ideas and suggestion.

Java Generics continue

September 24th, 2011 57 views No comments

I don’t have so much time to continue writing interesting points about java generics. So I am summarizing some advanced portion of java generics here with enough practical explanation excluding tedious theory.

If you are missing something or something is unclear or you really need some good examples then drop your comments. I’ll update this article as per your desire. But first go through my first article: Java Generics – Boys are not allowed in Girls community

Review the below code

//--Wrong--
static void fromArrayToCollection(Object[] a, Collection<?> c) {
	for (Object o : a) {
		c.add(o); // compile time error
	}}

let me explain the reason.

  • fromArrayToCollection() says that elements of Object[] a can be added to Collection c.
  • An Object array can have array of String or Number or any other class since it is the parent class of any class.
  • Collection< ? > c can be of ? type ie String or Number etc

It means, below statements must be acceptable

Number[] na = new Number[100];
Collection<String> cs = new ArrayList<String>();
fromArrayToCollection(na, cs);// compile-time error

A Number can not be added to a Collection of String. Correct syntax would be as below;

//--Right--
static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
	for (T o : a) {
		c.add(o); // correct
	}}

Had we discussed A,B,...E...T...Z before? .... OOPs I forgot to tell you. Actually,

This area is protected to registered users only.


Running Multiple Tomcat instances

September 4th, 2011 143 views No comments
  1. Install Tomcat in /opt/tomcat. (You can choose different path as well. You are independent after all)
  2. Create two directories beneath /opt/, say article-stack.com and thinkzarahatke.com, to keep 2 separate instances of Tomcat.
  3. Create required sub-directories in article-stack.com and thinkzarahatke.com.
  4. #cd thinkzarahatke.com
    #mkdir common logs temp server shared webapps work conf
    
  5. Copy all contents of conf directory from installed Tomcat distribution to newly created conf directory in step 3.
  6. #cp -a $CATALINA_HOME/conf conf/.
    

    Tomcat Multiple Instance directory structure Mockup 

  7. Since both Tomcat instance must run from different ports, change port number in both server.xml (/opt/thinkzarahatke.com/conf/server.xml and /opt/article-stack.com/conf/server.xml). Keep port number different in both server.xml.
  8. <Server port="8007" shutdown="SHUTDOWN">
    :
    <!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
    <Connector port="8081" maxHttpHeaderSize="8192"
    maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
    enableLookups="false" redirectPort="8443" acceptCount="100"
    connectionTimeout="20000" disableUploadTimeout="true" />
    
  9. Now, you must set the CATALINA_HOME environment variable to where you installed the Tomcat binary distribution (ie /opt/tomcat) and you must set the CATALINA_BASE environment variable to a different path where you are storing a JVM instance’s files (ie /opt/article-stack.com and /opt/thinkzarahatke.com)
    1. Create start.sh & stop.sh script to run particular Tomcat instance
    2. Put following contents inside start.sh
    3. #!/bin/sh
       set CATALINA_BASE="/opt/article-stack.com "
       set CATALINA_HOME="/opt/tomcat"
       export CATALINA_BASE CATALINA_HOME
       service tomcat start 	# Standard way to start on Linux
      
    4. Put following contents inside stop.sh
    5. #!/bin/sh
       set CATALINA_BASE="/opt/article-stack.com "
       set CATALINA_HOME="/opt/tomcat"
       export CATALINA_BASE CATALINA_HOME
       service tomcat stop # Standard way to stop on Linux
      
    6. Repeat all 3 steps for another instance. But change the value of CATALINA_BASE to “/opt/thinkzarahatke.com”
  10. #mkdir /opt/article-stack.com/bin
    #cd /opt/article-stack.com/bin
    

Java Generics – Boys are not allowed in Girls community

August 29th, 2011 1488 views No comments

In this article you’ll know How to use, Why to use, Where to use generics. You’ll get the answer for what problem we face while using generics with solution, explanation and examples.
I hope short stories and images, used in this article, will keep your interest alive and will increase your understanding.

Syntax

Old generation – Without Generic

List myIntList = new LinkedList(); // 1
myIntList.add(new Integer(0)); // 2
Integer x = (Integer) myIntList.iterator().next(); // 3

New generation – With Generic

List<Integer> myIntList = new LinkedList<Integer>(); // 1
myIntList.add(new Integer(0)); //2
Integer x = myIntList.iterator().next(); // 3

Purpose:

Generics are invented for type safety and to improve readability. Without generic, you are allowed to add any type of object to a List (collection) which you are supposed to type cast further while accessing. (See old generation syntax block above). It leads “chance of type casting exception” at run time. If a programmer is not aware about what type of elements are expected from the list, he can add any type of element to it. For example;


Mr. Ramesh was accessing employee from Person list. But Mr. Smith was unaware with Ramesh program. So he was adding students to Person list and passing it to Ramesh application.


Java class inheritance - generic

With generics this type of confusions can be averted easily. But if you really require that the Person list must allow employee as well as students but not else, generic has provision for that as well.

How to use

  1. When you create an object of a generic class, or you call a generic method you have to tell him the data type (see new generation syntax block above).
  2. When you define/declare a method/class you have to use generic type or specific data type. We’ll study it later.

Now problem begins….

Generics are not covariant. They are invariant
Arrays in the Java language are covariant — which means that if Integer extends Number (which it does), then not only is an Integer also a Number, but an Integer[] is also a Number[], and you are free to pass or assign an Integer[] where a Number[] is called for. (More formally, if Number is a supertype of Integer, then Number[] is a supertype of Integer[].)
But in generic List is not a supertype of List. You can’t pass a List where a List is expected.

List<Integer> inLi = new ArrayList<Integer>();
List<Number> numLi = inLi; // illegal

Explanation:

 

  1. List says that you can add any type of number (Float,Integer,Long) to the list numLi.
  2. But second statement depicts that numLi; pointing to inLi; can have only Integer type.

Both statements contradict each other. So the compiler itself doesn’t allow any contradict statement which depicts type unsafety. So the second statement is not allowed. Instead

List<Integer> inLi = new ArrayList<Integer>();
List<? extends Number> numLi = inLi; // legal
  1. You can pass any element/variable/object of type Number. Here extends is used for extend & implement both.
  2. ? stands for wildcard. It means any class.

Note that

List does not mean “list of objects of different types, all of which extend Number”. It means “list of objects of a single type which extends Number


java Generic example boy and girl

This area is protected to registered users only.
Well! There is one more keyword super. In summary,

List<String> is a subtype of List<? extends Object>
List<Object> is a subtype of List<? super String>

Another example;
This area is protected to registered users only.
Point to be noticed my loard
This area is protected to registered users only.
Intersting… Isn’t it? Let’s study more.

You cannot instantiate an array of a generic type (new List[3] is illegal). For example, the following example is illegal;

List<String>[] stringList = new List<String>[10]; // illegal

Why so?

As we know that every class is the subtype of Object class. And an array of Object class can have array of any class. And where “any” word comes, generic fails.
Since an array of object can have stringList

Object[] objArr = stringList;  // OK because List<String> is a subtype of Object

It can have list of other class type too. But clever generic never takes a chance. So the above statement is wrong. Instead,

Object[] objArr = new Object[10];

And below statements will work fine then

List<Integer> inLi = new ArrayList<Integer>();
inLi.add(new Integer(3));
objArr [0] = inLi;

Are you still not able to remember it? Ok ok just imagine an Object class as a Cricket Team. You can create team of boys as well as girls. But as per the rule, Boys team will play against boys team only and so on.
This area is protected to registered users only.


Java Generic girls vs boys cricket

Alternate
Even though you are not allowed to instantiate generic array, you can declare them in following manner;

List<String>[] stringList = (List<String>[]) new List<?>[10];
stringList.add(“amit gupta”);


Picture अभी बाकी हे मेरे दोस्त …. wait for my next post

*This article is under copyright. Publishing images, code, examples any where on net or book without permission and credit to original article is strictly prohibited.

WordPress plugin hack to display a post with thumbnail

August 11th, 2011 236 views No comments

amtyThumb plugin provides best way to generate thumbnail of an image. It can extract first image from any post. It provides you a single function to do this.

Now just edit your any plugin to display thumbnail along with post list.

Go to setting page of WP-PostViews page. You will find following text in Most Viewed Template:

<li><a href="%POST_URL%"  title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% views</li>

Add %THUMBNAIL% option to above text

<li><img src="%THUMBNAIL%" /><a href="%POST_URL%"  title="%POST_TITLE%">%POST_TITLE%</a> - %VIEW_COUNT% views</li>

Modify wp-postviews/wp-postviews.php
search for “$output .= $temp;”. Add below line just before the searched test.

$temp = str_replace("%THUMBNAIL%", amty_lead_img(75,75,1,'','','zoom',$post->ID);, $temp);

You can edit plugin in the same manner. Otherwise wait for coming version of amty Thumb Post which has capability to display mostly, recently, rarely …. posts with thumbnail and with any style.

Read more about using amtyThumb here. This article will guide you more about amty_lead_img() function & to insert a thumbnail in your post using shortcode.

Cracking a Password is an art

May 28th, 2011 93 views No comments

P@$$w0rd

Everyone is interested to steal personal information of others. But sometimes it is needful. I already had written 3 articles for hacking/cracking passwords and what to avoid.

  1. Brute-force and dictionary attack, poor hacking tactics
  2. How to extract contents from locked zip files
  3. 5 efficient ways to hack locked folders

One more article that I hadn’t published is about keylogger. Let me describe key loggers in brief.

Key logger is an application which can be installed anyone’s PC. They are completely hidden and even the antivirus becomes failed to catch them. They monitor what you type. And all keystrokes can be viewed by the bad guy later who installed it on the machine. They also can be mailed without any notification. So might be your PC is its victim. Nobody needs to visit your place to get this information. That’s why I strongly avoid accessing mails through cyber cafes.

Well! My aim for writing this article is to share a very useful link with you and to improve knowledge in how to guess password.

No body want to forget his password. So he generally chose the password related to his past, someone’s name or some stuff. To make it difficult he uses special characters looks similar to English character. For example , @ in place of a or $ or 5 in place of S.

One more common habit is putting @123 or @1234 at the end of password.

Well it is not the end there are more tricks which make someone guess password and crack it easily.

My this article is inspired by How To Guessing Hacking Figuring Cracking Password Art Guide

Write your own Progress Listener

May 17th, 2011 205 views No comments

While writing some other articles over Anonymous Classes, I wrote a code to watch over how many bytes of a file has bean read or write. I am sharing that code with all of you to improve understanding in use of anonymous Classes, advanced java concept.

This is very simple and handy code. You can change it as per your need like to keep a watch over file uploading (to limit upload file size), or intimating admin via mail when log file size exceed etc.

This article will also help you to understand need of interfaces over abstract classes.

myFileWriter.java

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;

/**
 * Copyright (C) 2011 Amit Gupta
 * You are free to redistribute the code or its modified version
 * by giving the credit to original code.
 *
 * This code is useful to understand the concept and
 * importance of anonymous class. And how to reuse java classes
 * @author Amty
 *
 */
public class myFileWriter extends OutputStreamWriter //implements myProgressListener
{
	myProgressListener mpl;
	long pBytesWrite;
	public myFileWriter(OutputStream arg0) {
		super(arg0);
		//mpl=this;
		pBytesWrite=0;
	}

	@Override
	public void write(int c) throws IOException {
		super.write(c);
		pBytesWrite++;
		if(mpl != null){
			mpl.update(pBytesWrite);
		}
	}

	public void setProgressListener(myProgressListener mpl){
		this.mpl = mpl;
	}
	//public void update(long pBytesRead, long pContentLength, int pItems){}

	public static void main(String[] args) throws Exception{
		FileOutputStream fos = new FileOutputStream("D:\\article-stack\\amtyOutput.txt");
		FileInputStream fis = new FileInputStream("D:\\article-stack\\amtyInput.txt");
		InputStreamReader isr = new InputStreamReader(fis, "UTF8");
		Reader in = new BufferedReader(isr);

		myProgressListener mpltest = new myProgressListener() {

			@Override
			public void update(long pBytesWrite) {
				System.out.println(pBytesWrite + "Bytes are written");
			}
		};

		myFileWriter mfw = new myFileWriter(fos);
		mfw.setProgressListener(mpltest);
		int ch;
	    while ((ch = in.read()) > -1) {
	       mfw.write((char) ch);
	    }
	    in.close();
	    mfw.close();
	}
}

myProgressListener.java

public interface myProgressListener {
	public void update(long pBytesWrite);
}

Output:

74381Bytes are written
74382Bytes are written
74383Bytes are written
:
77710Bytes are written
77711Bytes are written
77712Bytes are written
77713Bytes are written
77714Bytes are written

Here is attached code for your reference.
This area is protected to registered users only.

Interface vs Abstract classes, a practical difference

May 16th, 2011 510 views 2 comments

An Interface: This is a collection of methods. They have no definition whatsoever and their function is determined by the class that implements them. An example of an interface is a List. All lists (ArrayList, LinkedList) have add() and remove() methods because they implement the List interface, which demands them.

public interface List {
    public void add (Object o);
    public void remove (Object o);
}

public class MyList implements List {

    public void add (Object o) {
       // I must implement this method because of the interface List
    }

    public void remove (Object o) {
       // I must implement this method because of the interface List
    }
}

The implements keyword is used by a class to indicate that it is going to implement the methods demanded by an interface. A class can implement as many interfaces as it likes; the only requirement is that it provides a definition for each of those methods. It can also be defined as abstract and rely upon its subclasses to define some. Then, those methods are just like the abstract methods of any other abstract class.

Take an example of List interface. Most methods don’t care what kind of list they get; they just want to know that the object supports the common list methods. In the statement:

List<String> myList = new ArrayList();

You are creating an actual ArrayList object, but hiding it’s implementation under the List interface. Then, later, if you decide to use a LinkedList instead, you don’t have to change all of your code since it is also implementing the List interface.

Interface is programming structure where you define your functions/services that you want to expose to public or other modules. Kind of a contract where you promise that you are providing some functionalities or services, but hiding the implementation so that implementation can be changed without affecting your contract.

Abstract class is a partially implemented class and it has no real meaning other than serving as a parent for multiple child classes those with real meaning. Abstract class is special parent class that provides default functionalities to multiple child classes. It is created as abstract because of unavailability of suitable concrete parent class.

The extends keyword is used by a class to indicate that it is going to add functionality to some existing class. If the parent class is abstract, the extending class must implement any of those abstract methods in the parent.

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

Points to remember:

Interfaces and interface methods are implicitly abstract even if not declared as so. So there is no need to explicitly specify it.

Below code is senseless.

public abstract interface <interface-name> {
 public abstract void <function-name>(...);
}

Other differences

  1. An interface can extend any number of interfaces.
  2. A class can optionally extend exactly one class; it extends Object if you don’t specify anything.
  3. A class can implement any number of interfaces.
  4. Any class can be declared abstract.
  5. Any class that has abstract methods must be declared abstract.
  6. Any class that extends an abstract class or implements an interface must implement all abstract and interface methods or else must be declared abstract.

Practical usages and designs to inscribe clear picture in your mind

  • Interfaces allow the creation of proxies that encapsulate a concrete class. This is used extensively by frameworks in order to intercept method calls to the concrete class (e.g., for starting a transaction before the method is executed or to write to the log).
  • Keep watch over upload file size
  • Creating progress monitor in Java
  • If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

I am designing an java based API. I’ll share its design once it is completed. It’ll help you to understand the concepts of interfaces, abstract classes, enum, modifers, creating your own data type, delegators, OOPS concept etc. with example. So keep reading….

Sequence Diagrams

May 6th, 2011 258 views No comments

Time is running out for college students, they are hurried in preparing reports. I already had written article over UML Types with the purpose of helping them in preparing reports.

Diagrams, statistics, and UI designs are most important part of the project report. Fewer people are interested to read whole the report. They analyze your project from the diagrams inscribed in your report.

Here, one more short & sweet tutorial about Sequence diagram which helps you to understand the flow of your project. Sequence diagram helps you to know the sequence of interaction between various components(like classes, database etc). You can clearly show the steps of a complete process with these diagrams.

Please refer below guide for your reference. You can save it to read offline.



If you need more examples for your study, just comment here…

log4j fileappender – Advanced

April 28th, 2011 853 views No comments

As you seen in my last log4j article about log4j appenders – power of logging, There are various appenders provided by log4j. We discussed few.

Here i am discussing 2 more appenders which help us to divide log file as per the time basis or the size of file. It is very useful to take backup of old logs or to track logs.

DailyRollingFileAppender

Creates file per day. You can keep the backup of old files. It extends FileAppender.

Sample property file entry

log4j.appender.DAILY=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DAILY.File=${user.home}/daily.log
log4j.appender.DAILY.DatePattern='.'&nbsp;yyyy-MM-dd-HH-mm

You can define date pattern as follows

DatePattern Rollover schedule Example
'.'yyyy-MM Rollover at the beginning of each month At midnight of May 31st, 2002 /foo/bar.log will be copied to /foo/bar.log.2002-05. Logging for the month of June will be output to /foo/bar.log until it is also rolled over the next month.
'.'yyyy-ww Rollover at the first day of each week. The first day of the week depends on the locale. Assuming the first day of the week is Sunday, on Saturday midnight, June 9th 2002, the file /foo/bar.log will be copied to /foo/bar.log.2002-23. Logging for the 24th week of 2002 will be output to /foo/bar.log until it is rolled over the next week.
'.'yyyy-MM-dd Rollover at midnight each day. At midnight, on March 8th, 2002, /foo/bar.log will be copied to /foo/bar.log.2002-03-08. Logging for the 9th day of March will be output to /foo/bar.log until it is rolled over the next day.
'.'yyyy-MM-dd-a Rollover at midnight and midday of each day. At noon, on March 9th, 2002, /foo/bar.log will be copied to /foo/bar.log.2002-03-09-AM. Logging for the afternoon of the 9th will be output to /foo/bar.log until it is rolled over at midnight.
'.'yyyy-MM-dd-HH Rollover at the top of every hour. At approximately 11:00.000 o’clock on March 9th, 2002, /foo/bar.log will be copied to /foo/bar.log.2002-03-09-10. Logging for the 11th hour of the 9th of March will be output to /foo/bar.log until it is rolled over at the beginning of the next hour.
'.'yyyy-MM-dd-HH-mm Rollover at the beginning of every minute. At approximately 11:23,000, on March 9th, 2001, /foo/bar.log will be copied to /foo/bar.log.2001-03-09-10-22. Logging for the minute of 11:23 (9th of March) will be output to /foo/bar.log until it is rolled over the next minute.

Sample java code

PatternLayout pl;
FileAppender as_appender = new DailyRollingFileAppender(pl, logFilePath, "'.'dd-MM-yyyy");

RollingFileAppender

RollingFileAppender extends FileAppender, to backup the log files when they reach a certain size.
Sample property file entry

log4j.appender.ROLLING=org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING.File=${user.home}/rolling.log
log4j.appender.ROLLING.MaxFileSize=1MB

log4j.appender.ROLLING.MaxBackupIndex=5

Description

For above property file, when log file size will reach up to 1 MB then new log file with the same name will be created. And current log file will be renamed to <old name>.1 and so on. MaxBackupIndex says how many backup files has to be kept.

FileAppender

FileAppender append logs to every time to specified log file.

Sample property file entry

log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${user.home}/out.log
log4j.appender.FILE.Append=false

Description

Append property is set to true by default. If it is true then logs will be append every time else the previous logs will be overwritten every time.