4/10/2018

Generate Cron Expression in SOP

Sometimes, the cron expression is difficult to generate while you want to resolve urgent things. Let us SOP to generate cron expression.

Here are two links to easily generate cron expression.

  1. http://www.cronmaker.com/
  2. Cron Expression Generator & Explainer - Quartz

1/23/2018

Building Software

Based on Kent Beck describes three activities in building software:

  1. First make it work. (make business or money first)
  2. Then make it right. (refactor code ...you and others can understand it as needs change)
  3. Then make it fast. (refactor code for "needed" performance, don't do this first)

12/26/2017

My Development Principals in Java World

Here are my development principals in java world. 

  • Simple and Small objects
  • Immutable object
  • Interface, it’s a contract that our object must obey in order to communicate with other objects.
  • Don’t use static object and method
  • Don’t use null anywhere
  • Multiple contractor to reuse your code-fee
  • Always chain exception
  • Aspect-oriented programming for special handling
  • A unit test is a part of class.
  • ...continue...

9/18/2014

Switch JDK verson on Mac

When you installed many of jdk versions, there are two ways to switch the jdk.

  • Using Java Preferences application, but Apple decided to remove it.
  • Using java_home in console

After a lot of googing a found simple way solution, I’d like to share.

Edit your profile /.bash_profile or /.zshrc

alias java_ls='/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d[,_]" | cut -d , -f 1 | colrm 1 4 | grep -v Home'

function java_use() {
export JAVA_HOME=$(/usr/libexec/java_home -v $1)
export PATH=$JAVA_HOME/bin:$PATH
java -version
}

Execute java_ls

1.8.0_11
1.7.0_45
1.6.0_65-b14-462
1.6.0_65-b14-462

Execute java_use 1.8

java version "1.8.0_11"
Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)

3/17/2014

Different Between Reader/Writer and InputStream/OutputStream

A Reader/Writer are designed for character-oriented streams. If the information we are reading is all text, then the Reader will take care of character decoding for you. For example, you can read all in text file to represent the characters in Java.

An InputStream/OutStream are designed for byte-oriented streams. It grabs/writes the data byte by byte without performing any kind of translation. For example, you can read/write image data, mp3…or any binary files.

3/14/2014

Observer Pattern

The observer pattern is a behavioral design pattern which defines a one-to-many relationship between objects. When the observable object changes its state, all dependent objects are notified and updated automatically. In stock market case, a stock trading system keeps track of all trades of specific stocks (subjects) and must inform all trader (observers) when a new trade has been made for new prices.

Word Definitions

Observer

someone who sees or notices something

Observable

something that is observable can be seen or measured

Code Example

Event-Bus

To be honest, I don’t want the Observable class to be tightly coupled with the classes that are interested in observing it. It doesn’t care who it is as long as it fulfils certain criteria. I think we might consider the event-bus if there are a lot of usage in your system. There are two reasons :

  • It replaces the tight coupling introduced by explicit listeners with a flexible loosely coupled design.
  • An event bus can be thought of like the observer pattern with an extra layer of decoupling.

Reference Link

3/13/2014

Communicate between Threads in Java

The Java wait-and-notify mechanism is a way for inter thread communcation in Java. It allows one thread to communicate to another thread that a particular condition has occured. The wait-and-notify mechanism cannot be used to replace the synchronized mechanism and does not solve the race condition problem. In fact, wait-and-notify must be used in conjunction with the synchronized lock to prevent a race condition.

Just like the synchronized mechanism, the wait-and-notify mechanism is available from every object. There are three methods to invoke on any object should have the object monitor.

wait, notify and notifyAll methods in Java doc

  • wait()

    The current thread must own this object’s monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object’s monitor to wake up either through a call to the notify method or the notifyAll method.

  • notify()

    Wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object’s monitor by calling one of the wait methods.

  • notifyAll()

    Wakes up all threads that are waiting on this object’s monitor.

On the other question, wait and notify method are used to put thread on wait but they are not in Thread class along with Sleep() or Join() method instead they are declared in Object class. This article has few reasons which make sense on Why Wait and Notify is in Object Class in Java.

Code for Producer-and-Consumer Problem