Archive

Posts Tagged ‘beginner’

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.

Relation among XML XSL DTD XSD

March 23rd, 2012 34 views No comments

w3schools is good place to start learning web technologies before reading any other manual or book. Here I am summarizing the relation among most of XML flavors. I’ll recommend it  before starting with w3schools.

relation among XML XSD DTD XSL

In addition of above diagram,

  1. DTD & XSD both do the same job. But XSD is newer than DTD, in form of XML, and has more datatypes & other options.
  2. You need not to launch XSLT explicitly to see how your XSL will output finally. XSLT comes with Internet browser. So you just open your XML in browser. It’ll read linked XSL and will display output accordingly.

*Editor : I used eclipse for developing all XML flavours.


HTML vs XHTML

XHTML is nothing but XML formatted HTML. You need to follow XML rules nothing else. Like

  • proper nesting of tags
  • all tags must be closed
  • there must be a root tag
  • value of an attribute must be enclosed in double quote
  • tags name, attributes name and their values must be in low case.
  • etc. . .

Make Electronics easy

March 17th, 2012 32 views No comments

Although I generally write articles related to programming languages, hacks etc. but I like to share illustrated tutorial, videos, helpful snippets etc.
So this time I am sharing some basic animation for electronics student;

555 Timer

555 Timer animation tutorial

Op Amps

Op Amps animation

Transistors

Transistors animation tutorial

Field  Effect Transistors

Resistor

Resistors

Capacitors & ByPassing

Capacitor animation

Inductors

Inductors animation

Tropospheric Scatter Communication

Tropospheric Scatter Communication

Doppler Radar

Doppler Radar

Agile software development, an overview in 5 mins

March 16th, 2012 77 views 2 comments

Remember Arti asked me to develop an application for her mobile. Now she is my wife… housewife… all time free. So you can imagine my condition until I complete her application.

Everyday in the morning, when I get ready to my office, she asks me how much I have completed the application. I already had committed the date but she want to be update with what I am doing. So I have broken down my all development into steps. After every step, I complete, I give it to Arti for testing. It keeps her busy with testing and makes me relaxed to work on next step. She feels that I am busy with her application. So she disturbs me but less.

Well! This is all about Agile Development methodology. Have I finished it so early?

When I read about Agile, I realized that I was working on it from last 3.5 years. In short, If you are not following nay standard of development means you are following Agile. But it is not completely like that. Agile development is a standard in itself. It says

  1. Break down the requirement in steps. Complete one step, test it, and switch to the next.
  2. Let client interact with you to know development status.
  3. Be ready if client remove or modify an existing requirement. Or he adds something new in previous requirement.
  4. Let the client give feedback on last step you completed. It’ll make your system bugfree. And you’ll require less effort on maintenance.

To follow above points;

  1. Management needs to be involved to interact with client.
  2. Client needs free time to pull your leg.
  3. You require more time for iterative testing.

Besides;

  1. You are supposed to deliver the project on fix date. Otherwise the client will cut your throat.
  2. You can start development with rough but precise diagrams & documentation.

Have I missed something? Yes technical terms. Wiki it.

Brief comparision in waterfall and agile

*It seems very similar to waterfall model. But remember in Agile development, client can call you any time even If you are on candle light dinner with your lovable.

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

Struts 1 – how easier it is

March 12th, 2012 63 views No comments

I suppose I am increase redundancy over Internet by writing this article. You’ll find many of articles on Internet over struts. But only some of them has explained how easier strut is.

Prerequisites:

  1. JSP
  2. Servlets
Summary of JSP & Servlets:

JSP is nothing but a HTML page with java scriplets. They were designed to reduce Servlet work load & length. Imagine a well designed HTML page with 2 input box and 1 submit button. You fill HTML form & submit the request to a servlet. Servlet is not aware where the request is coming from. It might be some awt application or web page or web service etc. So it just respond with sum of values of both textbox. What happens next?……… content of your whole HTML page will be erased and you will see just a numeric value on that. That’s why JSP was discovered.

Now what is Servlet?
Servlet is nothing but a special java class running on server hidden from external people. A person can request/invoke it with its alias name written in web.xml. For each request to the webserver, container reads web.xml. Read aliases and calls actual java class who matches with that alias. For the convenience J2EE provides some classes for JSP & SERVLET;

  • Request : to read a request from a client
  • Response : To give response to client
  • Session : To remember client who requested last time
  • Cookie : To store some data at client’s computer.

Read more about it here

Lets start with Strut 1.

Strut is a framework over JSP & Servlet. It divides a Servlet class into 2 parts as per its working: Action & POJO class (or Controller & Model respectively). JSP is just for display/view purpose.

Controller just listen clients. Means it reads client requests and response them like a receptionist. It takes help from other java classes to fulfill the request. These java classes are caller Model. It is just like a restaurant where waiter listen your order and serve you meal. But chef inside kitchen actually cooks food.

This sort of architecture is called MVC(Model-view-controller) architecture.

Key Components

Action Servlet You need this one per application and its supplied by Struts itself. So you don’t write or modify it.
Form Beans For each HTML form one form bean is written. They are just Java beans. When a request to Struts Action Servlet arises. it calls setters on the form bean and populate its field member form request parameters.
Action Objects It has a call-back-like method called execute() which is a great place to get the validated form params and call model components.

its kind of a servlet lite

struts-config.xml This is the Struts-specific deployment descriptor, in it you will map

  • Request URLs to actions
  • Actions to Form beans
  • Actions to views
How struts work?

In short: A request come to web container (say Tomcat) who looks for alias in web.xml. It looks for URL pattern since eince exact URL alias is not present in web.xml. It sends request to ActionServlet. This servlet reads another configuration file (strut-config.xml) who actually has entries of all action classes. It calls setters of related form bean class and populate its fields with request parameters. Pass this form & request to relevant Action class.

how strut 1 works flow

See what actually happens:

  1. Action Servlet (provided into strut bundle, we don’t write  our own) needs to be mapped in web.xml same as other servlet are mapped.
  2. When a hit comes to Action Servlet, it reads strut-config.xml to know actual mapping (like which Action class or ActionForm is mapped and what are their initial values).
  3. Note: if it finds no action mapping, it set error in response header and return back.

  4. What ActionServlet does next with ActionForm
    1. It looks for form bean object into session. If it doesn’t present makes new one. (A form bean is simple java bean extends ActionForm)
    2. Calls reset() of ActionForm and populates it by calling all setters.
    3. If validate=true is set into action-mapping then it calls bean’s validate() of ActionForm.
  5. Note: it calls setter for all methods which are coming as request parameter. So there is no problem if form bean has an extra setter. But if there is an extra request parameter and form bean has no corresponding setter then it’ll generate run time error.

  6. If form bean returns a non-empty errors object then it pass error object to input page or error page as per its action-mapping.
  7. Note: If there is no page defined action-mapping as input page then it does nothing.

  8. If ActionError is empty then ActionServlet creates an object of relevant action class, pass the populated & validates form bean object, and calls action object’s execute().
  9. Note: if ActionForm’s setters or validate() modify actual values of form then they will not be reflected in session. But updated ActionForm will be passed to Action class.

  10. JSP: When a JSP with an html:form tag is rendered, the html:form tag uses its action path to look-up its action-mapping. Like the ActionServlet, it uses the form-bean to check for a ActionForm in session. If it doesn’t find one, it creates one. The new or pre-existing ActionForm is then used by the other html:tags to populate their elements. So the form fields get populated automatically if the form has been submitted once.
    Note:

    1. form-bean name specified in action-mapping is used as the default attribute name in session.
    2. If you are using same form for 2 or multiple actions with the same name in action-mapping, you will ever get the form with last submitted values. To avoid this, use the same ActionForm but with different name in action-mapping.
    3. Similarly, If you submit a form from a JSP page and forward to the same JSP page again from action class or ActionForm, you’ll get the form filled with submitted values even if the values are modified by setters or validate().
    4. html:tags uses form-bean name from session by default. You can change bean scope as per your need.

What else?
You’ll have to read strut tags similar to any other tag library like jsp, jakarta, apache, JSTL etc. Tags help in avoiding direct java code in JSP.

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

Multi-Threading, theory aparts

March 9th, 2012 14 views No comments
Very basic

This article is suitable for beginner only who are just starting in multi-threading. I hope you already have read some books and googled a lot. Here I am covering all basic concept without going to very deep in theory. Once the concepts are clear to you, just re-read books you already had read.

To remember multi-threading, remember multiple programs running on your computer like winamp, photoshop, MS word etc at the same time. You can call each program as a single process. Sometimes you do many things at the same time in a program. For example, your word editor. You read a document while printing it. You start searching for some word and importing clips as well.

Each task/job (can run parallel) under single process/program is called as thread.

CPU is 1000 time faster than you blink your eyes.

You might have slow printer for printing. Once your CPU has instructed printer about what to print, why do you need to wait to get printing done? Your CPU switch to another job like searching in your document. Mean while, with regular interval, CPU can check whether printing is finished. This sort of smart but complex working is called multi-threading.

If you be a CPU you will give up soon

Handling multiple jobs, remembering what that job was doing last time, whether a job need some resource to get completed ….. This is something like a father with 10 children who is not good in house work and his wife has gone to office.

Well! you can visit wiki for technical reading because only technical terms can help you in your exam. Here my aim is to clear concepts and explaining how to write simple threading program in java.

How to write java program

Syntax

1. Extends Thread class

     class ConcreteClass extends Thread {
         public void run() {
             :
         }
     }

	ConcreteClass cObj = new ConcreteClass();
	cObj.start();

2. Implement Runnable interface

     class ConcreteClass implements Runnable {
         public void run() {
             :
         }
     }

     ConcreteClass cObj = new ConcreteClass();
     Thread T1 = new Thread(cObj,"my Thread");
     T1.start();

3. Anonymous class

	class ConcreteClass implements Runnable {
         public void method() {
             :
         }
     }

     final ConcreteClass cObj = new ConcreteClass();
     new Thread(new Runnable(){
     	public void run() {
	   cObj.method();
        }

     }).start();

Sample programs

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.

Thread Tracer in Java

March 8th, 2012 21 views No comments

This program can be integrated with your any java program to trace various states of a thread. It can help you to understand the flow of a thread. But remember, Tracer runs a thread to trace states of a thread. Tracer will not able to print all states of a thread. since there might be a situation when Tracer thread get served to CPU very late or with big interval and another thread changes its 3-4 states in between. I generally run Tracer 2-3 times for proper understanding.

I took its help to find out deadlock in my one of the program over nested monitor.

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;

/*
 * (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.]
 *
 * ---------------------
 * Tracer.java
 * ---------------------
 * *
 */
public class Tracer {
	private ArrayList<WrapperThread> threadList;

	Tracer(){
		threadList = new ArrayList<WrapperThread>();
	}

	public void add(Thread t){
		threadList.add(new WrapperThread(t));
	}

	static{
		new Thread(new Runnable(){
			@Override
			public void run() {
				System.out.println("Monitoring Deadlocks");
				while(Thread.activeCount() >= 2)
					deadLockMonitor();
			}
		},"DeadLock Monitor").start();
	}

	private static void deadLockMonitor(){
		ThreadMXBean mx = ManagementFactory.getThreadMXBean();
		long[] DevilThreads = mx.findDeadlockedThreads();
		if(DevilThreads != null && DevilThreads.length > 0){
			System.out.println(currentTime() + "Deadlock detected");
			for(int i=0;i<DevilThreads.length; i++){
				System.out.println("Thread id :" + DevilThreads[i]);
			}
			System.out.println("Exiting from system");
			System.exit(0);
		}
	}

	public void trace() {
		new Thread(new Runnable(){
			@Override
			public void run() {
				Iterator<WrapperThread> itr;
				while(Thread.activeCount() >= 2){
					itr = threadList.iterator();
					while(itr.hasNext()){
						WrapperThread wt = itr.next();
						if(wt.isStateChanged()){
							System.out.print(currentTime() + " :: " + wt.originalThread.getName()+":"+wt.currentState + "\n");
						}
					}
				}
			}
		},"Tracer").start();
	}

	public static void traceImmediate(final Thread t) {
		new Thread(new Runnable(){
			@Override
			public void run() {
				WrapperThread wt = new WrapperThread(t);
				while(Thread.activeCount() >= 2){
						if(wt.isStateChanged()){
							System.out.print(currentTime() + " :: " + wt.originalThread.getName()+":"+wt.currentState + "\n");
						}
					}
			}
		},"Immediate Tracer").start();
	}

	public static String currentTime(){
		return (new Timestamp(System.currentTimeMillis())).toString();
	}
}

How to use it in your program

		Tracer t = new Tracer();
		t.add(A);
		t.add(B);
		t.trace();

Or

		Tracer.traceImmediate(A);
		Tracer.traceImmediate(B);

Happy coding :)

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.

Payroll System – source code

January 28th, 2012 218 views No comments

An analysis report on payroll system with diagrams is the 2nd most visited article. As per visitor’s interest I am writing this article to help you technically design your own payroll system.
I am not aware what computer language you prefer. So attaching source code for sample payroll system in maximum of languages. This is just for your reference and ideas. So you don’t leave any functionality by mistake while designing the whole system. It’ll also help you to complete your payroll system project in less time. And will give you an idea about screen designing.

Visual Basic .net

Payroll system in Visual Basic .net with source code
Account information
username: admin
password: admin

Java applet

Source code

Please rescan the downloaded files because I afraid from hackers before GOD ;-)

Let me know by your comments if you need more screen shots, source code or something else that can help you.