Java – java.util.concurrent.locks.Lock

This is all about Java’s standard way java.util.concurrent.locks to lock resources for proper control of concurrent access.

There are several ways how concurrent access can be controlled. Teh easiest way is zo use the keyword synchronized on method level, which will synchronize on the object itself (this), or the class object for static methods. This is an easy but often unperformant way to achieve locking.

Slightly better would be to synchronize on certain objects when really needed, and by using the volatile keyword along with the object to synchronize on now even double checked locking works in Java.

Since this still often is not elegant enough there are some more ways. Excellent choices here are the implementations of the interface java.util.concurrent.locks.Lock.

Outcome, Key Results and Milestones

For every task respective work package respective mini project you are about to undertake you shold be clear of certain things. First of all you should explicitely know your outcome, then you should know what the key results, and you should have the milestones in mind, to know where you are and whether certain things should be rearranged. More details on that will follow later in this article.

Why should you know your outcome, the key results and the milestones?

By knowing the outcome you understand what needs to be achieved. Knowing the key results helps you to put the focus on the right spots and therefore helps spending your time and energy efficiently. And having set milestones (set date and(!) time, don’t et room to fool yourself) makes it easy to keep track of your progress. So you can easily tell when you are off track and can start mitigating future situations that might become unpleasant otherwise.

Java – Passing Parameters To An Anonymous Class

As we all know fairly often anonymous classes do the perfect job in certain situations. I.e. it sometime is just not worth the effort to create an implementation class of a certain interface, just because that single one method of the interface is to be implemented.

Just imagine an interface like this one:

public interface MyInterface {
	void doSomething();

If different doSomething‘s are needed on dfferent places it’s not worth the effort to create, let’s say four implementation classes.

It’s rather more useful, to make this an anonymous class, and write the couple of lines which make up the doSomething code. Just like in this example: (more…)

Java – How To Make Deadlock

The entire web is full of articles that describe how deadlocks can be avoided. But when writing test cases it often comes in handy to actually make such a thing as a deadlock happen.

Of course we all know that resources have to be locked in a certain order, in order to avoid deadlocks. So it is easy to break this pattern to make a deadlock occur. Let’s have a resource A and a resource B. One thread locks first A and then tries to lock B. The other thread starts by locking B and then tries to get a lock for A. So both threads will wait forever, as they don’t have any strategy to avoid that.

Java – Internationalization and Localization

People often confuse the terms Internationalization i18n and Localization l10n during discussions. While their intention is correct their terminology is not. And if picky people – like me 🙂 – are around the danger is high, that the discussion might be interrupted to sort out things straight.

So first let us stick to W3C’s definition of the terms:

JavaScript – Type Conversion To Number

Posted on: 1 Comment

In JavaScript, the + operator is used for both addition of numeric values and concatenation of strings. This often causes problems when i.e. adding up form field values. Since JavaScript is a non-typed language form field values will be treated as strings, and if they are “+”‘d together, JavaScript will treat this as a concatenation operation instead of adding numeric values.

To fix this problem, JavaScript needs a hint to tell it when to treat value as numbers, rather than strings. The unary + operator can be used to implicitly convert the string value into a number. Prefixing a variable or expression with + will force it to evaluate as a number, which can then be successfully used in a math operation.

Java – Enumeration as List

Once in a while I stumble across a situation when I have an Enumeration<?>, but I’d actually rather have a List<?> instead.

In my current situation I had a little tool that tracked a http request’s header names for me. But it expected the names as an List<String> rather than the enumeration<?> the HttpServletRequest returns when being asked to give the names of all received headers.

This little trick does the job. And – best of all – it’s not even a hack… 🙂

Enumeration<String> enumeration = request.getHeaderNames();
List<String> names = Collections.list(enumeration);

Immutable Objects – A Powerful Concept

Posted on: No Comments

What Are Immutable Objects?

Immutable objects are objects that cannot be modified. So there are no setters or other methods that alter the state of such an immutable object. They are easy to design and easy to implement as i.e. thread safety is not an issue at all.

Immutable Objects In Combination With the Flyweight Pattern

A flyweight object always has to be immutable as for flyweight objects it always shall be guaranteed that an instance encapsulating a certain identity of the object always exists only once within the system. So in Java i.e. the String “sample” shall exist only once in the system. In this way memory can be saved as several hundred instances of “sample” will be avoided. All uses of “sample” will reference the single one instance of “sample”.
If now one instance of “sample” has to be modified to “SAMPLE” a new instance for te new “SAMPLE” is created and used for all upcoming “SAMPLE”‘s. Therefore obviously the “sample” has to be immutable as all other references would change to “SAMPLE” too.

So flyweights always have to be immutable, and immutables that are not implemented as flyweights keep redundant data in memory, and therefore shall be avoided unless it can be guaranteed that there will be no more than only a small number of identical instance.

Java – Guava Library’s ListeningExecutorService Tutorial

Posted on: No Comments

As an add-on to the previous article “Java – ExecutorService and ThreadPoolExecutor Tutorial” I’d like to introduce a feature from Google’s Guava Library.

The idea behind the ListeningExecutorService and the dependent ListenableFuture is that callback methods can be registered, that will be called once execution of a task reaches a defined state.

See here the code sample from the Guava wiki to get a first idea how this works.

Java – ExecutorService and ThreadPoolExecutor Tutorial

Posted on: 6 Comments

Starting with JDK1.5 Java comes in with a handy thread handling and pooling feature. As part of the java.util.concurrent-package there is the interface ExecutorService and the implementation class ThreadpoolExecutor. Along with these the interfaces Runnable and Callable are required.

Of course the java.util.concurrent-package does a lot more than holding these two definitions, but this will be all I am showing here in this blog post. Still it is a very powerful tool that comes in very handy whenever you have to fork execution of work that needs to be done.

So how is the ExecutorService being used? (more…)

JavaScript – Cookie Handling

Posted on: No Comments

Cookies, that are persistent pieces of information stored within the client’s browser, are used in many ways by both, server and browser. Sometimes it is necessary to access certain cookie values by JavaScript code running in the browser. JavaScript respective the Document Object Model offer the features required to gainthat sort of access.

These cookies are actually transported as HTTP Headers between server and client and back. The full spec can be found on Wikipedia on the page HTTP cookie.

The DOM offers the property cookie of the document. This property contains the cookie string received from the server plus anything being stored locally and is valid for the current domain, the current path and the current protocol.

JavaScript can be used to manipulate this cookie string in order to get respective set a cookie’s value.

Java – Static Block

Posted on: No Comments

In Java instances of a class can use the constructor to initialize the instance. A similar construct exists to initialize the class when it is loaded by the class loader. This is called a static block and can be used very easily as shown in the sample below:

public class Playground {

	static {
		// do something when initializing the class



Java – The Volatile Keyword

Posted on: No Comments

In Java the keyword volatile is used to indicate that a variable’s value will be modified by different threads.

In contrast to that the keyword synchronized is used to control access to the variable in a way that only one thread can modify the value of that variable.

Now where is the difference between indicating simultaneous access and controlling simultaneous access to a variable?

Project Identity

Posted on: No Comments

A project’s identity is made up of several components. It is obviously strongly influenced by the Corporate Identity of the organization performing the project. And every project, no matter how small it is, should also have an official mission statement and its own logo design. And, as readers of “No Hack!” may already suspect, a strong set of style guides will be required too. There don’t have to be big budgets for these additional items, but nonetheless there should be a clearly defined corporate identity, a mission statement and a nice logo design.

Every project manager should be trying hard to establish such a project identity. This not only provides value during the project’s execution phase. It also helps people remember that project later on, when they are asking question like: “This and that did work out excellent on the previous project XYZ, how did we do that there?”
And as a consequence of such post-mortem interest in projects make sure that people always known where to find the project’s master document, even if the project is long over. Only in that way they actually can find how stuff was done in this project.