Archive

Posts Tagged ‘examples’

Solved SQL exercise for novice to experts

April 28th, 2012 751 views No comments
database

There is an example , some questions, and corresponding queries. These queries are written in Oracle. But it’ll give you clear logic about how to write SQL queries in any sort of SQL like DB2, TSQL, MySql etc. I constructed this exercise during my training. Although Some questions do not having solution. So those will be an exercise for you. If i get time then will update them later.

First of all you need to create tables in your database. Use above “ready to use” code.

--################ DDL #######################
create table stock
(item_no varchar2(5) primary key,
item_name varchar2(15) not null,
Quoted_price number(6,2),
Category varchar2(5));

create table category
(c_code varchar2(5) primary key,
c_name varchar2(15) not null unique);

 alter table stock
 add constraint s_fk Foreign key(category) references Category(c_code) on  delete cascade ;

create table purchase
(p_no varchar2(5) PRIMARY KEY,
item_no varchar2(5) references stock(item_no),
p_price number(6,2) not null,
p_date date,
p_qty number(3));

 create table sale
 (s_no varchar2(5) PRIMARY KEY,
 item_no varchar2(5) references stock(item_no),
 s_price number(6,2) not null,
 s_date date,
 s_qty number(3),
 cust_name varchar2(15)) ;

Now you need to populate your tables with some dummy but meaningful data. So you can test your queries.

--############### DML #####################
BEGIN
insert into CATEGORY VALUES('C101','FOOD');
insert into CATEGORY VALUES('C102','SPORT');
insert into CATEGORY VALUES('C103','ELEX');
insert into CATEGORY VALUES('C104','CLOTH');
insert into CATEGORY VALUES('C105','OTHER');
END
;

BEGIN
insert into stock values('i101','BREAD',17,'C101');
insert into stock values('i102','BUTTER',67,'C101');
insert into stock values('i103','BISCUIT',87,'C101');
insert into stock values('i104','GEM',56,'C101');
insert into stock values('i105','CHOCOLATE',17,'C101');
insert into stock values('i106','EGG',5,'C101');
insert into stock values('i107','SALT',8,'C101');
insert into stock values('i108','BALL',27,'C102');
insert into stock values('i109','BAT',217,'C102');
insert into stock values('i110','FOOTBALL',117,'C102');
insert into stock values('i111','WICKETS',97,'C102');
insert into stock values('i112','CAP',111,'C102');
insert into stock values('i113','TSHIRT',1011,'C104');
END

BEGIN
insert into PURCHASE values('P101','i101',15,'01-nov-2008',16);
insert into PURCHASE values('P102','i102',65,'01-nov-2008',34);
insert into PURCHASE values('P103','i103',85,'01-nov-2008',12);
insert into PURCHASE values('P104','i104',56,'01-nov-2008',35);
insert into PURCHASE values('P105','i105',17,'01-nov-2008',23);
insert into PURCHASE values('P106','i106',4,'01-nov-2008',34);
insert into PURCHASE values('P107','i107',7,'01-oct-2008',23);
insert into PURCHASE values('P108','i108',25,'01-oct-2008',56);
insert into PURCHASE values('P109','i109',201,'01-sep-2008',67);
insert into PURCHASE values('P110','i110',100,'01-oct-2008',54);
insert into PURCHASE values('P111','i111',90,'01-mar-2008',23);
insert into PURCHASE values('P112','i112',100,'01-feb-2008',12);
insert into PURCHASE values('P113','i113',800,'01-mar-2008',6);
END;

BEGIN
insert into SALE values('S101','i101',19,'01-DEC-2008',6,'AMIT');
insert into SALE values('S102','i102',65,'02-DEC-2008',14,'ROHIT');
insert into SALE values('S103','i103',86,'05-DEC-2008',2,'AMIT');
insert into SALE values('S104','i104',63,'27-nov-2008',12,'SHYAM');
insert into SALE values('S105','i105',21,'16-nov-2008',3,'KAMAL');
insert into SALE values('S106','i106',6,'13-DEC-2008',4,'PREETI');
insert into SALE values('S107','i103',88,'17-nov-2008',2,'RAHUL');
insert into SALE values('S108','i104',57,'19-nov-2008',5,'RAHUL');
insert into SALE values('S109','i105',19,'28-nov-2008',2,'SOHAN');
insert into SALE values('S110','i111',115,'12-DEC-2008',3,'SHAALU');
insert into SALE values('S111','i112',145,'17-NOV-2008',8,'SHAALU');
insert into SALE values('S113','i113',1300,'17-FEB-2008',2,'UMESH');
insert into SALE values('S112','i112',120,'01-MAR-2008',4,'SHYAM');
insert into SALE values('S114','i112',130,'06-OCT-2008',2,'KAMAL');
END;

Now this is the time to practice. See given examples and solved queries. First try all queries given below. Then login to this article-stack to find out their solution.

1) LIST OF ITEMS OF ‘SPORT’ CATEGORY

This area is protected to registered users only.

2) LIST OF ITEMS OF ‘SPORT’ & ‘FOOD’ CATEGORIES
This area is protected to registered users only.

3) LIST OF ITEMS OF ‘SPORT’ CATEGORY, whose PRICE IS greater than average PRICE of all items.
This area is protected to registered users only.

4) LIST OF SOLD ‘SPORT’ ITEMS.
This area is protected to registered users only.

5) LIST OF ITEMS SOLD TO ‘AMIT’.
This area is protected to registered users only.

6) LIST OF ITEMS SOLD IN LAST WEEK.
This area is protected to registered users only.

7) LIST OF ‘FOOD’ ITEMS SOLD IN LAST WEEK.
This area is protected to registered users only.

8 ) LIST OF ITEMS THOSE ARE SOLD TO GAIN MAX PROFIT.
This area is protected to registered users only.

9) LIST OF ‘FOOD’ ITEMS PURCHASED 1 MONTH AGO.
This area is protected to registered users only.

10) LIST OF ‘FOOD’ ITEMS THOSE ARE NOT SOLD IN LAST WEEK.
This area is protected to registered users only.

11) LIST OF CUSTOMERS WHO PURCHASED ‘FOOD’ ITEMS BUT NOT ‘SPORT’ OR ‘CLOTH’ ITEMS.
This area is protected to registered users only.

12) LIST OF CUSTOMERS WHO PURCHASE ITEMS OF SINGLE CATEGORY ONLY.
This area is protected to registered users only.

13) LIST OF SOLD ITEMS WHOESE QUOTED PRICE > 30.
This area is protected to registered users only.

14) LIST OF ITEMS OF ‘SPORT’ CATEGORY, whose PRICE IS greater than average PRICE of all items.
This area is protected to registered users only.

15) LIST OF SOLD ‘SPORT’ ITEMS.
This area is protected to registered users only.

Type of Software Testing, illustrated

April 1st, 2012 70 views 2 comments

We generally use most of the testing strategy described here. But we are not aware with their name. So know which testing you use and impress your seniors.

Type of software Testing

Module or unit testing – when programmer/developer checks his program himself. It includes boundary condition check, program reviews, error handling, formal verification, all permutation of input/output data etc.

Integration Testing – Testing after integrating all or some modules. It can adopt approach of

  • Bottom –up – Test smaller parts first è merge them è test large part è merge other smaller part & test again and so on.
  • Top –down – Take bigger part & test it. Add subsequent part and test it until whole module is tested.
  • Or both (Sandwich Testing)
  • Big Bang – Form a large part from adding similar and smaller parts together. Test it.

The main advantage of the Bottom-Up approach is that bugs are more easily found. With Top-Down, it is easier to find a missing branch link.

Smoke Testing – A quick-and-dirty test that the major functions of a piece of software work without bothering with finer details. Like a check list.

Sanity Testing –just opposite of smoke testing, It asks for deep testing. It include Regression testing.

  • Regression Testing – break & test. It requires after integration or sanity testing to find out the bug and repair it.

Acceptance Testing

  • Pilot testing – Testing in production like environment. Simulation.
  • Parallel testing
  • Alpha test – Tested by customer under the supervision of the developer at the developer’s site
  • Beta test – Tested by customer at his or her own site without the developer being present

Functional testing – Testing functionality of a system. It includes all tests other than performance test.

Performance Testing – run-time performance of software

  • Stress test – checked to see how well it deals with abnormal resource demands
  • Volume/load test – test with mass amount of input/requests
  • Configuration test (hardware & software)
  • Compatibility – like testing a site on various browsers, or running software on many OS
  • Security tests – finding security hall, like prevention from SQL injection, Ajax bomb, cross site scripting etc.
  • Timing tests – whether system is able to respond properly with in defined time
  • Recovery tests – ability to recover from failures. Whether it can recover lost data. Whether it is persistent and crash safe.

Some other performance tests are

  • Environmental tests
  • Quality tests
  • Maintenance tests
  • Documentation tests
  • Human factors test
  • Benchmark test

Black Box Testing – Testing, conducted by external entity, without having any knowledge of system/application

White Box Testing – Testing by external entity who has list of test cases or has prior knowledge of system/application

Mystery behind equals(), ==, and hashcode

March 31st, 2012 25 views No comments

equals() of Object class and logic operator “==” puzzle me always. I decided to write this article after finding many arguments and questions on the same topic.

Lets start with a simple example;

		System.out.println("Amit".hashCode());
		System.out.println("Amit".hashCode());
		String name = "Amit";
		System.out.println(name.hashCode());
		System.out.println(name == "Amit");
		System.out.println(name.equals("Amit"));
		String[] nameList = {"Amit","Arti"};
		System.out.println(nameList[0].hashCode());
		System.out.println(name == nameList[0]);
		System.out.println(name.equals(nameList[0]));
		nameList[0] = new String("Amit");
		System.out.println("After initializing new memory location");
		System.out.println(name == nameList[0]);
		System.out.println(name.equals(nameList[0]));
Output:


2044535
2044535
2044535
true
true
2044535
true
true
After initializing new memory location
false
true

Conclusion:

  1. Logical operator “==” checks for reference not for value. So name == nameList[0] gives false after nameList[0] = new String(“Amit”);
  2. equals() checks for value not for reference.
  3. JVM allocates same memory location for same constants. So 2 different String variables having same value points to same memory location.
  4. hashCode() returns same value for two objects if obj1.equals(obj2) is true.

From Java Doc, the equals() method must exhibit the following properties:

  • Symmetry: For two references, a and b, a.equals(b) if and only if b.equals(a)
  • Reflexivity: For all non-null references, a.equals(a)
  • Transitivity: If a.equals(b) and b.equals(c), then a.equals(c)
  • Consistency with hashCode(): Two equal objects must have the same hashCode() value

* o.equals(null) must always return false.

Warning:

If A equlas B then A.hashcode must be equal to B.hascode but if A.hashcode equals B.hascode it does not mean that A must equals B.

So its better to override hashCode() to generate your own hashCode().

Short version of generating hash from Josh Bloch’s “Effective Java”;

  1. Create a int result and assign a non-zero value.
  2. For every field tested in the equals-Method, calculate a hash code c by:
    • If the field f is a boolean: calculate (f ? 0 : 1)
    • If the field f is a byte, char, short or int: calculate (int)f
    • If the field f is a long: calculate (int)(f ^ f( >>> 32)
    • If the field f is a float: calculate Float.floatToIntBits(f)
    • If the field f is a double: calculate Double.doubleToLongBits(f) and handle the return value like every long value
    • If the field f is an object: Use the result of the hashCode() method or 0 if f is a null referenence.
    • If the field f is an array: See every field as separate element and calculate the hash value in a recursive fashion and combine the values as described next.
  3. Combine the hash value c with result with:
  4. result = 37 * result + c

  5. Return result

Or use helper classes EqualsBuilder and HashCodeBuilder from the Apache Commons Lang library. An example:

public class Person {
	private String name;
	private int age;
	// ...

	public int hashCode() {
		return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
		// if deriving: appendSuper(super.hashCode()).
		append(name).
		append(age).
		toHashCode();
	}

	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (obj == this)
			return true;
		if (obj.getClass() != getClass())
			return false;

		Person rhs = (Person) obj;
		return new EqualsBuilder().
		// if deriving: appendSuper(super.equals(obj)).
		append(name, rhs.name).
		append(age, rhs.age).
		isEquals();
	}
}

* The downside of this API is the cost of object construction every time you call equals and hashcode (unless your object is immutable and you precompute the hash)

System.identityHashCode(): Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object’s class overrides hashCode(). It means if you don’t override hashCode() then System.identityHashCode(obj) == obj.hashCode() is true.

Collection

When using a hash-based Collection or Map such as HashSet, LinkedHashSet, HashMap, Hashtable, or WeakHashMap, make sure that the hashCode() of the key objects that you put into the collection never changes while the object is in the collection. The bulletproof way to ensure this is to make your keys immutable.

IdentityHashMap vs HashMap
Although IdentityHashMap and HashMap follows same data structure but IdentityHashMap compares keys as;

if(System.identityHashCode(k1) == System.identityHashCode(k1)){
	if(k1 == k2){
	:
	}
}

while HashMap compares keys as;

k1 == null ? k2 == null : k1.equals(k2)

It means

  • IdentityHashMap is faster than HashMap. It doesn’t check for quality of values but their hash code and reference.
  • HashMap can use you override equals()
  • If you don’t override equals() then both Map should work exactly same.

So when IdentityHashMap should be used?

The documentations says:

A typical use of this class is topology-preserving object graph transformations, such as serialization or deep-copying. To perform such a transformation, a program must maintain a “node table” that keeps track of all the object references that have already been processed. The node table must not equate distinct objects even if they happen to be equal. Another typical use of this class is to maintain proxy objects. For example, a debugging facility might wish to maintain a proxy object for each object in the program being debugged.

If you have some more points on this topic please comment.

Nice trick to get_the_ID() in wp_head()

March 16th, 2012 30 views No comments

If you call the_ID() or get_the_ID() in wp_head(), you’ll get nothing. Sometimes you require post id to do a specific action like including post id in meta tag or to count number of visits etc.
Well this trick can help you. Let’s see how;

add_action('wp_head', 'doSomethingInHead');
function doSomethingInHead() {
		:
		echo "#amtypostid#;
		:
	}
}

Now use jQuery functions find() & replaceWith() to replace this constant value with post id. But how will you get post id and when will you replace it?

Note: find() search for tag. So echo your constant in some tag. Otherwise create a new dummy tag. replaceWith() will replace all value with post id along with tag.

To get post id of current post:

add_action('the_content', 'getId');
function getId($content) {
	//prepare your jquery script here.
	//use the_ID() or get_the_ID() to get current post id.
	//attach it with your content and return.
	return $content . $myscript;
}

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 …

Postmortem of notify(), notifyAll(), and wait()

March 10th, 2012 42 views No comments

notify() or notifyAll(), and wait()

notify() or notifyAll(), and wait() must be in a synchronized block for the object you are waiting on. So the following codes are correct;

	public synchronized void method(){
		wait(); //or this.wait();
	}

	public void method(){
		synchronized(this){
			wait(); //or this.wait();
		}
	}

	public void method(){
		synchronized(a){
			a.wait();
		}
	}

	public synchronized void method(){
		synchronized(a){
			wait();
		}
	}
  1. But following code generates run time exception java.lang.IllegalMonitorStateException
  2. 	public void method(){
    		synchronized(a){
    			wait();
    		}
    	}
    
    	public void method(){
    		synchronized(a){
    			synchronized(this){
    				wait(n);
    			}
    		}
    	}
    
    	public synchronized void method(){
    		synchronized(a){
    			wait(n);
    		}
    	}
    
  3. If you change synchronized object inside synchronized block notify() or notifyAll(), and wait() will give run time exception java.lang.IllegalMonitorStateException.
  4. synchronized(a){
    	a = 55;
    	a.wait();
    }
    

    In above example, you are waiting on new copy of a while synchronizing on old copy of a. Below code doesn’t generate exception.

    synchronized(a){
    	a.wait();
    	a = 55;
    }
    

    Because you are waiting on same copy of a, you are synchronizing on. Then you are changing a.

  5. Thread.sleep(1000) asks current thread to wait till specified period. It doesn’t bother about locks and all. So it doesn’t releases lock. On the other hand, wait() releases lock and acquires lock again once notify() or notifyAll() get called on same object or specified time is over.
  6. 		public void acquire(){
    			synchronized(a){
    				print("acquire()");
    				try{
    					a.wait(5000);
    					print("I have awoken");
    					print("" + a);
    				}catch(Exception e){
    					e.printStackTrace();
    				}
    			}
    			print("Leaving acquire()");
    		}
    
    		public void modify(int n){
    			print("Entered in modify");
    			synchronized(a){
    				try{
    					a.wait(5000);
    					this.a=n;
    					print("new value" + a);
    				}catch(Exception e){
    					e.printStackTrace();
    				}
    			}
    		}
    

    Output:
    2012-03-06 19:51:27.969 :: A: acquire()
    2012-03-06 19:51:27.969 :: B: Entered in modify
    2012-03-06 19:51:32.992 :: B: new value97
    2012-03-06 19:51:32.992 :: A: I have awoken
    2012-03-06 19:51:32.992 :: A: 97
    2012-03-06 19:51:32.992 :: A: Leaving acquire()

    Explanation:

    1. Thread A: enters into monitor of a.
    2. Thread B: waits to get entered into monitor of a.
    3. Thread A: meets to a.wait(5000). So it releases lock on a.
    4. Thread B: enters into monitor a. waits and acquires lock on a.
    5. Thread A: try to acquire released lock. But it is already acquired by Thread B. So it waits.
    6. Thread B: changes value of a.
    7. Thread A: acquire lock on a. It prints new value of a.
  7. On the other hand, if I use Thread.sleep(n) in place of wait(n)
  8. public void acquire(){
    			synchronized(a){
    				print("acquire()");
    				try{
    					//a.wait(5000);
    					Thread.sleep(5000);
    					print("I have awoken");
    					print("" + a);
    				}catch(Exception e){
    					e.printStackTrace();
    				}
    			}
    			print("Leaving acquire()");
    		}
    

    Output:
    2012-03-06 20:02:39.395 :: A: acquire()
    2012-03-06 20:02:39.395 :: B: Entered in modify
    2012-03-06 20:02:44.418 :: A: I have awoken
    2012-03-06 20:02:44.418 :: A: 10
    2012-03-06 20:02:44.418 :: A: Leaving acquire()
    2012-03-06 20:02:49.427 :: B: new value97

    Explanation
    When Thread A meets to sleep(n) it doesn’t release lock.

  9. The problem with calling wait() and notify() on the empty string, or any other constant string is, that the JVM/Compiler internally translates constant strings into the same object. That means, that even if you have two different MyWaitNotify instances, they both reference the same empty string instance. This also means that threads calling wait() on the first MyWaitNotify instance risk being awakened by notify() calls on the second MyWaitNotify instance.
  10. If there are multiple threads waiting on same object, notify() can awake any thread randomly. It arises the condition that some threads never get awaken and some always get awaken. This situation is called Starvation. Sometimes some greedy threads don’t release resources (call sleep() instead of wait() etc). It also force other threads to wait. Some threads increase their priority to get served first to CPU, it also force low priority threads to wait for long. These all conditions where any thread need to wait very long is called Starvation.

Read How Nested Monitor can cause DeadLock for a cause of wait().

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.

Synchronization – solve the mystery

March 9th, 2012 11 views No comments

In continuation of need & use of synchronization, I am writing this article to reduce overhead of QA sites to solve basic problems related to multithreading. It’ll also clear the actual picture of synchronized.

Lets start with a small program.

public class SynchoTest {
		private Integer a = 10;

		public void acquire(){
			synchronized(a){
				print("acquire()");
				try{
					Thread.sleep(10000);
					print("I have awoken");
					print("" + a);
				}catch(InterruptedException e){
					e.printStackTrace();
				}
			}
			print("Leaving acquire()");
		}

		public void modify(int n){
			this.a=n;
			print("new value" + a);
		}

		public void display(){
			print("a=" + a);
		}
		:
}

In above program, I am starting 2 threads. Thread A calls method acquire() and Thread B calls modify(97). Remember it, I’ll use it longer in this article.

Output:

2012-03-06 07:48:10.038 :: A: acquire()
2012-03-06 07:48:10.038 :: B: new value97
2012-03-06 07:48:10.038 :: C: a=97
2012-03-06 07:48:20.054 :: A: I have awoken
2012-03-06 07:48:20.054 :: A: 97
2012-03-06 07:48:20.054 :: A: Leaving acquire()

What have you noticed in above output? yes. When A entered in his critical section, a’s value was 10. But when it went to print it, B already changed a’s value. So A prints new value of a ie 97.

Now do some small changes

		public void modify(int n){
			print("Entered in modfy");
			synchronized(a){
				this.a=n;
				print("new value" + a);
			}
		}

Output:

2012-03-06 07:51:46.19 :: A: acquire()
2012-03-06 07:51:46.19 :: B: Entered in modfy
2012-03-06 07:51:46.19 :: C: a=10
2012-03-06 07:51:56.206 :: A: I have awoken
2012-03-06 07:51:56.206 :: A: 10
2012-03-06 07:51:56.206 :: A: Leaving acquire()
2012-03-06 07:51:56.206 :: B: new value97

This time everything is fine. why so?

  1. If there are many synchronized blocks on same object and any thread has entered into one of the synchronized block then no other thread can enter into any synchronized block on the same object. synchronized checks happens-before condition then either it acquire lock or waits until another thread releases lock or get interrupted.
  2. synchronized blocks on different-2 objects has no interrelation. Any thread can enters into any synchronized block. Synchronized(a) and synchronized(this) are also non-related.

Lets play with some more code:

		public void modify(int n){
			this.a=n;
			print("new value" + a);
		}
		public void display (int n){
			print("a=" + a);
			synchronized(a){
				try{
					Thread.sleep(1000);
					print("a=" + a);
				}catch(InterruptedException e){
					e.printStackTrace();
				}
			}
		}

Output:

a=34
new value45
a=45

Code Postmortem

Thread 1 inserts into synchronized block of display(). It takes lock on a. Thread 2 insert into modify(). But there is no new synchronized block. So it doesn’t check whether some thread has taken lock on a before. So it allows modification in a. Thus display() prints modified value of a.

If I makes modify() synchronized even, it doesn’t change the output.

		public synchronized void modify(int n){
			this.a=n;
			print("new value" + a);
		}

Because making modify() synchronized ensures that no more than 1 thread can call modify() at a time. It is not related to synchronized(a).

Lets read some more important points about synchronization.

  • If you are starting threads on 2 separate objects then there is no relation between those threads because both objects use different memory address. Hence their synchronized blocks are also not related.
  • synchronized(a) should not contain any statement modifying value of a. You’ll notice it when you’ll read about wait(),notify() etc. However it is allowed.
  • Synchronizing a method or using synchronized(this) everywhere in class is not good practice. Because it blocks all threads to access any synchronized block. Taking lock on necessary field is always a good practice.

Well!! it is not end keep reading of Postmortem of notify(), notifyAll(), and wait()

Theory aparts – important multithreading terms

March 8th, 2012 16 views No comments

Monitor -
it ensures that only one thread can enter in its critical section. It is associated with every object in java. Synchronized acquires a monitor for a thread. It is also called intrinsic or monitor lock.

method(
	//some statement
	synchronized(this){
		//some statement
	}
}

method2(
	synchronized(this){

	}
	//Some statement
}

Starvation –
If there are multiple threads waiting on same object, notify() can awake any thread randomly. It arises the condition that some threads never get awaken and some always get awaken. This situation is called Starvation. Sometimes some greedy threads don’t release resources (call sleep() instead of wait() etc). It also force other threads to wait. Some threads increase their priority to get served first to CPU, it also force low priority threads to wait for long. These all conditions where any thread need to wait very long is called Starvation.

Slipped condition –
from the time a thread has checked a certain condition until it acts upon it, the condition has been changed by another thread.

method(
	:
	while(amount < required){
		synchronized(filler){
			filler.notify();
		}
		wait();
	}
	amount -= required;
	:
}

Race condition -
Race conditions occur only if multiple resources are accessing the same resource, and one or more of the threads write to the resource. If multiple threads read the same resource race conditions do not occur.

DeadLock –
When all threads waiting outside the door closed by another thread. And this chain is either circular or ending on resource which cant be assigned to any thread due to some reasons. (Might be because it is held by some devil thread).

Nested Monitor Lockout –
Lock inside lock. wait() releases one of the lock but not all. It results deadlock.

method(
	synchronized(this){
		while(flag){
			synchronized(a){
				a.wait();
			}
		}
	}
}

method2(
	synchronized(this){
		flag = false;
		synchronized(a){
			a.notify();
		}
	}
}

Guarded/loop/spin lock –
Loop & Wait until the condition becomes false.

while(condition){
      wait();
}

Reentrance Lock –
When one thread calls a synchronized block inside from another synchronized block on the same object. (when a thread reenters in its monitor)

 public synchronized outer(){
    inner();
  }

  public synchronized inner(){
    //do something
  }

It is allowed for same thread. But if 2 separate threads access 2 separate synchronized blocks, then one of them will have to wait.

Daemon Thread –
A thread that has no other role in life than to serve others. For example Timer thread. JVP stop executing a program if only daemon. To make a thread daemon in java just call setDaemon(true) on a thread.

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 :)

How Nested Monitor can cause DeadLock

March 8th, 2012 24 views No comments

I tried to code many programs which can help me to understand race condition, starvation, and deadlock practically. But I failed. Unknowingly I made this program which creates deadlock due improper synchronization.

	public synchronized void acquire() throws Exception{
		Thread.sleep(5000);
		synchronized(lock){
				wait(1);
		}
	}

	public synchronized void modify() throws Exception{
		Thread.sleep(5000);
		synchronized(lock){
				wait(1);
		}
	}

I added a tracer who explained me why both threads were getting blocked. You can integrate separate java thread tracer with your other threading programs.

Refer the output below to understand how improper nested monitoring took lock.

Output:

A:8
B:9
2012-03-07 19:54:57.625 :: [A:RUNNABLE,B:RUNNABLE]
2012-03-07 19:54:57.788 :: [A:RUNNABLE,B:BLOCKED]
2012-03-07 19:54:57.804 :: [A:TIMED_WAITING,B:BLOCKED]
2012-03-07 20:54:59.888 :: [A:RUNNABLE,B:BLOCKED]
2012-03-07 19:55:02.787 :: [A:TIMED_WAITING,B:TIMED_WAITING]
2012-03-07 19:55:02.788 :: [A:BLOCKED,B:TIMED_WAITING]
2012-03-07 19:55:07.787 :: [A:BLOCKED,B:RUNNABLE]
2012-03-07 19:55:07.787 :: [A:BLOCKED,B:BLOCKED]
Both threads are blocked
Deadlock detected in
Thread id: 9
Thread id: 8

Proper synchronization:

	public void acquire() throws Exception{
		Thread.sleep(5000);
		synchronized(lock){
				lock.wait(1);
		}
	}

	public void modify() throws Exception{
		Thread.sleep(5000);
		synchronized(lock){
				lock.wait(1);
		}
	}

You can solve this deadlock in another way as well. Its up to your need.

CyclicBarrier – A traffic policeman

March 8th, 2012 26 views No comments

CyclicBarrier = Cycle + Barrier


CyclicBarrier just force threads to wait until their count reached to N. Then release them to run on a wide road. And force next N threads to wait. and so on.

Example Program:

public class CycleBarrierDemo implements Runnable{
	CyclicBarrier controller;

	CycleBarrierDemo(){
		controller = new CyclicBarrier(1);
	}
	@Override
	public void run() {
		try{
			Thread.sleep(100);
			System.out.println(controller.getNumberWaiting());
			System.out.println(Thread.currentThread().getName() + " has been arrived");
			controller.await();
			System.out.println(Thread.currentThread().getName() + " has been Passed");
		}catch(Exception bbx){
			System.out.println(bbx.getMessage());
		}
	}
	 public static void main(String[] argc){
		 CycleBarrierDemo cd = new CycleBarrierDemo();
		 Thread A = new Thread(cd,"A");
		 Thread B = new Thread(cd,"B");
		 Thread C = new Thread(cd,"C");
		 Thread D = new Thread(cd,"D");

		 A.start();
		 B.start();
		 C.start();
		 D.start();
	 }
}

Output: [When barrier size was 1]

0
A has been arrived
A has been Passed
0
B has been arrived
B has been Passed
0
C has been arrived
C has been Passed
0
D has been arrived
D has been Passed

Output:[When barrier size was 2]
A has been arrived
1
B has been arrived
A has been Passed
B has been Passed
0
C has been arrived
1
D has been arrived
C has been Passed
D has been Passed

java multithreading – join() 2 minute story

March 8th, 2012 22 views No comments

I prefer not to go theory. So lets start practical


Statement 1
Statement 2

  1. If Statement 1 is a thread statement then Statement 2 can be executed any time. It doesn’t wait Statement 1 to be completed.
  2. If Statement 1 is simple statement then Statement 2 can’t be executed until Statement 1 is completed.
  3. If Statement 1 is a thread statement calling join() then Statement 2 can’t be executed until Statement 1 is completed.
  4. If Statement 1 is a thread statement calling join(n) then Statement 2 can’t be executed until Statement 1 is completed or time n is over.

Study with program

package Simple;

public class JoinDemo extends Object {
	  public static Thread createThread(String name, long napTime) {
	    final long sleepTime = napTime;

	    Runnable r = new Runnable() {
	        public void run() {
	          try {
	            print("in run() - entering");
	            Thread.sleep(sleepTime);
	          } catch ( InterruptedException x ) {
	            print("interrupted!");
	          } finally {
	            print("in run() - leaving");
	          }
	        }
	      };

	    Thread t = new Thread(r, name);
	    t.start();

	    return t;
	  }

	  private static void print(String msg) {
	    String name = Thread.currentThread().getName();
	    System.out.println(name + ": " + msg);
	  }

	  public static void main(String[] args) throws Exception{
	    Thread[] t = new Thread[2];
	    t[0] = createThread("thread A", 2000);
	    //t[0].join();
	    t[1] = createThread("thread B", 1000);

	  }
	}

Explanation:

When createThread() get called it creates a thread. Thread.sleep() interrupts this thread. So another createThread() call takes place.

    t[0] = createThread("thread A", 2000); //statement 1
    t[1] = createThread("thread B", 1000); //statement 3

Output:
thread A: in run() – entering
thread B: in run() – entering
thread B: in run() – leaving
thread A: in run() – leaving

    t[0] = createThread("thread A", 2000); //statement 1
    t[0].join();                           //statement 2
    t[1] = createThread("thread B", 1000); //statement 3

Output
thread A: in run() – entering
thread A: in run() – leaving
thread B: in run() – entering
thread B: in run() – leaving

In above condition, first t[0] will be completed then statement 3 will be called.

URI vs URL vs URN – Precise difference

January 15th, 2012 147 views 1 comment
    URL is now used as generic term. Although the subtle difference is;

  1. URN: refer the resource(including its location) without specifying how to access it.
  2. URL: it doesn’t refer the resource but its location of resource with method(protocol).
  3. URI: it represents resource as well as its location.
URI vs URL vs URN

URI vs URL vs URN

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.

HashSet vs LinkedHashSet

December 17th, 2011 81 views No comments

What the single difference I mentioned in java collection doodle chart is that iteration order in LinkedHashSet equals to insertion order. If you are interested to know how exactly it works then read this summary.

 

A hash table with chained overflow

A hash table with chained overflow

linked hash table

A linked hash table

A hash table with chained overflow

 

A linked hash table

 

In Hash table hash code is calculated from the element itself and then they are positioned. Since more than one element can have same hashcode so the colliding elements can have same position or bucket. Elements in same bucket are stored in linked list.

In LinkedHashSet, all the elements has extra reference to its next elements. There are 2 linked lists are maintained. One for all the elements of same bucket, another for all the elements stored.

Java Queues – when to use what and why

December 17th, 2011 162 views No comments

Pankaj, gifted a mobile to her sister Arti. she recharged mobile with 94 rs for net connection with the upper limit of 3gb and surfing speed of only 12Kbps. She asked her friend Amit to code an application which queues her all mails and send them in bulk whenever she connects to net. She added more conditions in her need.

  1. Mails must be sending in the same order she wrote. Say, if she wrote 1st mail to Shreya, then to Shruti, then to Shilpi, then whenever she connects net mail must be send to Shreya first and so on. So if net gets disconnected by any reason oldest mails must be dispatched first. (Sequence is important)
  2. If there is a mail marked as important then it must be send before any unimportant mail. (Priority is important)
  3. Her mailing application must be password protected so no one else can send mails from her mobile.(Security is important)
  4. Since her mobile has only 26mb internal memory so if she already had wrote mails up to 3mb she must be asked to connect net first. So the old mails can be dispatched to free disk space.
  5. She must be able to delete a mail which is written but not send yet.
  6. There should be a facility to schedule mails to send on particular time or date.(Scheduling is important)

 

From the first point of Arti’s requirement it was clear that Amit was thinking to go for queue data structure since all the mails has to go in FIFIO order. But he had to select appropriate queue type. Amit created 3 storage areas/variable for this.

  1. Draft Collection: A collection which can store draft mails. It can be any bounded collection. Since the sequence doesn’t matter so any special data structure, like queue/stack, is not required. As soon as Arti click on send button, this mail must be moved from this Draft collection to Mails Collection. Since any mail can be sent or discarded any time so random access would be required. So I am going with ArrayList.
  2. Mail Collection: This collection will store all the mails which are ready to send. Since mails have to be send in the same sequence they were written so Queue is only the option for this. Some more points are here to decide which kind of Queue will fit to this application.
    1. Random access is not required from Mails Queue. So ArrayList doesn’t get fit here.
    2. Important mails must queue up. Amit can do this with PriorityQueue easily. Every mail will have some priority. Whichever mail has higher priority will be pulled ahead in the queue.
    3. If internet is connected and there is no mail in Mails Queue then the application must wait if Arti is writing a mail. This sort of requirement can be fulfilled by some kind of BlockingQueue where producer waits if queue is full and consumer waits if queue is empty. For this application PriorityQueue + BlockingQueue = PriorityBlockingQueue.
    4. We also need scheduling of mails. For this I am taking DelayQueue. Elements from DelayQueue can be accessed only if they are expired. For this need I wrote separate utility who take elements from Scheduled queue once their scheduled time is over and put it to Mails Queue.
  1. MailSend Collection: It fetch mail from Mails Queue whenever internet is connected. Since only one mail can be send at a time so a synchronized collection of size 1 is required. SynchronousQueue fit for this need.

 

So finally,

  1. An ArralyList is required to keep draft mails.
  2. A PriorityBlockingQueue keeps mails ready to send
  3. A DelayQueue keeps scheduled mails. These mails move to PriorityBlockingQueue to send once their scheduled time is over.
  4. A SynchronousQueue hold the mail which is being send.

*This article is copyrighted with all examples and images used in this article. Publication of this article to any where without referencing to original article or to hard copy or any modification in original images or example is strictly prohibited.

 

Queue when to use what

Analysis of processing data file

December 17th, 2011 27 views No comments

I am working on a project where I need to read a text file and for every valid record I have to update database. I also have to write a response/log file. This seems very easy. Although I decided to make a generic solution which can be followed in object oriented way in any project easily.

Requirement

1.       Read all files from a folder following some specific naming conventions.

2.       There will max N records in a file and M fix number of columns in a record.

3.       File header will contain specific fields which are required to validate records in a file.

4.       There will be 2 tables in database. One for keeping file processing information like number of successfully proceed records, total records in a file, failed record, start & end time etc. And another for keeping records detail.

5.       Each field in record has to be format validated and then business validated.

6.       Instead of any update to database first the file then the record must be validated.

7.       It should generate a response file which can be sent for monitoring purpose later.

Design:

I made some classes where each class is responsible for their work only.

FileProcessor (IO processing)

1.       Reads Input folder for specific type of file.

2.       Calls ValidateFile() of FileValidator

3.       Moves a file to specific folder as per return value from ValidateFile()

FileValidator (Single file validation)

1.       Reads each line from input file.

2.       Calls

  1. isValidate() of FileHeader for first line
  2. isValidate() of FileFooter for last line
  3. isValidate() of FileRecord for rest lines

3.       It also writes a response/log file. You can make a separate class for response file in case you want formatted output/logs.

4.       It also performs database operation, if required in your project, when isValidate() returns specific message.

Status

1.       This is an enum.

2.       FileOperation class takes multiple decision on the basis of return value from ValidateFile(). Same with FileValidator class. It writes different messages to response file as per return value from isValidate(). This decision can’t be taken on the basis of boolean value. I guess returning enum is better than returning a String.

FileHeader, FileRecord, FileFooter (Single record validation)

1.       Header of a file generally contains meta data of file records like number of records in a file, max & min length of record, number of fields in a record line etc.

2.       All of them are made as bean class. It helps to store values in database and to compare later.

3.       Any change to these classes will not impact other classes but enum Status. To remove this dependency or to make it more independent you can create a separate class or interface. But remember FileProcessor and FileValidator classes take decision on the basis of return enum. So if this value get changed a little bit change in these classes will also be required.

Java data File Processing analysis

*This article is copyrighted with all examples and images used in this article. Publication of this article to any where without referencing to original article or to hard copy or any modification in original images or example is strictly prohibited.

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.