Posts

Showing posts from June, 2009

Java 6 Collections: Why use LinkedList

Image
Java collections framework avoid the idiosyncratic programming. Therefore, collection framework is more manageable and usability is unified across the applications, and best practices are enforced. Anyway, it is very important to understand the generics based new collection framework. I love to do programming with Eclipse. As shown in the above figure, the instance variable ‘d’ can be initialized with with the ‘ArrayList’ whose parameterized type is ‘Dog’. The type of the instance variable d is ‘List’ which is also based on ‘Dog’ generic type. To this array you can add only instances which are type of ‘Dog’. If the generic type is changed to ‘Integer,’ everything will change from ‘Dog’ to ‘Integer’. If you are comfortable with Java Generics, this is the time to consider that how they have implemented Collection to cater for any Object. Linked List Array and her counterpart ArrayList has direct access which cost Ο(1) time . Comparably, this is the cheapest. However, arra...

Java Thread Interaction: use of wait and notify

In this blog, my main intention to introduce the thread interaction based on the java.lang.wait() and java.lang.notify()/notifyAll() methods. Anyway, I would keen to keep the things simple. It is not difficult to visualize one of the playground trainings you had participated. There is one trainer and number of players. All the players are waiting for the ball which has been thrown by the instructor. One person can acquire ball at once and he will re-throws the ball back to the trainer. To make the things simple, suppose a player, who acquired the ball, has been pulled out from the field. For the design, we have two factors to model The behavior between Trainer and the players The ownership of the ball In the first case players are waiting for the trainer’s notification to acquire the ball, anyway in this competition thread scheduler decide who should get the ball, mean no special priority (all the Player threads are in the same priority level). To win the competition at...

Example for boolean java.lang.Thread.isAlive()

Image
This blog intended to check the aliveness of a thread. We have used the method boolean isAlive()  define in the class java.lang.Thread, which is not a static method. Aliveness of the thread can be noted in the Thread lifecycle diagram as follows.   I wrote a simple example to demonstrate the status of the aliveness of the Thread. In that program, aliveness tested before start, that is where the thread is in the New state, before end (Runnable, Running, or Blocked) and after end which is Dead status. The AliveExample thread is blocked while main thread is running, although it is alive. package com.ojitha.thread; enum Alive { YES(" thread is alive"), NO(" thread is not alive"); private String msg; private Alive(String msg){ this.msg = msg; } public String getMessage(){ return this.msg; } } public class TestAlive { /** * @param args */ public static void main(String[] args) { AliveExample a = new AliveExample(); Thread t...