Google

Aug 29, 2012

Article: Scenarios and solutions for better concurrency and thread safety part-3 Semaphores and mutexes

Scenario 1: A web service receives http requests for data, places the request into an internal queue, and a separate worker thread pulls the work item from the queue and performs the work. For example, a trading system creates a buy order and places it in a queue, and a separate consumer thread picks up the order and sends it to the stock exchange. A typical producer and consumer scenario.

Scenario 2: Semaphore can be used to create worker threads even when the number of worker threads to be created is not known upfront. This is because a semaphore can be created with 0 permits, and wait until a number of releases have been made. For example, if you want to recursively traverse through nested folders and spawn a number of worker threads to move the html files found to a destination folder and increment the semaphore permits with a release() method call. A separate thread will wait with the acquire(numberofpermits) to acquire all the released permits before start archiving (i.e. zipping up) those files.

Scenario 3: Reference counting where a shared resource is incremented or decremented. The increment/decrement/test operations must be thread safe. For example, a counter that keeps track of the nummber of active logged in users by incrementing the count when users log in and decrementing the count when the users log out.



Solution: In general, a sem-a-phore means sending messages by holding the arms or two flags or poles in certain positions according to an alphabetic code. In programming, especially in Unix systems, semaphores are a technique for coordinating or synchronizing activities in which multiple processes compete for the same operating system resources. Semaphores are commonly use for two purposes: to share a common memory space and to share access to files. In Java, a semaphore is used for coordinating multiple threads.

Q. What is the difference between a mutex and a semaphore?
A. 

Mutex: is a single key to a toilet. One person can have the key and occupy the toilet  at the time. When finished, the person gives (or releases) the key to the next person in the queue. In Java, every object has a mutex and only a single thread can get hold of a mutex.

Semaphore: Is a number of free identical toilet keys. For example, having 3 toilets with identical locks and keys. The semaphore count is set to 3 at beginning and then the count is decremented as people are acquiring the key to the toilets. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Here is an example of producer and consumer using a mutex.

public class ProducerConsumer {

 private int count;  //shared resource, i.e. accessed by multiple threads

 public synchronized void consume() {
  while (count == 0) {
   try {
    wait();    //wait till notified
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }

  //gets here if count is > 0
  count--;     //consume the count by decrementing it by 1
  System.out.println("Consumed:" + count);
  notify();  //notify waiting threads to resume

 }

 public synchronized void produce() {
  while(count > 0) {
   try {
    wait();    //wait till notified
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  
  //gets here if count == 0;
  count++;
  System.out.println("Produced: " + count);
  notify();   //notify waiting threads to resume
 }

 public static void main(String[] args)  {
        //inner classes can only access final variables
  final ProducerConsumer pc = new ProducerConsumer();

  //anonymous inner class for a new producer worker thread 
  Runnable producer = new Runnable() {
   @Override
   public void run() {
    for (int i = 0; i < 3; i++) {
     pc.produce(); 
    }
   }
  };

  
  //anonymous inner class for a new consumer worker thread
  Runnable consumer = new Runnable() {
   @Override
   public void run() {
    for (int i = 0; i < 3; i++) {
      pc.consume();
    }
   }
  };

  Thread producerThread = new Thread(producer); //creates a new worker thread from the main thread
  Thread consumerThread = new Thread(consumer); //creates a new worker thread from the main thread

  producerThread.start();  // executes the run() method in producer
  consumerThread.start();  // executes the run() method in consumer

 }
}


Now, let us look at the similar example with a Semaphore in Java 5. You can create a Semaphore with relevant number of permits, and the permits can be acquired or released.

import java.util.concurrent.Semaphore;

public class ProducerConsumer2 {

 private int count;
 
 /**
  * call to release will increment the permit(s) and 
  * call to acquire will decrement the permit(s)
  */
 static Semaphore semCon = new Semaphore(0);
 static Semaphore semProd = new Semaphore(1); //start with 1 permit

 public  void consume() {
  try {
   //acquire a permit from this semaphore before continuing
   semCon.acquire();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }

  count--;
  System.out.println("Consumed:" + count);
  //releases a permit, returning it to the semaphore.
  semProd.release(); 

 }

 public void produce() {
  try {
   //acquire a permit from this semaphore before continuing
   semProd.acquire(); 
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  count++;
  System.out.println("Produced: " + count);
  //releases a permit, returning it to the semaphore.
  semCon.release();   
 }

 public static void main(String[] args)  {

  final ProducerConsumer2 pc = new ProducerConsumer2();

  //anonymous inner class for a new producer worker thread 
  Runnable producer = new Runnable() {
   @Override
   public void run() {
    for (int i = 0; i < 3; i++) {
     pc.produce(); 
    }
   }
  };

  //anonymous inner class for a new consumer worker thread 
  Runnable consumer = new Runnable() {
   @Override
   public void run() {
    for (int i = 0; i < 3; i++) {
      pc.consume();
    }
   }
  };

  Thread producerThread = new Thread(producer); //creates a new worker thread from the main thread
  Thread consumerThread = new Thread(consumer); //creates a new worker thread from the main thread

  producerThread.start(); // executes the run() method in producer
  consumerThread.start(); // executes the run() method in consumer

 }
}


The output for both the above code:

Produced: 1
Consumed:0
Produced: 1
Consumed:0
Produced: 1
Consumed:0

Labels:

Aug 28, 2012

Are you going for programming jobs at investment banks or financial institutions? Knowing some investment and trading terms will be useful

The investment banks and financial organizations do pay a very competitive remunerations and many of you aspire to work for such organizations. Here are a  few questions and answers on different securities, derivatives, and FIX engine/trading system basics to familiarize yourself  with some of the terminologies.


Q. What is a stock, share, or equity?
A. Whether you say shares, equity, or stock, it all means the same thing. A stock is a share in the ownership of a company. Stock represents a claim on the company's assets and earnings. As you acquire more stock, your ownership stake in the company becomes greater. A stock is represented by a stock certificate. This is a fancy piece of paper that is a proof of your ownership. In today's computer age, you won't actually get to see this document because your brokerage keeps these records electronically.

Q. What is a Security?
A. Security is a broader term which includes shares as well. There are two types of securities:
  •    Equity security (e.g. Share, Common Stock, Options, Warrants, etc. )
  •    Debt security (e.g. Bonds, Debentures, Bank Notes, Coupons, FICC (Fixed Income, Currencies, and Commodities), etc)
Q. How do primary and secondary financial markets differ?  
A. A financial market is a market in which people and entities can trade financial securities. The primary market refers to the market where securities are created, while the secondary market is one in which the previously created stocks are traded among investors. In the primary market, a company that needs money sells stock. In the secondary market, someone who owns stock sells their stock to someone else. The secondary market does not bring in money for a company.

Q. What is a derivative?
A. A derivative is anything that is valued based upon some other asset.  The derivative itself is merely a contract between two or more parties. Its value is determined by fluctuations in the underlying asset. For example,  A call option is a derivative that gives the buyer the option (but not the obligation) to buy 200 shares of a certain stock at a pre-determined price. It is a derivative because the value of the option depends on what the underlying stock does. The most common underlying assets include stocks, bonds, commodities, currencies, and interest rates.  Derivatives are generally used as an instrument to hedge risk, but can also be used for speculative (i.e betting) purposes as most derivatives are characterized by high leverage.

Q. What is the difference between options and warrants?
A. The warrants are issued by companies to raise money, and the options are not. But both have lots of similarities.
  • Both options and warrants are based on an underlying asset such as stocks. They are exercised at a pre-determined price or strike price. The seller of an option or warrant is obliged to honor the terms of the option or warrant. The buyer of an option or warrant must pay a price (or premium) up front.
  • Both options and warrants expire at a pre-determined date.


Q. What are the different ways a company can raise money (i.e raise capital)?
A. 

1. Borrowing from a bank (debt security): Always short term (1 year or less) and the banks have the first claim on assets of a bankrupt firm.

2. Issuing preferred stocks (equity security): By choosing to issue new "preferred" stock to raise capital. Buyers of these "preferred stock" have special rights in the event the underlying company encounters financial trouble. If profits are limited, preferred-stock owners will be paid their dividends after bondholders receive their guaranteed interest payments but before any common stock dividends are paid.

3. Issuing common stocks (equity security): The companies that are in good financial health can raise capital by issuing common stock. Although common shareholders have the exclusive right to elect a ccompany's board of directors, they rank behind holders of bonds and preferred stock when it comes to sharing profits.

4. Issuing bonds (debt security): By issuing bonds, companies must make semi-annual or annual interest payments on the bond and must buy back the bond when it matures. Bonds can be both long and short term. This can be a substantial drain on a company's cash.

5. Issuing warrants (equity security): Warrants allow a company to generate money by selling stocks to the owner of a warrant who exercises the warrant. The warrants will only be exercised if the stock price is higher than the exercise price.

Q. What is a debt security?
A. Debt securities are legal obligations to repay borrowed funds at a specified maturity date and provide interim interest payments as specified in the contract. Examples of debt securities include bonds, certificates of deposit, commercial paper, loans, and debentures. The debt securities increase the probability of bankruptcy and expected bankruptcy costs; reduce financial flexibilities

Q. What is the difference between equity security and debt security?
A. Debt securities represent indebtedness or borrowing by the issuing company. On the other hand, equity securities represent ownership of the issuing company.

Bonds and stocks are both securities, but the major difference between the two is that stockholders have an equity stake in the company as the part owners of the company, whereas bondholders have a creditor stake in the company as the lenders. Another difference is that bonds usually have a defined term, or maturity, after which the bond is redeemed, whereas stocks may be outstanding indefinitely. A bond is a formal contract to repay borrowed money with interest at fixed intervals (semiannual, annual, sometimes monthly).

Bonds generally can trade anywhere in the world that a buyer and seller can strike a contractual deal. There is no central place or exchange for bond trading, as there is one for publicly traded stocks. The bond market is known as an "over-the-counter" market, rather than an exchange market. There are some exceptions to this as some corporate bonds are listed on an exchange. But the majority of bonds do not trade on exchanges.

Q. What is a share contract note?
A. It is the confirmation of the share order via a contract note, which is sent to you via email or post to you by your broker.

Q. What do you understand by the term "settlement and transfer"?
A. When you buy shares, on the other side of the transaction there is a seller. For every buy, there is a sell. The money you owe for the shares is owed to the seller and must be exchanged for the seller’s ownership in the company. This process is called settlement and transfer.

Q. What are the diffrent types of stock market orders?
A. The most common ones are "on market" and "at limit". The orders could also be either  "buy order" or "sell order"

On market: Usually one gets the stock at the ask price of the moment when the order reaches the exchange. A market order is the default option and is likely to be executed because it does not contain restrictions on the buy/sell price or the timeframe in which the order can be executed. So, you don't enter any price info while placing an order.

At limit: A limit order is an order to buy or sell a stock at a specific price or better. So, you need to enter a limit price when placing an order. A buy limit order can only be executed at the limit price or lower, and a sell limit order can only be executed at the limit price or higher.  A limit order is not guaranteed to execute.  A limit order can only be filled if the stock’s market price reaches the limit price.  While limit orders do not guarantee execution, they help ensure that an investor does not pay more than a pre-determined price for a stock.

Stop order: An order to buy or sell a security when its price surpasses a particular point, thus ensuring a greater probability of achieving a predetermined entry or exit price, limiting the investor's loss or locking in his or her profit. Investors normally place a stop order before they go on a holiday.

Q. What is an exchange traded fund (ETF)?
A. An exchange-traded fund (ETF) is a security that tracks an index, a commodity or a basket of assets like an index fund, but trades like a stock on an exchange. An ETF holds assets such as stocks, commodities, or bonds, and trades close to its net asset value over the course of the trading day. ETFs are attractive as investments because of their low costs, tax efficiency, and stock-like features.

Q. What is a FIX protocol?
A. FIX stands for Financial Information eXchange protocol, which is an open specification intended to streamline electronic communications in the financial securities industry. FIX supports multiple formats and types of communications between financial entities including trade allocation, order submissions, order changes, execution reporting,  email, and texting. For example, the  CameronFIX is an engine that uses the open FIX standards. QuickFixJ is a free open source Java based FIX engine. Here is a simplified high level over view of a trading system written in Java.

FIX simplies the implementation of interfaces by using so called FIX engines. There are open source implementations as well as commercial ones. The FIX engine takes care of the functional aspects like defining the message semantics, supporting different types of messages like heart beat, test request, resend request, execution report, new order, rejection message, etc and non functional aspects like logging FIX messages, dealing with dropped connections, handling high trading volumes, etc.

A FIX message has the following structure:
  • Message header: Contains the message type, length, sender/receiver name, sequence number, time stamp, etc.
  • Message body: Contains session/application specifc data. 
  • Message trailer: Contains a message checksum and an optional signature.


The FIX message fields are basically the combination of a tag, which is a numerical identifier, with a value, which is a string and a delimiter, which is written as ^ (i.e. ASCII 0x01). In addition to hundreds of predined tags, FIX also supports custom tags between field numbers 5000 and 9999.  Here is an example of a FIX message with a header, body, and trailer.


8=FIX.4.2^9=0195^35=D^34=10^43=N^49=VENDOR^50=CUSTOMER^56=BROKER^
52=20120824-10:30:00^1=ACCOUNT123^11=10^21=1^55=WBC^48=1234567^22=1^
54=1^38=500^40=2^44=46.5500^59=0^10=124
<br/>



Tag meaning: Please refer to  FIX Tag Details


8 (FIX Version - always first field), 9 (body length -- always last field), 35 (Msg Tpe, for example 0 - Heartbeat, 1 - TestRequest, D - NewOrderSingle, etc), 49 (company that is sending the message id), 50 (message originator id), 55 (Symbol), 48 (Security Id), 22 (alternative security id), 54 (side of order, for e.g. 0 - buy, 1 - sell, etc), 38 (order quantity), 40 (order type, 0 - Market, 1 - Limit, etc), 44 (price), 59 (order expiry, 0 - day, 1 - Good Till Cancel, etc), 10 (3 byte checksum -- always last field)





Q. What are some of the required capabilities of a trading system?
A. 
  • Place buy, sell, on market, at limit, etc orders to the market.
  • Handle status updates from the market asynchronously. The order statuses include partially-filled, rejected, cancelled, pending, completed, suspended (due to pending corporate actions), etc.
  • Validate orders against market rules before placing on to the market.
  • Process contract notes.
  • Produce graphs and reports for a trader to make a more informed decision.
  • Handle trade embargo and staff trade restrictions.
  • Handle high volume, especially during market crash and panic selling


Q. What do you understand by the term portfolio value, and how do you calculate the value?
A. The term portfolio refers to any collection of financial assets such as stocks, bonds, and cash.

  • Listed securities are valued at number of shares owned times the market price per share
  • plus the number of calls times the excess of the market price per share over the call price
  • plus the number of puts times the excess of the put price over the market price per share.
  • plus any managed funds owned
  • plus any bonds, term deposits, cash, and other fixed interests assets.





Labels:

Aug 22, 2012

Article: Scenarios and solutions for better concurrency and thread safety part-2 CountDownLatch and CyclicBarrier

There are other real life scenarios where the java.util.concurrent package with the following classes can be put to great use.
  • CountDownLatch
  • CyclicBarrier
Here are some real life scenarios discusssed below to make use of the above classes.

Scenario: A main thread creates 3 database connections and assigns each of those connection to 3 different child threads that are spawned from the main thread. The main thread must wait while all the child threads are completed and then close all the database connections. So, how will you accomplish this?

Solution: This where the CountDownLatch comes in handy as you already know that there are finite (i.e 3) number of threads.  CountDownLatch can be used by the main thread to wait on the child threads. A CountDownLatch will be created with 3 being the count.

CountDownLatch countDownLatch = new CountDownLatch(MAX_THREADS);

The main thread will spawn new threads and wait on the count to reach 0 with the awit( ) method.

countDownLatch.await();

As the each child thread process within the run( ) method, as the child thread completes processing, the count can be decremented with

countDownLatch.countDown(); 





Here is a simplified example where the worker threads wait for all the other worker threads to start with the startSignal and the main thread waits for all the worker threads to complete with a stopSignal as illustrated in the above diagram . Firstly, define a worker thread class.


import java.util.concurrent.CountDownLatch;

public class Worker implements Runnable {
 
    private CountDownLatch startLatch;
    private CountDownLatch stopLatch;

    public Worker(CountDownLatch startLatch, CountDownLatch stopLatch) {
       this.startLatch = startLatch;
       this.stopLatch = stopLatch;
    }

    @Override
    public void run() {
        try {
            startLatch.await(); // wait until the latch has counted down to zero
            System.out.println("Running: " + Thread.currentThread().getName());
        } catch (InterruptedException ex)  {
            ex.printStackTrace();
        }
        finally {
         //count down to let the main thread to continue once the count reaches 0 from MAX_THREADS (i.e. 5)
         stopLatch.countDown(); 
        }
 }

}


Finally, define the WaitForAllThreadsToStart class that creates the worker thread.

import java.util.concurrent.CountDownLatch;

public class WaitForAllThreadsToStart {
 
 private static final int MAX_THREADS = 3;
 
 public static void main(String[] args) throws Exception {
     CountDownLatch startSignal = new CountDownLatch(1);   //count down from 1 to 0
     CountDownLatch stopSignal = new CountDownLatch(MAX_THREADS); // count down from 3 to 0
        
     System.out.println("The main thread is going to spawn " + MAX_THREADS  + " worker threads.....");  
     for (int i = 1; i <= MAX_THREADS; i++) {
        Thread t = new Thread(new Worker(startSignal,stopSignal), "thread-" + i);
        Thread.sleep(300);
        t.start();
        System.out.println("Started: " + t.getName() + " but waits for other threads to start.");
     }
        
     //count down the start signal from 1 to 0, so that the waiting worker threads can start executing 
     startSignal.countDown();
     System.out.println("worker threads can now start executing as all worker threads have started.....");
     try{       
        stopSignal.await(); // wait for the worker threads to complete by counting down the stopLatch
     } catch (InterruptedException ex){
        ex.printStackTrace();
     }
     System.out.println("finished executing the worker threads and the main thread is continuing.");
     System.out.println("The main thread can execute any task here.");
        
 }
}


The output will like:

The main thread is going to spawn 3 worker threads.....
Started: thread-1 but waiting for other threads to start.
Started: thread-2 but waiting for other threads to start.
Started: thread-3 but waiting for other threads to start.
worker threads can now start executing as all worker threads have started.....
Running: thread-1
Running: thread-3
Running: thread-2
finished executing the worker threads and the main thread is continuing.
The main thread can execute any task here.


Scenario: What if the above scenario has a specific requirement where the 3 children threads need to wait between each other? For example, if each of the 3 threads need to perform 2 tasks. Before the task 2 can be started by a thread, all the 3 child threads must finish task 1. The task 1 can be reading data from the database and task 2 could be performing some computation and finally these computations need to be consolidated and written back by a single thread.

Solution:  A CyclicBarrier can be used if the number of children threads are know upfront and to implement waiting amongst child threads until all of them finish.  This is useful where parallel threads need to perform a job which requires sequential execution. It has methods like cyclicBarrier.await( ) and cyclicBarrier.reset( );




Here is the simplified code to improve understanding. Firstly the WorkerTask thread.


import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class WorkerTask implements Runnable {
 
 private CyclicBarrier barrier;
  
 public WorkerTask(CyclicBarrier barrier) {
  this.barrier = barrier;
 }

 @Override
 public void run() {
   String threadName = Thread.currentThread().getName();
   try {
         System.out.println(threadName + " is now performing Task-A");
         barrier.await();      //barrier point for all threads to finish Task-A
           
         System.out.println(threadName + " is now performing Task-B");
         barrier.await();     //barrier point for all threads to finish Task-B 
           
   } catch (BrokenBarrierException ex)  {
         ex.printStackTrace();
   } catch (InterruptedException e) {
      e.printStackTrace();
   }
  }
}


Now, the test class that creates the worker threads to perform Tasak-A and Task-B, and the barrier thread for consolidation work.

import java.util.concurrent.CyclicBarrier;

public class WaitForBarrierPoint {
 
 private static final int MAX_THREADS = 3;
 private static final int NO_OF_TASKS = 2;    //Task-A and Task-B
 
 private static int taskCount = 0;
 
 //new worker thread that monitors the barrier condition, i.e. 
 //MAX_THREADS that complete a task, and performs the consolidation task.
 //This is an anonymous inner class in action
 private static CyclicBarrier cb = new CyclicBarrier(MAX_THREADS, new Runnable() {
 
   @Override
   public void run() {
     System.out.println("All " + MAX_THREADS + " threads have reached the barrier point.");
     ++taskCount;
   
     //the consolidation job starts only after both tasks are completed.
     if(taskCount == NO_OF_TASKS) {
      System.out.println("The consolidation job can start now .... ");
   }
  }
 }); 
 
 
 public static void main(String[] args) {
  Thread t = null;
  //create 3 worker threads
  for (int i = 1; i <= MAX_THREADS; i++) {
   t = new Thread(new WorkerTask(cb), "Thread-" + i);
   t.start();
  }
  
  System.out.println("The main thread ends here.");
 }
 
}


Q. Why is it called a cyclic barrier?
A. Because it acts as a barrier point for a number of worker threads to wait for each other to complete their tasks. The barrier is called cyclic because it can be re-used after all the waiting worker threads are released for the next barrier point.

A barrier is constructed with the following arguments to a constructor.
  • the number of threads that will be participating in the parallel operation.
  • an optional, amalgamation or consolidation  routine to be run at the end of each step or iteration.
At each step (or iteration) of the operation:
  • each thread carries out its portion of the work to complete that step.
  • after doing its portion of the work, each thread calls the barrier's await ( ) method.
  • the await( ) method returns only when 
          -- all 3 threads have called await( ).
          -- the amalgamation or consolidation  method has run (the barrier calls this on the last thread to call
              await( ) before releasing the awaiting threads).

if any of the 3 threads is interrupted or times out while waiting for the barrier, then the barrier is "broken" and all other waiting threads receive a BrokenBarrierException. This will  propagate to all threads and for the other steps to halt, or for the steps to be interrupted externally by interrupting just one of the threads.

Q. So, when to use a CountDownLatch and when to use a  CyclicBarrier?
A. A CountDownLatch is initialized with a counter. Threads can then either count down on the latch or wait for it to reach 0. When the latch reaches 0, all waiting threads can resume.

If you want a set of threads to repeatedly meet at a common point, you are better served by using a CyclicBarrier. For example, start a bunch of threads, meet, do some stuff like data consolidation or amalgamation, meet again, validate some assertions, and do this repeatedly.

A given CountDownLatch can only be used once, making it inconvenient for operations that occur in steps, with intermediate results from the different threads needing to be consolidated between steps. The CountDownLatch also doesn't explicitly allow one thread to tell the others to "stop waiting", which is sometimes useful, for example, if an error occurs in one of the threads.

The CyclicBarrier is generally more useful than CountDownLatch in scenarios where:
  • a multithreaded operation occurs in steps or iterations, and
  • a single-threaded operation is required between steps/iterations, for example, to combine the results of the previous multithreaded steps.

Stay tuned for Semaphores and Atomic classes in the next part.

Labels:

Aug 20, 2012

Article: Scenarios and solutions for better concurrency and thread safety part-1 ReadWriteLocks

Scenario: You need to load  stock exchange security codes from a database and cache them for performance. The security codes need to be refreshed say every 30 minutes. This cached data needs to be populated and refreshed by a single writer thread and read by several reader threads. How will you ensure that your read/write solution is scalable and thread safe?

Solution:  The java.util.concurrent.locks package provides classes that implement read/write locks where the read lock can be executed in parallel by multiple threads and the write lock can be held by only a single thread. The ReadWriteLock interface maintains a pair of associated locks, one for read-only and one for writing. The readLock( ) may be held simultaneously by multiple reader threads, while the writeLock( ) is exclusive. In general, this implementation improves performance and scalability when compared to the mutex locks (i.e. via synchronized key word) when

  • there are more reads and read duration compared writes and write duration. 
  • It also depends on the system you are running on -- for example multi-core processors for better parallelism. 

The ConcurrentHashmap is another example where improved performance can be achieved when you have more reads than writes. The ConcurrentHashmap allows concurrent reads and locks only the buckets that are used to modify or insert data.

Here are a few terminlogies you need to be familiar with in implementing locks.

Q. What is a reentrant lock?
A. This can be considered as a replacement for the traditional “wait-notify” method. The basic concept is, every thread needs to acquire the lock before entering in to the critical section and should release it after finishing it. ReentrantLock eliminates the use of “synchronized” keyword to provide better concurrency

The term reentrant means if a thread enters a second synchronized block protected by a monitor that the thread already owns from , the thread will be allowed to proceed, and the lock will not be released when the thread exits the second (or subsequent) synchronized block, but only will be released when it exits the first synchronized block it entered protected by that monitor. Similarly, when you use reentrant locks, there will be an acquisition count associated with the lock, and if a thread that holds the lock acquires it again, the acquisition count is incremented and the lock then needs to be released twice to truly release the lock.  A writer can acquire the read lock - but the reverse is not true. If a reader tries to acquire the write lock it will never succeed.

Q. What do you need to watch out for in releasing a lock?
A. The locks need to be released in a finally block, otherwise if an exception is thrown, the lock might never get released.

Q. Why would you need a reentrant lock when there is synchronized keyword? How will you favor one over the other?
A. The reentrant locks are more scalable as it allows concurrent reads. Having said this, the locking classes in java.util.concurrent.lock package are advanced tools for advanced users and specific situations like the scenario discussed above. In general, you should stick with synchronization unless you have a specific need  like
  • the number of reads are far more than the number of writes. 
  • demonstrated evidence through proper performance testing that synchronization in a specific situation is a scalability bottleneck. 
  • specif features like timed lock waits, interruptible lock waits, non-block-structured locks, multiple condition variables, or lock polling are required.

Q. What are fair and unfair locks?
A. One of the arguments to the constructor of ReentrantLock is a boolean value that lets you choose whether you want a fair or an unfair lock. A fair lock is one where the threads acquire the lock in the same order they asked for it. In another words, when the write lock is released either the longest-waiting single writer will be assigned the write lock, or if there is a reader waiting longer than any writer, the set of readers will be assigned the read lock. When constructed as non-fair, the order of entry to the lock are not necessarily in the arrival order.
If the readers are active and a writer enters the lock then no subsequent readers will be granted the read lock until after that writer has acquired and released the write lock. Here is a simplified example of read/write locks.



Firstly, define the SharedData that gets accessed by multiple reader and writer threads. So, provide proper locks.

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;


public class SharedData<T> {
 
 private List<Integer> numbers = new ArrayList<Integer>(20);
 private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

 public List<Integer> getTechnologies() {
  return numbers;
 }
 
 public void writeData(Integer number) {
  String threadName = Thread.currentThread().getName();
  lock.writeLock().lock(); //acquire a write lock if no other thread has acquired read/write lock
                           //only a single thread can write
  
  System.out.println("threads waiting for read/write lock: " + lock.getQueueLength());
  // This should be always one
  System.out.println("writer locks held " + lock.getWriteHoldCount());
  try {
   numbers.add(number);
   System.out.println(">>>>>" + threadName + " writing: " + number);
   Thread.sleep(2000);    // sleep for 2 seconds to demo read/write locks
  } catch (InterruptedException e) {
   e.printStackTrace();
  } finally {
   System.out.println(threadName + " releasing write lock");
   lock.writeLock().unlock();  //lock will be released
  }
 }
 
 public void readData() {
    String threadName = Thread.currentThread().getName();
    lock.readLock().lock();//acquire a read lock if no other thread has acquired a write lock.
                           //concurrent reads allowed.
    
    System.out.println("threads waiting for read/write lock: " + lock.getQueueLength());
    System.out.println("reader locks held "  + lock.getReadLockCount());
    try {
   for (Integer num: numbers) {
    System.out.println("<<<<<<<" + threadName + " reading: " + num);
   } 
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    finally{
     System.out.println(threadName + " releasing read lock");
     lock.readLock().unlock();  //lock will be released
    }
    
  }
}


Next, define the reader and writer threads.

public class Reader<T> extends Thread {
 
 private SharedData<T> sharedData;
 
 public Reader(SharedData<T> sharedData) {
  this.sharedData = sharedData;
 }

 @Override
 public void run() {
  sharedData.readData();
 }
}


public class Writer<T> extends Thread {
 
 private boolean oddNumber = true;
 private SharedData<T> sharedData;
 
 public Writer(SharedData<T> sharedData, boolean oddNumber ) {
  this.sharedData = sharedData;
  this.oddNumber = oddNumber;
 }

 @Override
 public void run() {
  for(int i=1; i<=2; i++) {
   if(!oddNumber && i%2 == 0) {
      sharedData.writeData(i);
   }
   else if (oddNumber && !(i%2 == 0)){
      sharedData.writeData(i); 
   }
  }
 }
}


Finally, the ReadWriteProcessor class that spawns thw worker read and write threads and pass the SharedData to the threads.

public class ReadWriteProcessor {
 
 public static void main(String[] args) throws Exception {
  
  SharedData<Integer> data = new SharedData<Integer>();
  
  //create some writer worker threads
  Writer<Integer> oddWriter = new Writer<Integer>(data, true);
  oddWriter.setName("oddWriter");
  Writer<Integer> evenWriter = new Writer<Integer>(data, false);
  evenWriter.setName("evenWriter");
  
  //create some reader worker threads
  Reader<Integer> reader1 = new Reader<Integer>(data);
  reader1.setName("reader1");
  Reader<Integer> reader2 = new Reader<Integer>(data);
  reader2.setName("reader2");
  Reader<Integer> reader3 = new Reader<Integer>(data);
  reader3.setName("reader3");
  
  //start the writer threads
  oddWriter.start();
  Thread.sleep(100);
  evenWriter.start();
  
  //start the reader threads
  reader1.start(); 
  reader2.start();
  reader3.start();
 }
}


The output will be something like:

threads waiting for read/write lock: 0
writer locks held 1
>>>>>oddWriter writing: 1
oddWriter releasing write lock
threads waiting for read/write lock: 3
writer locks held 1
>>>>>evenWriter writing: 2
evenWriter releasing write lock
threads waiting for read/write lock: 2
threads waiting for read/write lock: 1
reader locks held 3
threads waiting for read/write lock: 0
reader locks held 3
reader locks held 2
<<<<<<<reader1 reading: 1
<<<<<<<reader2 reading: 1
<<<<<<<reader3 reading: 1
<<<<<<<reader2 reading: 2
<<<<<<<reader1 reading: 2
<<<<<<<reader3 reading: 2
reader1 releasing read lock
reader3 releasing read lock
reader2 releasing read lock


There are other real life scenarios where the java.util.concurrent package with the following classes can be put to great use.
  • CountDownLatch
  • CyclicBarrier
  • Semaphore
  • Atomic classes

The real life scenarios with solution will be discussed in the ensuing parts.

Labels:

Aug 15, 2012

Article: Sample Java Resume or CV

A good Java resume or CV can make a world of a difference to open more doors to your career. Here is a sample resume that is extracted from my book "Java/J2EE Resume Companion" for experienced Java professionals. Even if you are not an experienced professional, it will guide you as to what you need to work towards. As you get more experienced, you need to update your Java resume or CV to highlight your accomplishments as shown below. There are more eye catching resume phrases and sample resumes for novice, career change, intermediate, cover letter, etc covered in the book in more detail with explanation.

John Experienced
Mobile: 0123 456 789   e-mail: java-interview@hotmail.com   blog:: java-success.blogspot.com

Professional Summary
  • Track record of delivering quality Java/JEE based solutions over 8+ years in the Investment, Insurance, Finance, Telecommunications, and Retail industries.
  • 2.0+ years as a hands-on architect, 2+ years as a technical lead, 3+ years in Java/JEE development, and 3+ years in system integration.
  • Well versed with entire development life cycle and facilitating agile practices.
  • Spearheaded the design and development of a Spring MVC, Spring, and Hibernate based mission critical system budgeted at $10 million, which earned high accolades in the independent code and design reviews conducted by an independent consultancy.
Achievements
  • Provided technical leadership on the architecture, design, and successful implementation of a mission critical 24x7 Java/JEE application valued at $10 million at BrandName Ltd.
  • Enjoyed repeated success in understanding business expectations, building effective solutions, and facilitating agile development practices.
  • Re-architected, refactored, and performance tuned a Websphere and J2EE based online insurance application, which previously came down almost daily, became a true 24x7 application at BrandName Ltd.
  • Instrumental in building our Java/JEE consultancy teams at Consultancy Ltd to grow from 6 consultants to 25 consultants and technical pre-sales activity in placing the consultants at client sites. 
     
Technical Summary


Java Technologies: 8+ years
Java 1.2, 1.3, 1.4, & 1.5, Swing, Applet, JMX, JDBC, and RMI. 3+ years in building stand-alone multi-threaded and TCP/IP socket based applications
JEE Technologies: 5+ years
JSP, Servlet, JSF, EJB, JMS, JSTL, EL, JNDI, JTA, and LDAP.
Frameworks/Libraries:  Spring (2+ years), Hibernate (3+ years) , Acegi, Spring-ws (6+ months), JSF Sun RI (2 years), Facelets, Ajax4JSF, JiBX, JUnit, Log4J, etc
Tools: Ant, Maven2, CVS, SVN, Eclipse, Rational Software Architect, etc
Platforms: Databases (Oracle, MySQL), OS (Linux, Windows), Application Servers (Tomcat, JBoss, Websphere, and Weblogic), Message Oriented Middleware (Websphere MQ and webMethods).
Design skills: OOA (Object Oriented Analysis) & OOD (Object Oriented Design), AOP (Aspect Oriented Programming), Design by contract, GoF design patterns, JEE & EJB design patterns, and UML.
Software Development Methodology: Agile methodology, RUP (Rational Unified Process), XP, TDD (Test Driven Development).
Other: SOAP Web services, HTML, CSS, Ajax, JavaScript, XML, XSD, XSL, XSLT, XSL-FO, WSDL, JAXP, Xalan, Xerces, SQL, and Jasper Reports.


Work Experience

Aug 2006 – Present: Brand name Ltd - Sydney

Position: Java/JEE Architect/Senior developer ( Design & development focus of a $10 million dollar high visibility and mission critical project using cutting-edge technologies and frameworks.)

Tasks/Achievements:
  • Architected a JSF, Websphere, Oracle, Spring, and Hibernate based 24x7 Web application.
  • Built an end to end vertical slice for a JEE based billing application using popular frameworks like Spring, Hibernate, JSF, Facelets, XHTML, Maven2, and Ajax by applying OO design concepts, JEE & GoF design patterns, and best practices.
  • Integrated other sub-systems like loans application, equity markets online application system, and documentation system with the structured products application through JMS, Websphere MQ, SOAP based Web services, and XML.
  • Designed the logical and physical data model, generated DDL scripts, and wrote DML scripts for Oracle 9i database.
  • Tuned SQL statements, Hibernate mapping, and Websphere application server to improve performance, and consequently met the SLAs.
  • Gathered business requirements and wrote functional specifications and detailed design documents.
  • Improved the build process by migrating it from Ant to Maven2.
  • Built and deployed Java applications into multiple Unix based environments and produced both unit and functional test results along with release notes.

Technologies used:   Java 1.5, JSF Sun RI, Facelets, Ajax4JSF, Richfaces, Spring, XML, XSL, XSD, XHTML, Hibernate, Oracle 9i, PL/SQL, MINA, Spring-ws, SOAP Web service, Websphere, Oracle, JMX, ANT, Maven2, Continuum, JUnit, SVN, TDD, and XP.




Feb 2003 – Aug 2006: Consultancy Ltd - Sydney Clients: Sure Insurance, ABC utility, and MQR Finance.

Position: Java/J2EE Technical lead/Senior developer (Leadership, design, and development focus of a number of Java/JEE based multi-tiered, high volume (10,000 to 100,000 users), and distributed applications.)

Achievements:
  • Managed and mentored a group of application developers, assigned responsibilities, elaborated use cases, managed project schedules, and module targets.
  • Reviewed code and encouraged developers to use key design patterns, such as façade, singleton, factory, command, proxy, strategy, decorator, etc.
  • Provided recommendations on OO design concepts, best practices, exception handling, and identifying and fixing potential memory, performance, and transactional issues.
  • Conducted interviews and made recommendations for hiring Java/JEE professionals.
  • Increased the number of JUnit tests from 30+ to 400+ in my watchful eye to improve the overall quality of the Java/J2EE based applications.
  • Produced detailed design documents with design alternatives, pros and cons for each approach and reasons for the recommended approach.
  • Analyzed “What if” scenarios and exceptional conditions and made recommendations to fill the gaps identified in the functional specs and detailed design.
Technologies used: Java 1.4, EJB 2.0, JNDI, JDBC, Servlet, JSP, Struts, Spring, XML, XSL, XSL-FO, XSD, XHTML, Hibernate, Oracle 8i, PL/SQL, FOP, Websphere, ANT, CVS, and JUnit.


Jan 2000 – Feb 2003: Telecom Ltd - Sydney

Position: Java/J2EE developer (Development focus of Java/J2EE based applications.)
 
Tasks/Achievements:
  • Designed and developed Struts like MVC 2 Web framework using the front-controller design pattern, which is used successfully in a number of production systems.
  • Spearheaded the “Quick Wins” project by working very closely with the business and end users to improve the current website’s ranking from being 23rd to 6th in just 3 months.
  • Normalized Oracle database, conforming to design concepts and best practices.
  • Resolved product complications at customer sites and funneled the insights to the development and deployment teams to adopt long term product development strategy with minimal roadblocks.
  • Convinced business users and analysts with alternative solutions that are more robust and simpler to implement from technical perspective while satisfying the functional requirements from the business perspective.
  • Applied design patterns and OO design concepts to improve the existing Java/JEE based code base.
  • Identified and fixed transactional issues due to incorrect exception handling and concurrency issues due to unsynchronized block of code.

Technologies used: Java 1.2/1.3, Swing, Applet, Servlet, JSP, custom tags, JNDI, JDBC, XML, XSL, DTD, HTML, CSS, Java Script, Oracle, DB2, PL/SQL, Weblogic, JUnit, Log4J and CVS.


Education
  • BEng in Mechanical Engineering with First Class Honors.
  • Training in Websphere Application Server.

Other skills and achievements
  • Secured 97% in Java test conducted by Brand name Ltd.
  • Passed the senior Java application developer test conducted by Consultancy Ltd.
  • Passed the EJB [1.1 & 2.0] Tests conducted by Sure Insurance Ltd.
  • Passed the Senior Developer Technical Interview with the Sun Micro Systems.
  • Maintaining a Java blog at http://www.java-success.bogspot.com/ to communicate my findings and thoughts.

Publication
  • Self-published a book entitled “Java/J2EE Job Interview Companion” (ISBN 978-1-4116-6824-9), which is a best seller at http://www.lulu.com and received 4.0 out of 5.0 star user rating on http://www.amazon.com.
  • Published a “Java World” article entitled “Some Java Topic”, which had over 40,000 views and positive review comments.

Professional Affiliations
  • Member of Australian Computer Society (ACS).

References
  • Available upon request.

Labels: ,

Aug 14, 2012

Article: Using Builders instead of Constructors to create Immutable objects

This article explains how you can create immutable objects in Java by using the builder design pattern as opposed to constructors. This will make your code more readable. Firstly, let's see what is not so elegant about using a constructor as shown below with a CashBalance object that takes 3 BigDecimal arguments. Then we will see how a builder class will make your code more intuitive.

Next time you are asked to explain a design pattern in an interview, you could pick this as opposed to the very common factory and singleton design patterns. This article also touch on other two important concepts like immutability and thread-safety.

import java.math.BigDecimal;

/**
 * Immutable, hence thread safe CashBalance object
 */
public final class CashBalance {
 
 private BigDecimal initialBalance;
 private BigDecimal totCredits;
 private BigDecimal totDebits;
 
 //construct
 public CashBalance(BigDecimal initialBalance, BigDecimal totCredits, BigDecimal totDebits) {
  this.initialBalance = initialBalance;
  this.totCredits = totCredits;
  this.totDebits = totDebits;
 }
 
 //only getter methods and not setter methods as it is an immutable object

}


So, why wasn't it elegant? The constructor takes 3 BigDecimal arguments, and for the user of the class it is not intuitive enough as to which is initialBalance, which is totCredits, etc. It would have been nicer if you could invoke the constructor something like


CashBalance bal = new CashBalance(initialBalance:BigDecimal.valueOf(250.00), 
                                  totCredits:BigDecimal.valueOf(250.00), 
          totDebits:BigDecimal.valueOf(250.00));

Unfortunately, you can't use above syntax. You need to invoke it as shown below.

CashBalance bal = new CashBalance(BigDecimal.valueOf(250.00), 
                                  BigDecimal.valueOf(250.00), 
          BigDecimal.valueOf(250.00));




Constructing the object with an empty constructor and 3 setter methods are more elegant, but that will make your object mutable. Here is the builder design pattern to the rescue. Here is the builder defined as an inner class to construct the CashBalance object.
import java.math.BigDecimal;

/**
 * Immutable, hence thread safe CashBalance object
 */
public final class CashBalance {
 
 private BigDecimal initialBalance, totCredits, totDebits;
 
 //construct
 public CashBalance(CashBalanceBuilder builder) {
  this.initialBalance = builder.initialBalance;
  this.totCredits = builder.totCredits;
  this.totDebits = builder.totDebits;
 }
 
 //builder design pattern
 public static class CashBalanceBuilder {
  
  //has same fields as the object it is going to build
  protected BigDecimal initialBalance, totCredits, totDebits;

  //define the setters with package private access
  void setInitialBalance(BigDecimal initialBalance) {
   this.initialBalance = initialBalance;
  }

  void setTotCredits(BigDecimal totCredits) {
   this.totCredits = totCredits;
  }

  void setTotDebits(BigDecimal totDebits) {
   this.totDebits = totDebits;
  } 
 }
 
 //only getter methods and not setter methods as it is an immutable object

}

Now, you can construct the CashBalance object from outside the class as shown below.



public static void main(String[] args) {
 CashBalance.CashBalanceBuilder builder = new CashBalance.CashBalanceBuilder();
 builder.setInitialBalance(BigDecimal.valueOf(250.00));
 builder.setTotCredits(BigDecimal.valueOf(250.00));
 builder.setTotDebits(BigDecimal.valueOf(250.00));
 CashBalance bal = new CashBalance(builder);
}

The above code does the job, but if you have many number of fields, the construction code will be very verbose. This can be further improved as shown below.

The following improved method changes the void setter methods to return the builder itself after setting the value.

import java.math.BigDecimal;

/**
 * Immutable, hence thread safe CashBalance object
 */
public final class CashBalance {
 
 private BigDecimal initialBalance, totCredits, totDebits;
 
 //construct
 public CashBalance(CashBalanceBuilder builder) {
  this.initialBalance = builder.initialBalance;
  this.totCredits = builder.totCredits;
  this.totDebits = builder.totDebits;
 }
 
 
 public static class CashBalanceBuilder {
  
  //has same fields as the object it is going to build
  protected BigDecimal initialBalance, totCredits, totDebits;

  //define the setters that return itself
  CashBalanceBuilder setInitialBalance(BigDecimal initialBalance) {
   this.initialBalance = initialBalance;
   return this;
  }

  CashBalanceBuilder setTotCredits(BigDecimal totCredits) {
   this.totCredits = totCredits;
   return this;
  }

  CashBalanceBuilder setTotDebits(BigDecimal totDebits) {
   this.totDebits = totDebits;
   return this;
  } 
 }
 
 //only getter methods and no setter methods as it is an immutable object

}

The beauty of the change is that now the oCashBalance construction code becomes:

public static void main(String[] args) {
   CashBalance.CashBalanceBuilder builder = new CashBalance.CashBalanceBuilder()
     .setInitialBalance(BigDecimal.valueOf(250.00))
     .setTotCredits(BigDecimal.valueOf(250.00))
     .setTotDebits(BigDecimal.valueOf(250.00));
   CashBalance bal = new CashBalance(builder);
}


So, these kinds of "know hows" will be noticed in peer code reviews and will help you earn a reputation as a go to person.


Similar post:

Labels:

Aug 10, 2012

Article: Functional programming in Java


In addition to the usual set of concepts like OO programming, and AOP (Aspect Oriented Programming), the functional programming offers a different way to think about software and solve some of the problems. For example, manipulating a collection of objects and mathematical computations. Java is not a functional programming language. Scala, Clojure, and Groovy are functional programming languages and run on the JVM and can interact with legacy Java classes. Programming with multiple programming languages like Java, Scala, Clojure, and Groovy is known as the polyglot programming.


This article focuses on how you can implement functional programming in Java with some verbosity. You can't do the following in Java to iterate through a collection of items and print each item


items.each(new Function() { public void apply(String item) { System.out.println("print:" + item); }})



But, you can achieve the similar results  in Java as shown below:

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class FuncProg {
 
 public static void main(String[] args) {
  //list of Java technologies
  List<String> items = Arrays.asList("Java", "JEE", "JDBC", "JMS");
  
  new Functional<String>().each(items, new FuncProg.Function<String>() {
   @Override
   public void apply(String item) {
    System.out.println("print: " + item);
   }
  });
 }
 
 //inner functional class. This could be defined as a separate outer class
 public static class Functional<T> {
  public void each(final Iterable<T> items, final Function<T> f){
     Iterator<T> it = items.iterator();
     while(it.hasNext()){
      f.apply(it.next());
     }
  }
 }
 
 //interface for the function
 public interface Function<T> {
  void apply(T item);
 }

}


Hmmm, you might think that the above functionality can be easily implemented by having two methods perform the relevant tasks. The true functional programming languages have much more concise syntax to achieve this. The real power of functional programming is realized when you want greater reuse of the functions as demonstrated below with Java code. You can have different combinations of iterating through a collection and computing each item in the collection. The Functional class provides different possible iteration logic and the Function interface provides different way to compute each item.



Step 1: Define the interface that applies some functionality to each item.

public interface Function<T> {
 void apply(T item);
}

Step 2: Define the class that can process a collection in different ways.

import java.util.Iterator;

public final class Functional<T> {
 
 public void each(final Iterable<T> items, final Function<T> f) {
  Iterator<T> it = items.iterator();
  while (it.hasNext()) {
   f.apply(it.next());
  }
 }

 public void eachSecondItem(final Iterable<T> items, final Function<T> f) {
  Iterator<T> it = items.iterator();
  int i = 0;
  while (it.hasNext()) {
   T item = it.next();
   // every second item
   if (i % 2 == 0) {
    f.apply(item);
   }
   ++i;
  }
 }
}

Step 3: Finally the sample test class and the implementations of the Function interface.

import java.util.Arrays;
import java.util.List;

public class FuncProg {
 
 public static void main(String[] args) {
     //list of Java technologies
  List<String> items = Arrays.asList("Java", "JEE", "JDBC", "JMS", "XML");
  
  //print each item: Function is defined as anonymous inner class but can be extracted to its own class
  new Functional<String>().each(items, new PrintFunction<String>());
  
  //print items that are abbreviation like JEE, JDBC, JMS
  new Functional<String>().each(items, new PrintAbbreviationFunction<String>());
  
  //print every second item
  new Functional<String>().eachSecondItem (items, new PrintFunction<String>());
  
  //print every second abbreviated item
        new Functional<String>().eachSecondItem (items, new PrintAbbreviationFunction<String>());
       
 }
 
 //Inner class. This could be in its own separate class
 static class PrintFunction<T> implements Function<T> {
  @Override
  public void apply(T item) {
   System.out.println("print: " + item);
  }
 }

 //Inner class. This could be in its own separate class
    static class PrintAbbreviationFunction<T> implements Function<T> {
      @Override
   public void apply(T item) {
    char[] chars = ((String)item).toCharArray();
    boolean allCaps = true;
    for (Character charVal : chars) {
     if(Character.isLowerCase(charVal)){
      allCaps = false;
      break;
     }
    }
    
    if(allCaps){
     System.out.println("print abbrevated item: " + item);
    }
   }
  }
}



The output for the above code is:
print: Java
print: JEE
print: JDBC
print: JMS
print: XML
print abbrevated item: JEE
print abbrevated item: JDBC
print abbrevated item: JMS
print abbrevated item: XML
print: Java
print: JDBC
print: XML
print abbrevated item: JDBC
print abbrevated item: XML


So, understanding functional programming is very useful.If you want true functional programming, with closures and functions as first class objects, pick another language. Scala, Clojure, Groovy all run on the JVM and can interact with legacy Java classes. This knowledge will be handy in writing code in JavaScript where functions are first class objects and can be referenced and passed around. In functional programming, you need to grasp the concepts like functions as first-class values, currying, immutable values, and closures. Look at the JavaScript examples for better understanding of functional programming.


Here is another example of creating a closure in Java  as shown below:


public class JavaClosure {

 public static void main(String[] args) {
  
  //Create a closure with an anonymous inner class
  Runnable workerThreadObject = new Runnable() {
   
   @Override
   public void run() {
    System.out.println("Printed from a newly created worker thread '" + Thread.currentThread().getName() + "'");
    
   }
  };
  
  System.out.println("Printed from main thread '" + Thread.currentThread().getName() +  "' that is always started by the JVM.");
  
  
  JavaClosure jc = new JavaClosure();
  jc.callAnotherMethod(workerThreadObject);
 }

 private void callAnotherMethod(Runnable workerThreadObject) {
  
  //invoke the closure method on the object that gets passed around
  Thread thread1 = new Thread(workerThreadObject);
  thread1.start();  //calls the run method in the closure 
  
 }

}


The output will be

Printed from main thread 'main' that is always started by the JVM.
Printed from a newly created worker thread 'Thread-0'


Closures are very common in JavaScript and you can learn more about JavaScript closures.

Labels:

Aug 8, 2012

Java logical thinking and coding questions and answers



When coding, the logical operators like &&, ||, and ! can be confucing. Here is an example that will evaluate a candidate's ability to process logic.

Q. What is the opposite of (score < 100) ?
A. Some might think it is (score > 100), but that is wrong. The correct answer is (score >= 100). The opposite of < is >=, and the opposite of > is <=.

Q. What will be the output of the following code snippet if the score is 120?

if (! (score < 100) ) { 
   System.out.println("The score is greater than 100");
}
else{
    System.out.println("The score is less than 100");
}

A. 
Prints "The score is greater than 100".

Q. In the code snippet below, how will you go about implementing the validate logic so that the validation fails (i.e returns false) when the security is
  • Included and
  • SecurityType == OTHER and 
  • SecuritySubType == OTHER
Show with and without using the not (i.e. !) operator


public class LogicalThinking1 {

 public static class Security {

  enum SecurityType {
   WARRANT, OPTION, OTHER
  }

  enum SecuritySubType {
   INVESTMENT, MARGINLENDING, OTHER
  }

  private boolean excluded;
  private SecurityType type;
  private SecuritySubType subType;

  public Security(boolean excluded, SecurityType type, SecuritySubType subType) {
   this.excluded = excluded;
   this.type = type;
   this.subType = subType;
  }

  // getters/setters, equals/hashCode and equals methods are omitted for
  // brevity

  public boolean validate() {
   //logic goes here
  }

 }
}


A. The validate method must return false when the security is  included, and of type OTHER and of subType OTHER. Under all other conditions, it must return true (i.e validation succeeds)

Approach-1: Using the ! operator.


    public boolean validate() {
      //use of && and ! operators
      return !(!excluded && SecurityType.OTHER == this.type && 
                SecuritySubType.OTHER == this.subType) ;
    }

The above code basically constructs the condition which returns "true" within the paranthesis and puts the not outside because it must return "false" (i.e the validation should fail) if the given condition is satisfied.

Approach-2: Now, the tricky bit is to achieve the same results without the the ! operator.


    public boolean validate() {
       return excluded || SecurityType.OTHER != this.type || 
              SecuritySubType.OTHER != this.subType ;
 }

It is achieved by using the reverse of the conditions shown within the paranthesis in the approach 1 with the 'or' operator instead of 'and'.



Here is the full code that you can experiment with

public class LogicalThinking1 {

 public static class Security {

  enum SecurityType {
   WARRANT, OPTION, OTHER
  }

  enum SecuritySubType {
   INVESTMENT, MARGINLENDING, OTHER
  }

  private boolean excluded;
  private SecurityType type;
  private SecuritySubType subType;

  public Security(boolean excluded, SecurityType type, SecuritySubType subType) {
   this.excluded = excluded;
   this.type = type;
   this.subType = subType;
  }

  // getters/setters and equals/hashCode and equals are omitted for
  // brevity

  public boolean validate() {
   //return !(!excluded && SecurityType.OTHER == this.type && SecuritySubType.OTHER == this.subType) ;
   return excluded || SecurityType.OTHER != this.type || SecuritySubType.OTHER != this.subType ;
  }

 }

 public static void main(String[] args) {
  //example only, need to cater for different input arguments like
  //false, OTHER, OTHER --> false
  //true, OTHER, OTHER  --> true
  //false, WARRANT, OTHER --> true
  //true, WARRANT, OTHER --> true
  //true, WARRANT, INVESTMENT --> true
  //false, WARRANT, INVENSTMENT --> true, etc. Basically 2 * 3 * 3 = 18 combinations are possible
  LogicalThinking1.Security security = new LogicalThinking1.Security(Boolean.FALSE, 
                                  LogicalThinking1.Security.SecurityType.WARRANT, 
                                  LogicalThinking1.Security.SecuritySubType.INVESTMENT);
  
  System.out.println("Validation:" + security.validate());
  
 }

}

Note: So, true && true && true is equivalent to !(false || false || false).


Q. Can you give a real life scenario where you may have to use the second form?
A. A typical real life scenario would be to implement the business validation rules. Some validation rules do change frequently due to legislative changes, etc and hard coding those rules within code is not a good idea. A more flexible approach would be to use a business rules engine like Drools or custom write your validation engine by storing the validation rules and relevant error messages in a database. This will enable the validation rules to be modified as it changes due to legislative changes or other business drivers. A very simplified database schema will look like

The rules are comma separated and the logical operator joining the rules is determined by the join_type. The join type can be either OR or AND.  The validation error message is displayed if the validation rule returns false. This means,  if the portfolio is not authorized and the cashAvailable is less than the cashDepletion then display the validation error message "There are insufficient funds". The cashAvailable, cashDepletion, and authorized are fields with corresponding getter methods in the bean "Portfolio". The version flag is important because you might be working in multiple streams of development sharing the same development database and you don't want a new rule that was added in one of the development streams to break the other streams. The validation engine will have code something similar to


boolean ruleResult = evaluateRule( .....);
if(!ruleResult) {
     displayValidationErrorMsg(....);
}


Q. Can you write code that will check if a given string input is a numeric?
A. You could use exception handling for this. If it throws an exception, it is not a numeric value.

public boolean isInteger( String input )  
{  
   try  
   {  
      Integer.parseInt(input);  
      return true;  
   }  
   catch(Exception e)  
   {  
      return false;  
   }  
}  

Alternatively, you could use regex (i.e. Regular Expressions)


public boolean isInteger( String input ) {
    if(input == null || input.length() == 0) {
    return false;
 }

 //using regex and conditional operator. '\d' means digit. Additional '\' to escape '\'.
    return if(input.replaceAll("\\d+","").length() > 0) ? false : true;
}


Labels: ,

Aug 7, 2012

How to standout from other Java/JEE professionals?

The developer world is very competitive. It's difficult to stand out from the pack, but this blog illustrates with three different scenarios where you can make yourself shine - at an interview, in your resume and at work to take the road less traveled.

"You can standout from the people who are more experienced or qualified than you are by presenting yourself in a different light as a well-rounded professional"


1. At Java/JEE Job Interviews

Let’s have a look at a typical Java/JEE Job interview scenario, where a candidate is asked the following popular interview question:

Q. What are the two main OO techniques for code reuse? Which one of those two will you favor and why?
A. Most candidates will correctly say:
  • Class inheritance.
  • Object Composition.
Some will then go on to say that "object composition should be preferred over class inheritance (i.e. implementation inheritance). A few will then continue to briefly explain that… “If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance can break all the sub classes, if the super class is modified. Do not use inheritance to just get polymorphism. If there is no ‘is a’ relationship and all you want is polymorphism, then use interface inheritance with composition, which gives you code reuse."

Some interviewers will be digging a bit deeper to make sure that the candidate really understands the OO concepts. Only a handful of candidates fully understand OO concept and will be able to elaborate with all the vital information as discussed below. This will enable those candidates to stand out and make a real impression on the interviewer(s) or prospective employers.   


"Class inheritance is static and it is done at compile time. In object composition, functionality is acquired dynamically at run-time by objects collecting references to other objects. This is possible because objects are accessed via their interfaces. So, one object can be replaced by other objects as long as they have the same type."

If permitted, these better candidates will also continue illustrating...  

"Interface inheritance should be preferred over implementation inheritance because it promotes the design concept coding to interface and reduces coupling. If you look at Gang of Four (GoF) design patterns, you can see that it favours interface inheritance to implementation inheritance. For example, the decorator design pattern used in java.io.* classes where responsibilities to objects are attached at run-time. The template method design pattern is a typical example for the implementation inheritance used mainly in application frameworks so that the overriding class can implemnt additional behavior". 


Note: Before you elaborate, ask the interviewer if it is okay to do so. In some interviews, you will be given a sample problem to code, where interviewer will be expecting good understanding of OO concepts. This is is just an example, and similar approach should be used to other interview questions relating 16 key areas like coding, performance, transaction management, etc.

In most cases, most of us know the basic answer and have worked with these practically, but being able to give a clear and concise answer will make you standout from the pack and get a job offer as a first preference and also power to negotiate your salary or rates. So, refreshing prior to interviews can really pay off. Also, being thorough in key fundamentals can make the interviewer over look any other weaknesses like not having worked extensively with a particular framework or an application server.

The second most important aspect is to prepare prior to your job interview by going through your past achievements using the SAR (Situation-Action-Result) technique. Here is an example.


Situation: Performance problem where the application server had to be restarted every day.
Action:
  • Used JMeter to simulate the load conditions and reproduce the issue. 
  • Identified the cause of the problem to be leaking database connections due to not properly closing the connections under exceptional scenario. 
  • Fixed the issue by closing the database connections in the finally block. 
  • Tuned the JVM settings and configured proper service timeouts.
  • Load and endurance tested the fixed code with the load testing tool JMeter to confirm that the issue has been fixed.
Result: The application becomes a true mission critical 24x7 type with much improved performance.


2. In your Java/JEE Resume(s)


Today many prospective employers embrace networking as the key source for finding a new position. Even when candidates are brought through a networked source, the first thing one would say is "Ask them to send their resume and I will see what I can do".  There are no clear cut definitions in separating an ordinary resume from a resume that stands out. This is always subjective based on the prospective employer. Some guidelines can be drawn to compare an ordinary resume with a one that is more effective.

An ordinary resume markets you just as a techie, uses ordinary phrases, information is not quantified, packed with irrelevant and trivial information fulfilling your needs not the employers’ needs, one size fits all, lots of responsibilities included…. phrases, and vital information is scattered throughout.

An outstanding resume markets you as well balanced candidate with technical skills, soft skills, and business focus, uses perfect phrases in a clear and concise manner, achievements are quantified, addresses employers’ real needs, replaces responsibilities included… phrases with on the job accomplishments, resume is customized to the position applied for, and the important information is in the first page. 

You could transform your achievement that was shown in the above example as an eye catching phrase on your resume as shown below:

  • Re-factored and performance tuned a JEE based online insurance application, which previously came down almost daily, became a true 24x7 application.

So taking the effort to writing a resume that stands out from the pack can open more doors for you than people who are better qualified than you.  


3. At Work

Being a well-balanced developer with good technical skills, outstanding soft skills, and business focus can do wonders to your career. Being a visible contributor is the key. Knowing the key areas and core concepts can save a lot of development time and improve the overall quality of the software. One example that comes to my mind is the very common "Concurrent Modification" Exception. Many of us get through solving this by trial-and-error in a time consuming manner. But if your fundamentals are clear then you would save heaps of time by knowing what exactly to be done to solve the problem. More often you make a good impression on your superiors and peers by pro-actively identifying potential issues related to performance, multi-threading, transaction management, security, quality coding, etc and fixing these issues before they become a major headache. Just being a techie alone not enough. One needs to have good technical skills complemented with excellent soft skills and good domain knowledge to succeed in his/her career.

So, how do you improve your problem solving skills?
  • Look at existing solutions to common problems, e.g. design pattern to solve a particular problem.
  • Create a problem and then solve it -- for example, create a connection leak issue and then fix it, create a memory leak and then fix it, create a performance issue due to back tracking regex or SQL with cartesian product and then fix it, create a security hole and then fix it, create a thread dead lock situation and then fix it, etc. 
  • Look at existing code and find ways to improve it. 
  • Surf the net for common programming problems and then solve them. 
  • Learn to use the right tools to solve your problems, for example jconsole, IDE debugger, Java remote debugger, jhat, jmap, wireshark, etc.  
  • Go to Java forums and help others with solving their problems.
  • Sometimes explaining the problem to another person or asking a few questions can unlock the mental floodgate. 

These gestures will make you stand out from the pack and consequently can do wonders to your career. You don’t have to wait for 2-3 years to be what you dream of becoming. Proactive self learning is the key to succeed in your career.


"Real job security in IT is not all about working for the same company, but keeping your knowledge and skills relevant. You need to have both technical & non-technical skills"

My books and blog posts focus on these knowledge and skill building.
  • 16 technical key areas to solve problems & impress your peers, superiors, and interviewers. 
  • Java/JEE fundamentals through 650+ questions and answers approach with lots of diagrams, examples, and code snippets.
  • Core Java Essentials focusing on writing OO and data structure code.
  • Career making know hows, job hunting, networking, resume writing, interviewing, and passive income generating ideas/tips.

Your career is your responsibility, and not your company's. I strongly believe that this site will shed lots of light to motivate, inform, and fast-track your Java career. It is very satisfying get positive feedback.

Labels: